远程管理工具SSH的使用方法

SSH简介

SSH(Secure Shell)是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux 系统的首选方式。在此之前,一般使用 FTP 或 Telnet 来进行远程登录。但是因为它们以明文的形式在网络中传输账户密码和数据信息,因此很不安全,很容易受到黑客发起的中间人攻击,这轻则篡改传输的数据信息,重则直接抓取服务器的账户密码。

想要使用 SSH 协议来远程管理 Linux 系统,则需要部署配置 sshd 服务程序。sshd 是基于 SSH协议开发的一款远程管理服务程序,不仅使用起来方便快捷,而且能够提供两种安全验证的方法:

  • 基于口令的验证—用账户和密码来验证登录;
  • 基于密钥的验证—需要在本地生成密钥对,然后把密钥对中的公钥上传至服务器,并与服务器中的公钥进行比较;该方式相较来说更安全。

sshd 服务的配置信息保存在/etc/ssh/sshd_config 文件中,sshd 服务配置文件中包含的参数以及作用如下表:

参数 作用
Port 22 默认的 sshd 服务端口
ListenAddress 0.0.0.0 设定 sshd 服务器监听的 IP 地址
Protocol 2 SSH 协议的版本号
HostKey /etc/ssh/ssh_host_key SSH 协议版本为 1 时, DES 私钥存放的位置
HostKey /etc/ssh/ssh_host_rsa_key SSH 协议版本为 2 时, RSA 私钥存放的位置
HostKey /etc/ssh/ssh_host_dsa_key SSH 协议版本为 2 时, DSA 私钥存放的位置
PermitRootLogin yes 设定是否允许 root 管理员直接登录
StrictModes yes 当远程用户的私钥改变时直接拒绝连接
MaxAuthTries 6 最大密码尝试次数
MaxSessions 10 最大终端数
PasswordAuthentication yes 是否允许密码验证
PermitEmptyPasswords no 是否允许空密码登录(很不安全)

安装SSH服务端

在centos和redhat系统上安装执行如下命令:
yum -y install openssh-server      #一般默认一已经安装
在Ubuntu上安装执行如下命令:
sudo apt-get -y install openssh-server       #一般默认没有安装,需要手动安装
说明:如果在Ubuntu上无法安装openssh-server,执行如下命令,再次安装即可
sudo apt-get update

启用SSH

CentOS6:
service sshd start      #开启服务
chkconfig sshd on      #加入开机自启动
CentOS7:
systemctl start sshd      #开启服务
systemctl enable sshd      #加入开机自启动

客户端安装SSH工具

如果是Windows系统可安装如下工具
xshell、CRT、putty
如下是Xshell6下载,其他工具可去官网自行下载:
链接:https://pan.baidu.com/s/10Hlri4dxl0W38nAZUEt5hA
提取码:m462
如果是类Linux系统可执行如下命令安装(一般系统默认都会安装):

centos或者redhat:
yum install openssh-server-client
ubuntu:
sudo -y apt-get openssh-server-client

客户端连接SSH服务

1.账户密码连接

ssh root@IP

在这里插入图片描述
SSH config 命令
config为了方便我们批量管理多个ssh
config存放在~/.ssh/config
config配置语法关键字:

Host:别名
HostName:主机名
Port:端口(默认22)
User:用户名
IdentityFile:秘钥文件的路径

SSH安全免密码登录:ssh key
使用非对称加密方式生成公钥和私钥
私钥存放在~/.ssh目录
公钥可以对外公开,放在服务器的~/.ssh/authorized_keys
Linux平台生成ssh key

ssh-keygen -t rsa
ssh-Keygen -t dsa
ssh-add ~/.ssh/私钥文件(加载私钥ssh本地)

示例:
1.在客户端主机中生成“密钥对”

[root@localhost ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #默认
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):    #回车
Enter same passphrase again:   #回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:uqpR4dNarkwh+dDOY9R1wE9DHvWSdDsLWWLGrohWZes [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|       ...oo* o  |
|        .o==.B . |
|    .   .=o+= +  |
|   + + ...o .o o |
|  + B +oSo .  .  |
|   O *o.. E      |
|  . O.o          |
|   = o .         |
|  ..+..          |
+----[SHA256]-----+

