ssh 有2種認證方式:一種是密碼認證,一種是密匙認證。shell
然而密碼認證的方式是比較不安全的。首先若是密碼強度不是很高的話很容易就被別人猜出來了(固然不是用人腦猜),其次是密碼在傳輸的過程當中容易被別人劫持(專業術語叫 中間人攻擊)。詳細的ssh認證過程能夠看這個帖子,介紹的比較全。(傳送門)安全
對於運維人員來講管理大量的服務器人工輸入密碼是不現實的,將密碼寫在登陸腳本里也是不可取的。那麼使用密匙登陸服務器是比較好的作法。bash
場景: server1(127.0.0.1), server2(192.168.0.6), server3(192.168.0.7)服務器
1. 在server1服務器上面使用命令 ssh-keygen 生成公鑰私鑰。運維
bingaos-MacBook-Pro:~ bingao$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/Users/bingao/.ssh/id_rsa): /Users/bingao/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/bingao/.ssh/id_rsa. Your public key has been saved in /Users/bingao/.ssh/id_rsa.pub. The key fingerprint is: SHA256:tm55gD9g9rv3X3+LOnkn4FP95SCJb0Wl1Gu2zt3wm0Y bingao@bingaos-MacBook-Pro.local The key's randomart image is: +---[RSA 2048]----+ | . | | . o| | . o.| | o+ | | .S . o+ .| | =.....o.+E.| | o +.o..+o+=*| | .* o=oo.*X| | .+* +*o=+=| +----[SHA256]-----+ bingaos-MacBook-Pro:~ bingao$
-t rsa 表示 使用rsa方式加密,其餘加密方式還有 dsa, ecdsa, ed25519dom
因爲我這裏已經生成過,因此提示 overwrite (y/n)ssh
Enter passphrase 輸入口令,最好輸一下,我這裏沒有輸入。ide
2. 生成完以後能夠在用戶目錄的 .ssh 文件夾下面看到2個文件: id_rsa, id_rsa.pub加密
bingaos-MacBook-Pro:~ bingao$ ll ~/.ssh/ total 96 -rw------- 1 bingao staff 1679 Dec 7 15:53 id_rsa -rw-r--r-- 1 bingao staff 414 Dec 7 15:53 id_rsa.pub -rw-r--r-- 1 bingao staff 22799 Dec 7 15:52 known_hosts bingaos-MacBook-Pro:~ bingao$
id_rsa 是私鑰,內容相似.net
-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEArdh52+aPhalePduymkPWVSI6odvf/DVfxkVEpm4Kl9TuB7o2 ... 640rfbph0AflsPhjIhDu/a/TdVtLYBgkR4ENTvnCNC7YLRA+GHXGsaVnooWvQQjW KDB//FarJuEf9386hW/Xp9/0+FnQxSY+WHD0ULsDdLS2PeHkd44dBw== -----END RSA PRIVATE KEY-----
id_rsa.pub 是公鑰,內容相似
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCt2Hnb5o+FqV4927KaQ9ZVIjqh2...P4TfovAyHfafboYz2JopyoUU3L12Q2xDB4wjAA7R+Eccbx/fwxwOIL0hcru/OMSmpaxMY5G2Z4+d bingao@bingaos-MacBook-Pro.local
3.將公鑰(id_rsa.pub)上傳到目標服務器(server2)上面
bingaos-MacBook-Pro:~ bingao$ scp ~/.ssh/id_rsa.pub root@192.168.0.6:~ root@192.168.0.6's password: id_rsa.pub 100% 414 73.4KB/s 00:00 bingaos-MacBook-Pro:~ bingao$
4.在目標服務器上講公鑰寫入用戶目錄下 .ssh 文件夾下面的 authorized_keys 文件內(如沒有則建立)
[root@localhost ~]# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys -bash: /root/.ssh/authorized_keys: No such file or directory [root@localhost ~]# mkdir ~/.ssh [root@localhost ~]# cat ~/id_rsa.pub >> ~/.ssh/authorized_keys [root@localhost ~]# ll ~/.ssh/ total 4 -rw-r--r--. 1 root root 414 Dec 7 16:21 authorized_keys [root@localhost ~]#
5.將 authorized_keys 文件權限設置爲 600
[root@localhost ~]# chmod 600 ~/.ssh/authorized_keys [root@localhost ~]# ll ~/.ssh/ total 4 -rw-------. 1 root root 414 Dec 7 16:21 authorized_keys [root@localhost ~]#
配置完成,接下來就能夠直接登陸了
bingaos-MacBook-Pro:~ bingao$ ssh root@192.168.0.6 Last login: Thu Dec 7 16:26:06 2017 from 192.168.1.164 [root@localhost ~]#
由於server2已經持有server1的公鑰,因此直接經過密匙認證成功。從認證原理來看 用戶將本身的公鑰儲存在遠程主機上。登陸的時候,遠程主機會向用戶發送一段隨機字符串,用戶用本身的私鑰加密後,再發回來。遠程主機用事先儲存的公鑰進行解密,若是成功,就證實用戶是可信的,直接容許登陸shell,再也不要求密碼。也就是說凡是持有這個私鑰的用戶都可以直接登陸遠程服務器。
將server1的私鑰發送到server3上面
bingaos-MacBook-Pro:~ bingao$ scp ~/.ssh/id_rsa john@192.168.0.7:~ john@192.168.0.7's password: id_rsa 100% 1679 364.5KB/s 00:00 bingaos-MacBook-Pro:~ bingao$
在server3上面使用命名 ssh -i ~/id_rsa root@192.168.0.6
[john@localhost ~]$ ssh -i ~/id_rsa root@192.168.0.6 The authenticity of host '192.168.0.6 (192.168.0.6)' can't be established. ECDSA key fingerprint is 23:c5:0e:6b:3c:26:0c:e9:31:21:12:1f:d8:b5:60:a2. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.6' (ECDSA) to the list of known hosts. Last login: Thu Dec 7 16:28:02 2017 from 192.168.1.164 [root@localhost ~]#
因爲是第一次登陸,提示是否信任遠程主機的簽名,輸入 yes,無需輸入密碼便可登陸。
第二次登陸直接進入服務器
[john@localhost ~]$ ssh -i ~/id_rsa root@192.168.0.6 Last login: Thu Dec 7 16:39:15 2017 from 10.2.0.130 [root@localhost ~]#
因此運維人員有義務保證私鑰的安全,避免在開發人員之間隨意傳播。