A secret-free connection commonly used in Linux. Realized by key pair. When the key file is not specified, the default id_rsa is used
- Create key pair command
ssh-keygen
-t: Specify the key type. The default is rsa. You can omit -C: set the comment text -f: specify the file name of the key file storage. The default is id_rsa
Examples:
# ssh-keygen
# Specify file name # ssh-keygen -f /root/.ssh/gitlab -C "GITLAB"
/root/.ssh/config configuration
# When there are multiple key pairs. When ssh connection, you need to specify the private key file used. When not specified, the default id_rsa file is used. So when the public key you copied is not id_rsa.pub, the SSH connection will also require a password, and no password-free login can be achieved. # Specify the key file. Another way is to create a config file in /root/.ssh on the Linux host. It is defined in the config file and will be distinguished when using ssh to connect. # 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 configure gitee and github
# 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
Specify the key file when using the ssh command.
# ssh -i /root/.ssh/nginx01 192.168.1.194
ssh-copy-id command:
-i: Copy the specified public key to the remote host. The file copied by default is /root/.ssh/id_rsa.pub # ssh-copy-id -i /root/.ssh/nginx02.pub 192.168.1.190