1.SSH(Secure shell)是一种能够以安全方式提供远程登录的协议。也是目前远程管理Linux系统的首选方式。 在Linux系统中想要使用SSH协议来远程管理系统,就需要配置sshd服务程序。sshd是一种基于SSH协议开发的一款远程管理服务程序。不仅使用方便快捷,而且能够提供两种安全验证方法。 。。。。基于口令的验证——用账户密码来验证登录。 。。。。基于密钥的验证——需要在本地生成密钥对,然后把密钥对中的公钥上传至服务器, 并于服务器中的公钥比较。该方式相较于来说更安全。 2.我们都知道“linux系统中的一切都是文件”,因此在Linux系统中修改服务程序的运行参数,实际就是在修改程序配置文件的过程。sshd服务的配置信息保存在/etc/ssh/sshd_config文件中。运维人员一般会把保存最主要配置信息文件称为主配置文件,因为这些文件参数都是默认的,所以配置文件中许多以#号开头注释行,要想要这些配置参数生效,需要在修改参数后在去掉前面的#号。
1.准备2台虚拟机 计算机名为xxgc-01,角色为Redhat7.4服务器,IP地址为192.168.10.10/24 计算机名为xxgc-02,角色为Redhat7.4服务器,IP地址为192.168.10.20/24 2.提示:在Redhat7.4系统中,已经默认安装并启用了 sshd 服务程序。如果不是7版本自行通过yum软件仓库安装
1.连接ssh服务 [root@xxgc-01 ~]# ssh 192.168.10.20 The authenticity of host ‘192.168.10.20 (192.168.10.20)’ can’t be established. ECDSA key fingerprint is SHA256:Pxribu2yq+q4Yf58+LwxzZuVjHCtuh3d3ssrS05W6a4. ECDSA key fingerprint is MD5:9e:55:66:11:14:88:bf:5e:87:10:48:16:c0:d0:58:db. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.10.20’ (ECDSA) to the list of known hosts. root@192.168.10.20’s password: //此处输入远程主机root管理员的密码 Last login: Sun Oct 11 08:18:33 2020 [root@xxgc-02 ~]# [root@xxgc-02 ~]# exit logout Connection to 192.168.10.20 closed. [root@xxgc-01 ~]#
2.其他参数可在配置文件/etc/ssh/sshd_config 中利用vim编辑器进行修改
1.各位通过名字就应该明白了,这种方式比第一种方式更加安全。那么具体配置如下: 本例使用普通用户来进行实验 1.在服务器xxgc-03上建立用户 student,并设置密码:
[root@xxgc-03 ~]# useradd yyy [root@xxgc-03 ~]# echo “redhat” | passwd --stdin yyy//passwd yyy Changing password for user yyy. passwd: all authentication tokens updated successfully. [root@xxgc-03 ~]#
2.在客户机xxgc-02中生成“密钥对”。查看公钥id_rsa.pub和私钥id_rsa。 [root@xxgc-02 ~]# 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:7IxppN08mwJTTwnGacRG+8Jl+8h0faNMfhPDWWQBMDU root@xxgc-02 The key’s randomart image is: ±–[RSA 2048]----+ | =o. ooE…+| | O. . .o | | +…o. .| | …=o. . . o | | .+oS . o B | | o+ @.+ + o + | | .o= O . + o | | … + . . | | .o | ±—[SHA256]-----+ [root@xxgc-02 ~]# [root@xxgc-02 ~]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDUIT1cPz9UyTtUjKEnwm6871xbq25srOsEvWl2rSqg4B+ANq2qR/AWBTkb+7+K7+3c9w0uHn3PBfITkHdLoDiv179rqsx8zUCdtCkofUm8A6nBGpS/QAHPUcW2QnEwZLf3VBstzJs36lFEJNn7WZAofoKm92pN5kJx7VQ3PETv1sF9TZmZNnx6gdJCp76KegPys8/l++n6YL3KhH7JIZE+uoBgsmFInj18ksieygf/cOP6I/qRz4Q3cKbrrIlKXXww/1oHSnp1mBmO/oahQ6Zc1zVdYdR8FK8lE8jhNRvKusXBRC1o4aQlzetA6x/wzkIUqRvq3iNqNI/uGYZfcBCp root@xxgc-02 [root@xxgc-02 ~]# [root@xxgc-02 ~]# cat /root/.ssh/id_rsa
3.把客户机xxgc-02中生成的公钥文件传送至远程主机。
[root@xxgc-02 ~]# ssh-copy-id yyy@192.168.10.30 The authenticity of host ‘192.168.10.30 (192.168.10.30)’ can’t be established. ECDSA key fingerprint is SHA256:Pxribu2yq+q4Yf58+LwxzZuVjHCtuh3d3ssrS05W6a4. ECDSA key fingerprint is MD5:9e:55:66:11:14:88:bf:5e:87:10:48:16:c0:d0:58:db. 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 yyy@192.168.10.30’s password: //输入远程主机密码
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘yyy@192.168.10.30’” and check to make sure that only the key(s) you wanted were added.
[root@xxgc-02 ~]#
4.设置服务器xxgc-01 (第65行左右),使其只允许密钥验证,拒绝传统的口令验证方式。将 “PasswordAuthentication yes”改为“PasswordAuthentication no“,保存退出wq,并重启服务。 "… … … To disable tunneled clear text passwords, change to no here! #PasswordAuthentication yes #PermitEmptyPasswords no PasswordAuthentication no
#Change to no to disable s/key passwords #ChallengeResponseAuthentication yes ChallengeResponseAuthentication no … … … " [root@xxgc-03 ~]# systemctl restart sshd
5.现在我们开始使用sshd服务 [root@xxgc-02 ~]# ssh yyy@192.168.10.30 [yyy@xxgc-03 ~]$ [yyy@xxgc-03 ~]$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.30 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::6a62:29b1:5324:320b prefixlen 64 scopeid 0x20 ether 00:0c:29:f1:01:99 txqueuelen 1000 (Ethernet) 6.在xxgc-03上查看xxgc-02客户机的公钥是否传输成功。本例传输成功。 [root@xxgc-03 ~]# cat /home/yyy/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHQMBzVkEdZq+Q0L/dGDrjWocb+RSUB45Z9+JDs4iXqSS9nemohnbD7uA0G8rk/6Tidft8d8m89IE0ympeLNXAmNDjcQRnR85wc3fVEv9mNshS19wwH291fmvvf/pbrwTxNHU6T3rbhVP1NnFygEGf/z78sarpcYBudqenQhGARdkn4/hgJ55gqqqgfy2c3ION4FTj5o5OHauMrCFCK5uEivZdA2pTEv3Xq4ng55xlSqDyE/+uA0FZ3OyVc6Aym++pM2MvAblcYjkX4uhIclM4UWijwGYjmDu294ux+LbokKAfzuUpoH0wf8iM2Y4vaLv+xK7fPlqSkngAsWtX7SoJ root@xxgc-02 [root@xxgc-03 ~]#
1.通过了以上实例,我们完成了简单的sshd服务器,我们继续实验还可以得出如果在root管理员身份下生成密钥对,那么普通用户也可使用。但是普通用户下生成的密钥对,root用户不可使用,这也是一种安全完善机制吧。具体实验,各位就私下验证。下面就是一些基础命令。 [root@xxgc ~]# systemctl status sshd —查看状态 [root@xxgc ~]# systemctl stop sshd —停止服务 [root@xxgc ~]# systemctl restart sshd —重启 [root@xxgc ~]# systemctl start sshd —启动服务 [root@xxgc ~]# systemctl enable sshd —加入开机自启动 [root@xxgc ~]# systemctl is-enabled sshd —查看是否开机自启动 [root@xxgc ~]# systemctl disable sshd —禁止开机自启 [root@xxgc02 ~]# systemctl reload sshd —重新加载配置文件(不停止服务)
[root@xxgc ~]# vim /etc/ssh/sshd_config
[root@xxgc ~]# hostnamectl set-hostname xxgc01
[root@xxgc01 ~]# ssh 172.25.1.6 -l mylinux
[root@xxgc01 ~]# ssh mylinux@172.25.1.6
[root@xxgc02 ~]# setenforce 0 — 临时禁用SELinux
[root@xxgc02 ~]# systemctl stop firewalld.service —关闭防火墙
[root@xxgc01 ~]# ssh 172.25.1.6 -p 220