天天看點

ssh相關知識

  • ssh-agent
    • 使用場景
      • 管理秘鑰,可以對不同伺服器指定不同的秘鑰
      • 省略輸入秘鑰的密碼
    • 啟動方式
      • ssh-agent $SHELL

        ssh-agent 隻在目前終端中起作用​

      • eval `ssh-agent`

        作為獨立程序啟動​

    • 關閉方式
      • 如果是目前終端啟動的,可以這樣關閉 ssh-agent -k
      • 可以直接使用kill指令進行關閉
  • ssh-add
    • 作用:向ssh-agent中添加秘鑰
    • 指令:
      • 添加私鑰:ssh-add ~/.ssh/id_rsa_new
      • 檢視已經添加的私鑰: ssh-add -l
      • 檢視私鑰對應的公鑰: ssh-add -L
      • 删除私鑰: ssh-add -d ~/.ssh/id_rsa_new
      • 清空所有私鑰: ssh-add -D
      • 鎖定目前的私鑰: ssh-add -x
  • ssh-copy-id
    • 作用:将公鑰拷貝到伺服器
    • 指令:ssh-copy-id -i ~/.ssh/id_rsa_new [email protected]
  • ssh
    • 作用:登陸伺服器
    • 要點
      • 當不指定秘鑰檔案的時候,預設會使用 ~/.ssh/id_rsa 進行驗證
      • 可以使用 -i 參數指定秘鑰檔案。 ssh -i ~/.ssh/id_rsa_new [email protected]
  • ssh 配置檔案config
    • 作用:對不同的位址指定不同的私鑰
    • 常用字段
      • Host alias
      • HostName hostname
      • User root
      • Port 22
      • IdentityFile ~/.ssh/identity
      • AddKeysToAgent 是否自動将 key 加入到 ssh-agent,值可以為 no(default)/confirm/ask/yes。
      • LocalForward 5433 localhost:5432 将遠端的5432端口映射到本地的5433
      • ProxyCommand 代理指令

        Host gateway

        HostName proxy.example.com

        User root

        Host db

        HostName db.internal.example.com # 目标伺服器位址

        User root # 使用者名

        # IdentityFile ~/.ssh/id_ecdsa # 認證檔案

        ProxyCommand ssh gateway netcat -q 600 %h %p # 代理指令

    • 詳細字段說明參見 man ssh_config
    • 檔案位置
      • $ ~/.ssh/config # 使用者配置檔案
      • /etc/ssh/ssh_config # 系統配置檔案
  • 參考文檔
    • 了解ssh代理:ssh-agent http://www.zsythink.net/archives/2407
    • SSH Config 那些你所知道和不知道的事  https://deepzz.com/post/how-to-setup-ssh-config.html

繼續閱讀