1. Create a public key on the local machine
Open the Universal terminal, execute the following command, ignoring all the output, all the way to enter the cheerful.
Ssh-keygen-t rsa-c ' Your [email protected] '
-t specifies the key type, which is RSA by default and can be omitted
-C Set comment text, such as your mailbox
2. Copy the public key to the SSH server
The public key ~/id_rsa.pub file generated in the previous step is copied to the ~/.ssh/authorized_keys file under the corresponding user of the SSH server, there are many ways to describe only the three commonly used.
- [For OSX system] use the SSH-COPY-ID-FOR-OSX tool to copy the public key to the SSH server
Brew install Ssh-copy-idssh-copy-id [email protected] #将username和hostname替换为你的ssh服务器用户名和IP
- Use this method when the SSH server username The user directory is not already in the. SSH directory
Cat ~/.ssh/id_rsa.pub | SSH [email protected] "mkdir ~/.ssh; Cat >> ~/.ssh/authorized_keys "
SCP ~/.ssh/id_rsa.pub [email protected]:~/#将公钥文件复制至ssh服务器ssh [email protected] #使用用户名和密码方式登录至ssh服务器mkdir. SSH # If the. SSH directory already exists, you can omit this step cat id_rsa.pub >>. Ssh/authorized_keys #将公钥文件id_rsa. pub file contents to the Authorized_keys file
3. Quick Login
Once you have completed the above steps, you can log in to the SSH server using the following command, and mom will never have to worry that you can't remember the password.
SSH [email protected] #将username替换为你的ssh服务器用户名, hostname replaced with the IP of the server
However, every time you still need to enter SSH [email protected], still not the best solution, if you can implement one-click Login or a command to log in is the best, look at the following to see the solution
- SSH itself also provides a quick way to solve this problem by adding your SSH server information to the ~/.ssh/config configuration file.
Vim ~/.ssh/config #若没有该文件, can be created directly
Add file content format as follows:
Host alias #自定义别名 HostName HostName #替换为你的ssh服务器ip或domain Port Port #ssh服务器端口, default is 22 user user #ssh服务器用户名 identityfile ~/.ssh/id_rsa #第一个步骤生成的公钥文件对应的私钥文件
After you save the file, you can log in to the SSH server using an alias
Alias for SSH alias #alias是你在 ~/.ssh/config file configuration
If you have more than one SSH account to configure, just write it in ~/.ssh/config empty line, as follows:
Host foo HostName 192.168.2.222 Port $ User test identityfile ~/.ssh/id_ Rsahost alias HostName HostName Port Port user User
- If your local terminal is using ZSH, it's too simple, just add an alias to Zsh
echo "Alias ssh-to-username= ' ssh [email protected] '" >> ~/.ZSHRC #将username和hostname替换为你的服务器信息source ~/.ZSHRC #重新加载更改后的zshrc文件ssh-to-username #使用别名, you can log in to your SSH server with a single command
- If your local terminal is using ITERM2 also can add the profile implementation of one-click login, the specific steps here skip
Configure remote Linux server SSH key authentication automatic login under Mac