2.把客户端主机中生成的公钥文件传送至远程主机:

[root@localhost ~]# ssh-copy-id 172.16.56.131
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.56.131 (172.16.56.131)' can't be established.
ECDSA key fingerprint is SHA256:7Egkd79vLczeQe+gWsyJ+feFmiG8OO0y0QR66Kc0H7s.
ECDSA key fingerprint is MD5:47:fb:a3:67:3e:f7:07:aa:c8:5d:fa:01:46:fb:86:df.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:       #此处为远程服务器的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.56.131'"
and check to make sure that only the key(s) you wanted were added.

在服务器上进行查看,公钥是否上传
[root@localhost ~]# cd .ssh/   #切换到存放公钥的目录下
[root@localhost .ssh]# ls
authorized_keys       #已上传成功
[root@localhost .ssh]# cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDiS+pn5f7aGCwQZrM95T39RoajDCrrdKvfyWjYSYu7y2bpHs8lWP81pVcEQYyLpCZDBhaYbujNuapDQHmt/l7Cgl3ebuYmuf8T39Zo8W454kHF65BpZXhBmyxvNkX0yWhgebqqYR5bIdCFa6OSVRm+szisXF0TMRofMGDDRkHhTY3wL9dEdxZa9ZmbJC2tkv5kTML07bVyeIMzpJcizuUrOpVIg/Pvp7tvICPglLxsAPAuZaxsmUHjIN49GBNrAq8Eu+h5ZUbjHbE4d0M2T3inbpIRljvq6zLOaGSRVhR0GsbMSyOsttP9l/+WCkvA/h0gDFAdItKpeHj884TrSHTP [email protected]

3.对服务器进行设置,使其只允许密钥验证,拒绝传统的口令验证方式

vim /etc/ssh/sshd_conf       #修改如下一行
PasswordAuthentication no

4.重启服务进行验证

systemctl restart sshd
[root@localhost ~]# ssh 172.16.56.131        #无需账户密码直接登录,安全秘钥连接成功
Last login: Fri Dec 21 20:38:53 2018 from 172.16.56.1
[root@localhost ~]# 

远程传输命令
说到ssh远程连接,顺便也说一下一个在Linux系统上的远程传输的命令–scp。scp(secure copy)是一个基于 SSH 协议在网络之间进行安全传输的命令,其格式为“ scp[参数] 本地文件 远程帐户@远程 IP 地址:远程目录”。
scp 命令中可用的参数及作用:

参数 作用
-v 显示详细的连接进度
-P 指定远程主机的 sshd 端口号
-r 用于传送文件夹
-6 使用 IPv6 协议

上传

[root@localhost ~]# echo "Welcome to centos.org" > test.txt     #创建一个测试文件
[root@localhost ~]# scp /root/test.txt 172.16.56.131:/home       #上传至服务器/home下
test.txt                                                                         100%   22    32.4KB/s   00:00    
[root@localhost ~]# ssh 172.16.56.131        #连接到服务器查看
Last login: Fri Dec 21 21:02:35 2018 from 172.16.56.132
[root@localhost ~]# cd /home/
[root@localhost home]# ls
test.txt       #已上传

下载

[root@localhost ~]# scp 172.16.56.131:/etc/centos-release /root
centos-release                                                                   100%   38    39.9KB/s   00:00    
[root@localhost ~]# ls      #已下载到本地/root目录下
anaconda-ks.cfg  centos-release  Kmaster-slave.+157+17661.key  Kmaster-slave.+157+17661.private  ma6174  test.txt

Windows平台生成ssh key
xshell工具(tool)生成key,将生成的公钥保存在本地,然后cp公钥的内容,粘贴在服务器的/root/.ssh/authorized_keys 文件中即可,然后修改/etc/ssh/sshd_conf配置文件,配置方法和上面的一样。

猜你喜欢

转载自blog.csdn.net/weixin_42342456/article/details/85197555