ssh 免密碼登入
ssh 無密碼登入要使用公鑰與私鑰。linux下可以用用ssh-keygen生成公鑰/私鑰對,下面我以CentOS為例。
有機器A(192.168.1.155),B(192.168.1.181)。現想A通過ssh免密碼登入到B。
1.在A機下生成公鑰/私鑰對。
[root#A ~]$ ssh-keygen -t rsa -P ''
-P表示密碼,-P '' 就表示空密碼,也可以不用-P參數,這樣就要三車回車,用-P就一次回車。
它在/root下.ssh下生成有id_rsa和id_rsa.pub。
2.把A機下的id_rsa.pub複制到B機下,在B機的.ssh/authorized_keys檔案裡,我用scp複制。
[root#A ~]$ scp .ssh/id_rsa.pub [email protected]:/root
[email protected]'s password:
id_rsa.pub 100% 223 0.2KB/s 00:00
由于還沒有免密碼登入的,是以要輸入密碼。
3.B機把從A機複制的id_rsa.pub添加到.ssh/authorzied_keys檔案裡。
[root#B ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[root#B ~]$ chmod 600 .ssh/authorized_keys
authorized_keys的權限要是600。
4.A機登入B機。
[root#A ~]$ ssh 192.168.1.181
The authenticity of host '192.168.1.181 (192.168.1.181)' can't be established.
RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.
Last login: Thu Jul 3 09:53:18 2008 from chenlb
[root#B ~]$
第一次登入是時要你輸入yes。
現在A機可以無密碼登入B機了。