1). client ---> server
客戶端發起對服務器的連接,登錄服務器。
2). 須在客戶端生成密鑰對
注意:
公鑰加密私鑰解;私鑰加密公鑰解。
可以發布公鑰,但私鑰是不能出本機的。
把公鑰給誰就授信誰,信任誰的身份。
$ ssh-keygen -t rsa
不需要給passphrase,同時初次會在用戶家目錄下生成.ssh/,且生成id_rsa和id_rsa.pub兩個文件。
也可以使用如下命令
$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
3). 將客戶端的公鑰復制到服務器端
$ scp .ssh/id_rsa.pub user@192.168.0.11:~/.ssh/
這里192.168.0.11是服務器,user是服務器上的用戶。
4). 服務器上的操作
$ cat id_rsa.pub >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
.ssh/需要700權限
注意,文件.ssh/authorized_keys的格式如下:
-------------------------------------------------------------------------
ssh-rsa AAAAB3NzaC1...... user1@node01
ssh-rsa AAAAB3PzcIIE1...... user2@node02
ssh-rsa AAAAB3NzaC1...... user3@node03
-------------------------------------------------------------------------
一行一個公鑰密鑰串(UNIX回車換行,可以用vim的o指令添加)。
每行以ssh-rsa開頭,最后為描述信息,形如:user1@node01。描述信息是可選的。
上述3和4步可以合成如下一步完成:
$ ssh-copy-id 192.168.0.11
或者
$ ssh-copy-id -i ~/.ssh/id_rsa.pub bee@192.168.1.1
最后服務器上配置 /etc/ssh/sshd_config 並重啟服務:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
關於兼容性:
- 對於錯誤
Unable to negotiate with legacyhost: no matching key exchange method found.
Their offer: diffie-hellman-group1-sha1
使用如下命令設置
ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 user@legacyhost
或者修改配置文件 ~/.ssh/config
Host somehost.example.org (域名或IP)
KexAlgorithms +diffie-hellman-group1-sha1
- 對於錯誤
Unable to negotiate with legacyhost: no matching host key type found. Their offer: ssh-dss
使用如下命令設置
ssh -oHostKeyAlgorithms=+ssh-dss user@legacyhost
或者修改配置文件 ~/.ssh/config
Host somehost.example.org (域名或IP)
HostKeyAlgorithms +ssh-dss
- 查詢ssh支持的算法
ssh -Q cipher # List supported ciphers
ssh -Q mac # List supported MACs
ssh -Q key # List supported public key types
ssh -Q kex # List supported key exchange algorithms
- 查詢連接特定主機時所使用的配置
ssh -G user@somehost.example.com
- ssh登陸兼容性問題處理實例
$ ssh user@192.168.1.10
Unable to negotiate with 192.168.1.10 port 22: no matching key exchange method found. Their offer: diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
$ ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 user@192.168.1.10
Unable to negotiate with 192.168.1.10 port 22: no matching cipher found. Their offer: aes256-cbc,aes128-cbc,3des-cbc,des-cbc
$ ssh -Q cipher
3des-cbc
aes128-cbc
aes192-cbc
aes256-cbc
rijndael-cbc@lysator.liu.se
aes128-ctr
aes192-ctr
aes256-ctr
aes128-gcm@openssh.com
aes256-gcm@openssh.com
chacha20-poly1305@openssh.com
$ ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -c aes128-cbc user@192.168.1.10
debug:
$ ssh -vvvv -oKexAlgorithms=+diffie-hellman-group1-sha1 -c 3des-cbc user@192.168.1.10
配置文件 ~/.ssh/config
Host 192.168.1.10 example.org
user someone
KexAlgorithms diffie-hellman-group1-sha1
Cipher 3des-cbc