通過配置ssh-config,簡化ssh連接

  • 很多時候我們需要通過ssh連接開發,測試,線上服務器。在windows下我們有xshell等工具,配置一下密鑰認證就可以快速連接。但是在mac或者在linux工作環境下下可能就沒有比較通用的客戶端了,這裏我們介紹一種原生的通過配置ssh_config的方式簡化連接命令。
  • 這是一段ssh_config文件的註釋,解釋這樣做的原理:
# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.
  • 步驟如下:
  • 配置你的ssh_config文件(一般在/etc/ssh目錄下)如下:
Host 98  //這裏是設置你的連接的一個別名
Hostname 111.111.111.111   //設置ip
Port 56789 //設置端口號
User wangsan   //設置登錄名
PasswordAuthentication no  //是否需要密碼認證,如果是sshkey認證,則no
ForwardAgent yes   //是否需要轉發key到代理服務器,如果需要通過跳轉機連接同學,這裏需要填寫yes
AddKeysToAgent yes //將key添加到sshkey轉發列表
IdentityFile ~/.ssh/98/id_rsa //這裏是你生成的私鑰地址,對應的公鑰也需要放入id_rsa.pub
  • 將你的公私鑰文件放入上面填寫的地址,這裏需要注意,需要將/.ssh/98目錄設置成700權限,將id_rsa文件設置成600權限,擁有者改爲自己,原因如下:
~/.ssh/identity
     ~/.ssh/id_dsa
     ~/.ssh/id_ecdsa
     ~/.ssh/id_ed25519
     ~/.ssh/id_rsa
             Contains the private key for authentication.  These files contain sensitive data and should be readable by the user but not accessible by others (read/write/execute).  ssh will simply ignore a private key file if it is accessible by others.  It is possible to specify a passphrase when generating the key which will be used to encrypt the sensitive part of this file using 3DES.
也就是私鑰文件因爲安全需要,不能被其他用戶訪問,如果訪問安全不夠,則會默認忽略該私鑰。
具體怎麼改?
修改權限:
    1.把從.ssh到獲取密鑰之間的目錄權限修改成700,上面的例子我們需要修改98目錄的權限:chmod 700 目錄
    2.把私鑰文件權限修改成600:chmod 600 私鑰文件
    3.查看當前登錄用戶 whoami
    4.修改私鑰所有者爲自己 chown 當前用戶
  • 配置好後,我們只需要鍵入:ssh 98,即可訪問配置好的遠程服務器
  • 備註
    1. 網上也有通過iterm2配置profile的方式簡化登錄方式,也不失爲一種好辦法,但是比較依賴客戶端程序
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章