一般系統驗證用戶身份,我們都是用的密碼的方式。
其實還可以通過密鑰的方式,進行用戶身份驗證。
參考這裏:https://blog.csdn.net/yezhouyong/article/details/78315881
ssh-keygen
命令專門是用來生成密鑰的。該命令有很多選項,這裏列出了最基本的四個:
-
-t
用來指定密鑰類型(dsa | ecdsa | ed25519 | rsa | rsa1
); -
-P
用來指定密語 -
-f
用來指定生成的密鑰文件名 -
-C
用來添加註釋
ssh-keygen -t rsa -P 123456 -f host -C 'my host key'
意思就是新建了密語爲123456
註釋爲my host key
文件名爲host
的密鑰。此命令會生成host
和host.pub
兩個文件,前者爲私鑰文件
,後者爲公鑰文件
。如果你想免密登錄的話,請將密語設置爲空。
比如我在 69 服務器上 想遠程連接 76服務器,則需要在69上執行生成祕鑰對的操作。
把生成的密鑰對中的公鑰,複製到76上,把公鑰拼接到76的authorized_keys
注意:如果沒有authorized_keys,則自己使用touch命令建一個。
#在本機上執行此命令,上傳公鑰 scp -P your_port host.pub user@hostname:/tmp #在服務器上執行此命令,追加到authorized_keys cd /tmp && cat host.pub >> ~/.ssh/authorized_keys #更改權限 chmod 600 ~/.ssh/authorized_keys
然後去76上,打開/etc/ssh/sshd_config
設置成:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
返回到69,
etl@lenovo-FFFFFFFFFF:~/.ssh$ ls
known_hosts
etl@lenovo-FFFFFFFFFF:~/.ssh$ cd ..
etl@lenovo-FFFFFFFFFF:~$ ssh -i ./76server [email protected]
Enter passphrase for key './76server':
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.2.0-42-generic x86_64)
* Documentation: https://help.ubuntu.com/
369 packages can be updated.
303 updates are security updates.
New release '16.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
WARNING: Security updates for your current Hardware Enablement Stack
ended on 2016-08-04:
* http://wiki.ubuntu.com/1404_HWE_EOL
There is a graphics stack installed on this system. An upgrade to a
configuration supported for the full lifetime of the LTS will become
available on 2016-07-21 and can be installed by running 'update-manager'
in the Dash.
Last login: Thu Oct 11 13:49:57 2018 from 192.168.8.148
lenovo@lenovo-70FR0028CN:~$
如果想登錄時必須同時有密碼和私鑰,則按照如下操作:
讓服務器更安全,開啓密碼和證書雙重驗證,先修改SSH配置文件:
vim /etc/ssh/sshd_config
PasswordAuthentication 改爲yes
然後再加一條:AuthenticationMethods publickey,password
重啓SSH服務:/etc/init.d/sshd restart 或者 service ssh restart