Mac uses ssh to connect to remote server without password login

In Windows remote server, I like to use xshell, which can save server and password information conveniently. What tool do you use for remote ssh on Mac? I personally think that the built-in "terminal" function under Mac is quite good. The disadvantage is that you have to enter the ip address + password every time. Is there any way to remember the password?

In theory, Linux is similar and should be usable.

1 Generate a pair of public key keys locally

ssh-keygen -t rsa

During the process, let you choose the save path and just copy the default value in the past. The directory I set is:/Users/itkey/.ssh/id_rsa

The password set in this step will be entered as long as you connect to ssh in the future. If it feels troublesome, just leave it empty .Insert picture description here

Insert picture description here

Set up permissions.

chmod 600 /Users/itkey/.ssh/id_rsa.pub

2 Put the public key on the server

ssh-copy-id [email protected]

userChange to the remote user name
ip.comto the address of the remote server
Insert picture description here

3 configure and save ssh server information

For the first configuration, create a new configuration file.

touch ~/.ssh/config

Edit it with your favorite file editor.

#自定义主机名称,写上好记的就行了。
Host goodjob
#SSH连接的地址,IP或者是域名
HostName ip.cn
#SSH远程登录的名称
User user
#SSH的端口默认是22
Port 22
#指向私钥的位置,这里写你自己的地址。
IdentityFile /Users/itkey/.ssh/id_rsa

Special note here under, IdentityFile written /Users/itkey/.ssh/id_rsa,
not /Users/itkey/.ssh/id_rsa.pub
before watching the tutorial was wrong, studied for a long time.

What if there are multiple ssh servers? Just copy the above content a few more times. as follows:

#自定义主机名称,写上好记的就行了。
Host goodjob
#SSH连接的地址,IP或者是域名
HostName ip.cn
#SSH远程登录的名称
User user
#SSH的端口默认是22
Port 22
#指向私钥的位置,这里写你自己的地址。
IdentityFile /Users/itkey/.ssh/id_rsa

#自定义主机名称,写上好记的就行了。
Host goodjob2
#SSH连接的地址,IP或者是域名
HostName ip2.cn
#SSH远程登录的名称
User user2
#SSH的端口默认是22
Port 22
#指向私钥的位置,这里写你自己的地址。
IdentityFile /Users/itkey/.ssh/id_rsa

The measured Host can be written in Chinese, so it is easier to remember.

Login ssh

After logging in, you only need to enter:

ssh goodjob

You can log in without entering a password, once and for all.
Insert picture description here
The passwords of many servers are super long and have no regularity, so there is no need to enter the password now! That's cool.

Server settings

This login method should be related to the ssh server. If the server does not allow this method to log in, it will fail.
For example: the way to directly connect to the bastion machine, I still need to enter the password when connecting.

The ssh server I use is generally provided by the client, and I try not to change the client's settings, so I haven't modified the server's settings.

I haven't measured the server settings. The following are copied from the Internet.
Set the information for automatic verification on the server:
open the file /etc/ssh/sshd_config

sudo gedit /etc/ssh/sshd_config

Remove the "#" comment in front of the following lines

RSAAuthentication yes 
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys

Reference documents

Thanks for the following article.
"SSH connection to the server + let the server remember the password and IP"
https://blog.csdn.net/sunyao_123/article/details/74783582

《id_rsa.pub file SSH Error: invalid format》
https://stackoverflow.com/questions/48328446/id-rsa-pub-file-ssh-error-invalid-format

《How to Fix “WARNING: UNPROTECTED PRIVATE KEY FILE!” on Mac and Linux》
https://stackabuse.com/how-to-fix-warning-unprotected-private-key-file-on-mac-and-linux/

Guess you like

Origin blog.csdn.net/lxyoucan/article/details/112527301