SSH To log in to the server without password, you need to generate the public and private keys locally, upload the public key to the server, and save the public key to the.ssh/authorized_keys directory at home.

SSH public and private keys are generated locally

To generate an SSH key on a local host, run the following command:

ssh-keygen -t rsa
Copy the code

After running the command, press Enter to enter the required input. In the. SSH directory, two files id_rsa and id_rsa.pub are generated. Id_rsa is the file storing the private key and id_rsa.pub is the file storing the public key.

Example Upload the local SSH public key to the server

There are many ways to upload local files to the server. The SCP command is the most convenient, and it can also copy files from the server to the local. SCP source desc, where source is the path to the file you want to upload or copy from the server to the local file, and desc is the destination path, where the file ends up. (source from where, go to where desc), for other parameters can be baidu. If the source file or destination path is on the remote host, pay attention to the path format. The following uses the remote host as a Linux host. To copy files from a remote host:

scp user@hostname:/home/userhome/a.txt ./
# user specifies the user name of the remote host and hostname specifies the name of the remote host. Hostname specifies the name of the remote host, which can be either a domain name or an IP address
Copy the code

For example, the local file is in the. SSH directory. To upload the id_rsa.pub file to the server, do the following:

SCP. / id_rsa. Pub [email protected]: / home/gitCopy the code

Run this command to upload the id_rsa.pub file to the /home/git/ directory of the server at 192.168.56.114. The user name is git. The user password is required for uploading or copying remote files.

Note: id_rsa is a private key file and must not be leaked. The public key file id_rsa.pub is uploaded to the server.

Add the public key to.ssh/authorized_keysIn the file

After the public key is uploaded to the server, you cannot log in to the server without using the public key. You need to add the public key to the. SSH /authorized_keys file:

$ ls 
id_rsa.pub
$ cat id_rsa.pub > .ssh/authorized_keys

$ cat .ssh/authorized_keys
ssh_rsa XXXXXXXXXXX [email protected]
Copy the code

Now that the password free login function is complete, you can simply SSH user@ip to log in to the server from your local host. If you cannot log in, try restarting the local SSH service.

The lazy tips

Now, although we can log in to the remote host without password, every time we log in, we have to input the user name @host IP. In line with the principle of lazy, we can map a simple name to the host to log in. There is a config file in the.ssh directory that will alias the remote host for quick logins. If you don’t have this file, create a new one.

The SSH /config file is modified as follows:

Host mac
    HostName 192.168.56.114
    User git
    #port 22
Copy the code

The “MAC” field after the “Host” field is our alias, the “HostName” field is the IP address of the Host, and the “User” field specifies the User name to log in. Since I put the authorized_keys file in the. SSH directory of the git User’s home directory, my “User” field is git

You’re done

Now all you need to do is log in to the remote host using SSH MAC from the local host.