ssh學習筆記

※無密碼登陸以及使用別名登陸linux

ssh 無密碼登陸要使用公鑰與私鑰。linux下能夠用用ssh-keygen生成公鑰/私鑰對,下面我以CentOS爲例。
有機器A(192.168.1.155),B(192.168.1.181)。現想A經過ssh免密碼登陸到B。算法

【總結就一句話:將登陸機器的id_rsa.pub文件中的內容複製到(追加)被登陸機器的~/.ssh/authorized_keys中便可面密碼登陸】

1. 在A機下生成公鑰/私鑰對。vim

  [chenlb@A ~]$ ssh-keygen -t rsa -P ''

  -P表示密碼,-P '' 就表示空密碼,也能夠不用-P參數,這樣就要三車回車,用-P就一次回車。
  它在/home/chenlb下生成.ssh目錄,.ssh下有id_rsa和id_rsa.pub。

2. 把A機下的id_rsa.pub中的內容複製到B機的.ssh/authorized_keys文件裏服務器

  首先將本地A的id_rsa.pub文件複製到B機中
  [chenlb@A ~]$ scp .ssh/id_rsa.pub chenlb@192.168.1.181:/home/chenlb/id_rsa.pub 
  chenlb@192.168.1.181's password:
  id_rsa.pub                                    100%  223     0.2KB/s   00:00

  因爲尚未免密碼登陸的,因此要輸入密碼。而後B機把從A機複製的id_rsa.pub添加到.ssh/authorzied_keys 文件裏ssh

  [chenlb@B ~]$ cat id_rsa.pub >> .ssh/authorized_keys
  [chenlb@B ~]$ chmod 600 .ssh/authorized_keys

  authorized_keys的權限要是600spa


3. A機登陸B機。blog

  [chenlb@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
  [chenlb@B ~]$

第一次登陸是時要你輸入yes。
如今A機能夠無密碼登陸B機了。
小結:登陸的機子可有私鑰,被登陸的機子要有登陸機子的公鑰。這個公鑰/私鑰對通常在私鑰宿主機產生。上面是用rsa算法的公鑰/私鑰對,固然也能夠用dsa(對應的文件是id_dsa,id_dsa.pub)
想讓A,B機無密碼互登陸,那B機以上面一樣的方式配置便可。
參考:SSH-KeyGen 的用法 http://blog.163.com/chen98_2006@126/blog/static/158584272007101862513886/seo


※使用別名登陸ip

能夠建立~/.ssh/config 文件併爲每臺服務器指定登陸信息和驗證方法,以下所示:ci

$ vim ~/.ssh/config
 
Host www
    HostName www.ttlsa.com
    Port 22 User root IdentityFile ~/.ssh/id_rsa IdentitiesOnly yes Host bbs HostName 115.28.45.104 User anotheruser PubkeyAuthentication no

而後直接指定別名進行登陸

選項註釋:通常有前三個就好了

  • Host便是別名
  • HostName 指定登陸的主機名或IP地址(遠程主機ip或主機名)
  • Port 指定登陸的端口號 (22)
  • User 登陸用戶名,這個是你以遠程主機上的哪一個用戶名登陸遠程主機
  • IdentityFile 登陸的密鑰文件
  • IdentitiesOnly 只接受SSH key 登陸
  • PubkeyAuthentication

Bad owner or permissions on /root/.ssh/config

以root用戶ssh 遠程登陸時,提示了上面那個錯誤。緣由是權限問題。修改下/root/.ssh文件夾下的文件的權限便可,可是注意必須是700權限才行,其餘的600或777都不行。chmod 700 /root/.ssh/*

相關文章
相關標籤/搜索