近期碰到須要使用密鑰登陸的狀況,不一樣的鏈接終端的狀況不太同樣,因此對此總結一番html
1、 密鑰對生成
注意:本地主機和目的主機需 ping 通
1)確認服務器是否安裝有 ssh 以及openssh服務端linux
[zyy@kube-master ~]$ rpm -qa |grep openssh openssh-6.6.1p1-25.el7_2.x86_64 openssh-clients-6.6.1p1-25.el7_2.x86_64 openssh-server-6.6.1p1-25.el7_2.x86_64 [zyy@kube-master ~]$ [zyy@kube-master ~]$ [zyy@kube-master ~]$ systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2018-05-21 17:40:41 CST; 22h ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 10690 (sshd) CGroup: /system.slice/sshd.service └─10690 /usr/sbin/sshd -D
2)在目的主機切換到須要使用密鑰登陸的帳戶,生成密鑰對shell
[zyy@kube-master ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/zyy/.ssh/id_rsa): Created directory '/home/zyy/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/zyy/.ssh/id_rsa. Your public key has been saved in /home/zyy/.ssh/id_rsa.pub. The key fingerprint is: 1d:46:32:22:37:b3:ff:f1:14:d9:6f:3c:19:cb:4a:11 zyy@kube-master The key's randomart image is: +--[ RSA 2048]----+ | . = o . E | | o = + o. | | . o o... | | . o . .oo+| | S o .. =+| | . +. ...| | . .. | | | | | +-----------------+
3)查看生成的密鑰、公鑰對,權限均設置爲 600vim
[zyy@kube-master .ssh]$ ll total 8 -rw------- 1 zyy zyy 1679 May 22 16:32 id_rsa # 私鑰 -rw-r--r-- 1 zyy zyy 397 May 22 16:32 id_rsa.pub # 公鑰
4)根據須要修改登陸方式服務器
修改 sshd 鏈接配置文件 vim /etc/ssh/sshd_config PasswordAuthentication no # 不容許密碼驗證登陸 PubkeyAuthentication yes # 容許公鑰驗證登陸 AuthorizedKeysFile .ssh/id_rsa.pub # 指定公鑰文件路徑
5)將私鑰和公鑰導出來保存備用dom
[zyy@kube-master .ssh]$ sz id_rsa id_rsa.pub
2、使用 Xshell 鏈接ssh
打開鏈接終端,設置好主機名,而後設置好鏈接信息ide
在鏈接過程當中可能會出現以下錯誤信息.net
這是由於在服務端沒有加入認證3d
將公鑰追加到一個文件,命名爲authorized_keys [zyy@kube-master .ssh]$ cat id_rsa.pub >authorized_keys 將文件權限設置爲 600 [zyy@kube-master .ssh]$ chmod 600 authorized_keys [zyy@kube-master .ssh]$ [zyy@kube-master .ssh]$ ll total 12 -rw------- 1 zyy zyy 397 May 22 16:47 authorized_keys -rw------- 1 zyy zyy 1679 May 22 16:32 id_rsa -rw------- 1 zyy zyy 397 May 22 16:32 id_rsa.pub
而後咱們從新進行鏈接就OK了
3、使用 SecureCRT 鏈接
打開CRT,配置鏈接信息,將密鑰認證設置爲首選登陸方式
而後導入公鑰
點擊確認,成功鏈接
參考資料