The steps are as follows:
1. Generate a key locally
lousama-Mac:~ apple$ ssh-keygen -t rsa
At this time, enter ls ~/.ssh to view the generated public and private keys
lousama-Mac: ~ apple $ ls ~ / .ssh id_rsa //private key id_rsa.pub //public key
2. Configure ssh config
lousama-Mac: ~ apple $ vim ~ / .ssh / config Host test //host abbreviation, after completion, you can log in directly through ssh+abbreviation HostName 192.168.xx.xx //Server address Port 22 //Server port User root //Login name PreferredAuthentications publickey //Authentication method here is public key authentication IdentityFile ~/.ssh/id_rsa //Private key file
3. The public key is placed on the server
. Next, the public key is placed on the server.
scp ~/.ssh/id_rsa.pub root@HostName:~/.ssh
Then root login to the remote server
ls ~/.ssh id_rsa.pub authorized_keys
Next, write the public key into authorized_keys, if there is no one under ~/.ssh, create a new one
touch ~/.ssh/authorized_keys //新建authorized_keys cat id_rsa.pub >> authorized_keys //write public key
Tips: If the login user is not the root user, then you need to put the public key under /home/user/.ssh, the user corresponds to the login user name
and you're done!
ssh test directly on the local terminal, try to connect?