Configure remote Linux Server SSH key authentication for automatic logon in Mac OS X
1. Create a public key on the local machine
Open the omnipotent terminal, execute the following command, ignore all output, and press ENTER happily.
ssh-keygen -t rsa -C 'your email@domain.com'
-T specifies the key type. The default value is rsa, which can be omitted.
-C: Set the comment text, such as your mailbox.
2. Copy the public key to the ssh server
Convert the Public Key generated in the previous step ~ /Id_rsa.pub file, which is copied to ~ The/. ssh/authorized_keys file can be used in multiple ways. Here, we only introduce three common methods.
- [For osx systems] use the ssh-copy-id-for-OSX tool to copy the public key to the ssh server
Brew install ssh-copy-idssh-copy-id username @ hostname # Replace username and hostname with your ssh Server user name and IP address
- This method is used when there is no. ssh directory in the username user directory of the ssh server.
cat ~/.ssh/id_rsa.pub | ssh username@hostname "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
scp ~ / .ssh / id_rsa.pub username @ hostname: ~ / #Copy the public key file to the ssh server
ssh username @ hostname #Log in to the ssh server using username and password
mkdir .ssh #If the .ssh directory already exists, you can omit this step
cat id_rsa.pub >> .ssh / authorized_keys #Append the contents of the public key file id_rsa.pub file to the authorized_keys file
3. Quick Logon
After completing the above steps, you can use the following command to log on to the ssh server directly. Mom no longer needs to worry about your password.
Ssh username @ hostname # Replace username with your ssh Server user name, and hostname with the server ip Address
However, each time you still need to enter ssh username @ hostname, it is still not the best solution. If you can achieve one-click Login or one command login, it is best to see the solutions below.
- Ssh also provides a quick way to solve this problem ~ Add your ssh server information to the/. ssh/config configuration file.
Vim ~ /. Ssh/config # If the file does not exist, create a new one.
The file content format is as follows:
Host alias #Custom alias
HostName hostname #Replace with your ssh server IP or domain
Port port #ssh server port, default is 22
User user #ssh server username
IdentityFile ~ / .ssh / id_rsa #The private key file corresponding to the public key file generated in the first step
After saving the file and exiting, you can use the alias to log on to the ssh server.
Ssh alias # alias is where you are ~ The alias configured in the/. ssh/config file
If multiple ssh accounts need to be configured ~ /. Ssh/config empty lines and write again, as shown below:
Host foo
HostName 192.168.2.222
Port 22
User test
IdentityFile ~/.ssh/id_rsa
Host alias
HostName hostname
Port port
User user
- If zsh is used on your local terminal, it is too simple to add an alias to zsh directly.
echo "alias ssh-to-username = 'ssh username @ hostname'" >> ~ / .zshrc #Replace username and hostname with your server information
source ~ / .zshrc #Reload the changed zshrc file
ssh-to-username #Using aliases, one command can log in to your ssh server
- If your local terminal uses iterm2, you can also add a Profile for one-click logon. The steps are skipped here.
You may also like the following SSH-related articles. For details, refer:
Complete SSH service configuration and troubleshooting in Ubuntu
How to install Samba and SSH server in Ubuntu 14.04
SSH service remote access to Linux Server login is slow
How to Improve the SSH login authentication speed of Ubuntu
Enable the SSH service to allow Android phones to remotely access Ubuntu 14.04
How to add dual authentication for SSH in Linux
Configure the SFTP environment for non-SSH users in Linux
Configure and manage the SSH service on Linux
This article permanently updates the link address: