ssh 建立多個祕鑰對,鏈接不一樣的服務器

Linux 中經常使用的免密鏈接。經過祕鑰對實現。當不指定密鑰文件時,使用的是默認的id_rsalinux

  • 建立祕鑰對命令

ssh-keygennginx

-t : 指定祕鑰類型。默認是rsa. 能夠省略
-C : 設置註釋文字
-f : 指定祕鑰文件存儲的文件名。 默認是id_rsa

實例:git

# ssh-keygen

image.png

# 指定文件名
# ssh-keygen -f /root/.ssh/gitlab -C "GITLAB"

image.png

  • /root/.ssh/config 配置github

# 多個密鑰對時。ssh鏈接時,須要指定使用的私鑰文件. 當不指定時,使用的時默認的id_rsa文件。因此當你複製的公鑰不是id_rsa.pub時,ssh鏈接也會須要輸入密碼,實現不了免密登陸
# 指定密鑰文件另外一種方法是在linux 主機的 /root/.ssh 建立config文件。在config文件定義,在使用ssh鏈接時,會進行區分。
# touch /root/.ssh/config
# cat /root/.ssh/config
Host 192.168.1.194
  Hostname 192.168.1.194
  #PreferredAuthentications publickey
  IdentityFile /root/.ssh/nginx01

Host 192.168.1.191
  Hostname 192.168.1.191
  #PreferredAuthentications publickey
  IdentityFile /root/.ssh/nginx02
  
Host 192.168.1.190
  Hostname 192.168.1.190
  #PreferredAuthentications publickey
  # User root
  IdentityFile /root/.ssh/gitlab
  • congfig 配置gitee 和  githubbash

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_rsa

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa

使用ssh 命令時指定祕鑰文件。ssh

# ssh -i /root/.ssh/nginx01  192.168.1.194

ssh-copy-id 命令:ide

-i : 複製指定的公鑰到遠程主機。默認複製的文件是 /root/.ssh/id_rsa.pub
# ssh-copy-id  -i /root/.ssh/nginx02.pub 192.168.1.190
相關文章
相關標籤/搜索