SSH:secure shell, protocol;安全的远程登陆,默认端口号为22。SSH在远程连接时候,通过对传输过程的数据进行加密,避免传输数据被非法窃取,保证数据传输过程的安全性。因而被广泛使用。
SSH的登陆方式常见的有两种:
一,基于用户和口令的登陆方式:
这个很好理解,在平常的工作中,使用这登陆方式较多。如果能够ping通另一台主机,并且知道另外一台主机的用户名和密码时,使用SSH可以直接登陆。如下:
B主机的IP:
[20:03:59 root@centos6 ~]#ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:78:E9:C5
inet addr:172.16.129.172 Bcast:172.16.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fe78:e9c5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1119 errors:0 dropped:0 overruns:0 frame:0
TX packets:151 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:107863 (105.3 KiB) TX bytes:17876 (17.4 KiB)
通过上面命令可以查看B主机的IP为:172.16.129.172,因此就可以在A上通过SSH命令来登陆到B主机:
[15:33:26 root@centos7 ~]#ssh 172.16.129.172 #A主机位centos7系统
The authenticity of host '172.16.129.172 (172.16.129.172)' can't be established.
RSA key fingerprint is SHA256:o8xgQCvMdFMe7nVRCXkFeUfg1oYfR5FnSjd0fU3t0g4.
RSA key fingerprint is MD5:9b:89:7c:78:ea:24:50:91:26:e9:5c:77:bf:1a:4d:71.#对方主机公私
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes #会连续两次提醒你确认登陆,并且第二次输入完整的'yes'来确认。
Warning: Permanently added '172.16.129.172' (RSA) to the list of known hosts.#如果是第二次再次登陆,这些确认信息将不会出现,因为A主机拿到了B的公匙信息。
root@172.16.129.172's password: #输入对方主机的密码
Last login: Fri Jun 21 20:02:02 2019 from 172.16.9.9 #登陆成功提示信息
[20:09:13 root@centos6 ~]#hostname #可以看到已经登陆到centos6主机上。
centos6.localdomain
上述方式就是基于用户名口令的方式来进行登陆。下面我们着重讲解一下第二种登陆方式。
二,SSH服务基于密匙登陆验证:
<一>,实验环境:
主机名称 | 主机IP | 主机角色 |
centos7(A主机) | 172.16.128.89 | 服务端 |
centos6(B主机) | 172.16.129.172 | 客户端 |
<二>,整个验登陆过程如下图:
整个过程基本可以分为7个步骤:
1 ),首先在客户端生成一对密钥
命令:[21:28:10 root@centos6 ~]#ssh-keygen
客户端生成的密匙和公私是一对,可以互相加密和解密。该密匙对的创建是基于用户当前的用户名的。例如对于B主机可以在root和普通用户账户分别创建密匙:
B主机以root用户创建密钥:
[21:28:10 root@centos6 ~]#ssh-keygen #创建密匙对命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): test_root #输入你想创建的密匙对名称
Enter passphrase (empty for no passphrase): #询问你是否需要输入密码。输入密码之后,以后每次都要输入密码。请根据你的安全需要决定是否需要密码,如果不需要,直接回车。
Enter same passphrase again:
Your identification has been saved in test_root. #提示密匙的私钥已经创建并保存
Your public key has been saved in test_root.pub. #提示密匙的公钥已经创建并保存
The key fingerprint is: #生成的SSH密钥指纹
43:bf:c4:5f:40:10:32:f2:bd:4d:ad:a3:26:e2:66:9e root@centos6.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| . o oo. |
| o + . . |
| o . o . |
| . o + o |
| S = + . |
| o + o |
| . . + . |
| .oo o |
| +E |
+-----------------+
[21:36:40 root@centos6 ~]#cd /root/.ssh #进入密匙对保存的目录。
[21:37:11 root@centos6 .ssh]#ll #该目录下生成的文件。
total 12
-rw------- 1 root root 1675 Jun 21 20:54 id_rsa #id_rsa 生成的私钥
-rw-r--r-- 1 root root 406 Jun 21 20:54 id_rsa.pub #id_rsa.pub 生成的公钥
-rw------- 1 root root 395 Jun 21 21:27 known_hosts
[21:37:11 root@centos6 .ssh]#cat id_rsa.pub #查看root下生成的公钥
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsMSpE0gJpUe0KRewW6FDXT3n9GNOZSDz/HHRB1FqcE204EVO1/d2wpEb4ncoxx5jR7P+BLLbNGRyv4K3/eGwjKT3BcHX9rf2BJlUK7LYys1bQ7YXGxkoWDlrOT4kN45lHYzKZ1TWvMnfvhQDdXvC08bvV+F4vHYUKfntREr/WfECISl5tJf6bj+klLXNZbCEgdx+2A/SX2RCHV8qcJbDR0b7XOzcsovAlgswT+nk3zBazKcDw/E60mmUPC7eABolCoIEUpCHVXdSVPOf5GQvwt89IjbRzL9zRRip2AixK8lzYA0zzpM0deOiyBIF7L9irEfneK9cQeHqAMQWvWAUnQ== root@centos6.localdomain
B主机以testtesthostB账户创建密钥:
[21:44:08 root@centos6 ~]#su - testhostB #切换到testhostB账户
[21:44:53 testhostB@centos6 ~]$ssh-keygen #创建密匙命令
Generating public/private rsa key pair.
Enter file in which to save the key (/home/testhostB/.ssh/id_rsa): test_testhostB#密匙对名称
Enter passphrase (empty for no passphrase): #不输入密码
Enter same passphrase again:
Your identification has been saved in test_testhostB.
Your public key has been saved in test_testhostB.pub.
The key fingerprint is:
14:61:84:a3:7e:ba:4c:35:aa:b4:7f:3a:50:75:8a:54 testhostB@centos6.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| .Eo=. |
| . +... |
| . + +. |
| + .. |
| o o S |
| . .o.. |
| ..oo |
| . =o . |
| o.== |
+-----------------+
[21:49:37 testhostB@centos6 ~]$ll -a
total 40
drwx------ 4 testhostB testhostB 4096 Jun 21 21:45 .
drwxr-xr-x. 4 root root 4096 Jun 21 20:51 ..
-rw------- 1 testhostB testhostB 17 Jun 21 20:52 .bash_history
-rw-r--r-- 1 testhostB testhostB 18 Mar 23 2017 .bash_logout
-rw-r--r-- 1 testhostB testhostB 176 Mar 23 2017 .bash_profile
-rw-r--r-- 1 testhostB testhostB 124 Mar 23 2017 .bashrc
drwxr-xr-x 2 testhostB testhostB 4096 Nov 12 2010 .gnome2
drwxr-xr-x 4 testhostB testhostB 4096 May 10 11:36 .mozilla
-rw------- 1 testhostB testhostB 1671 Jun 21 21:45 test_testhostB #生成的私钥
-rw-r--r-- 1 testhostB testhostB 411 Jun 21 21:45 test_testhostB.pub #生成的公钥
[21:49:43 testhostB@centos6 ~]$cat test_testhostB.pub #查看生成的公钥
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqsrzqAmT3BBzzMjqcO+ur9QB2oW7ZEwZJ/P+2kv/L9hrGkNfoDqS/A/FKMPkA57UgtwTH6JJ5rAzEIh4yzLM2ndFun/Q+3efZyZd1GACkam5/qYFfOQFOKgDH+XkclZAzLqKiFEz5DHPAutveq+GHVpXgCeOyMXM1jhg0Tptv/5xqow85SkMkzE1S7XnYfvz8pHBmrBm8TIPkxhrVdzNCiXIa9b4hGbse1+uy991cNO/JROsWCSBQBdGI8HvJnZb7HgMWTNc3Qm50jxYgC2YleD5FThn6NW3DUoRmUJKyww9ihVf8EzY9s4QSASgSr2MaY2PLfqiN48o/XvCp5Jtiw== testhostB@centos6.localdomain
通过上面测试可以看到在不同账户下创建的密钥结果是不一样的,SSH创建密钥是基于当前用户账户的。
2 ),将客户端的公钥ssh-copy-id 拷贝到服务端,(将B主机root账户的公钥拷贝到A主机):
命令:[23:11:42 root@centos6 .ssh]#ssh-copy-id -i ./id_rsa.pub root@172.16.128.89
[23:11:42 root@centos6 .ssh]#ssh-copy-id -i ./id_rsa.pub root@172.16.128.89 #通过ssh-copy-id将B主机公钥拷贝到A主机root目录下,完成公钥的分发
root@172.16.128.89's password:
[09:14:41 root@centos7 .ssh]#ll #可以看到公钥文件已经被拷贝到当前文件夹下
total 8
-rw------- 1 root root 406 Jun 22 09:14 authorized_keys
-rw-r--r-- 1 root root 1319 Jun 21 20:17 known_hosts
3 ),当客户端再次发送一个连接请求,包括ip、用户名:
[09:38:25 root@centos6 ~]#ssh root@172.16.128.89 #采用SSH直接登陆,并且不要输入密码
Last login: Sat Jun 22 09:46:24 2019 from 172.16.129.172
[09:47:33 root@centos7 ~]#hostname #已经成功登入到A主机
centos7.localdomain
4 ), 服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如:13579
5 )服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端
6 ),得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的 字符串发送给服务端
7)服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一 致,就允许免密码登录