When connecting to a Linux server, the default method is using password authentication. This has obvious drawbacks: simple passwords are easily cracked, while complex passwords are difficult to remember and type, making it highly inconvenient.
Therefore, logging in using SSH keys has become a safer, more efficient recommended solution. Through key-based authentication, not only do we avoid the trouble of remembering and typing tedious passwords, but we also greatly enhance server security, effectively defending against unauthorized access attempts.
We will use the
Ed25519key format, which has a cleaner public key and is a more modern and secure encryption algorithm than RSA.
Step 1: Generate SSH Key Pair
First, generate a pair of SSH keys on your local computer:
- Open your terminal and run the following command:
1 | ssh-keygen -t ed25519 -C "[email protected]" |
-t ed25519:Specifies the key type as Ed25519.-C "[email protected]":Adds a comment to your key, making it easy to identify later.
- Once the command runs, it will prompt you for a filename (path) to save the key. Enter a name or press Enter to accept the default.
1 | Enter file in which to save the key (/home/your_username/.ssh/id_ed25519): |
- Next, the program will prompt you to enter a passphrase. Choose whether to set a password for the private key, or press Enter to proceed without a password.
1 | Enter passphrase (empty for no passphrase): |
- Once successfully generated, you will see output similar to the following:
1 | Your identification has been saved in /home/your_username/.ssh/id_ed25519 |
At this point, two files are generated in the ~/.ssh/ directory:
id_ed25519:Private key, which must be kept secure. Never expose it to anyone.id_ed25519.pub:Public key, which can be safely shared with others or uploaded to servers.
Step 2: Upload the Public Key to the Server
Now that we have generated the key pair, the next step is to copy the public key to your Linux server.
Using the
ssh-copy-idcommand (Recommended)
This is the simplest method. It automatically copies your public key into the server's~/.ssh/authorized_keysfile and applies the correct file permissions.Simply run the following command:
1
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
The program will prompt you to enter the server's password. Once entered, the public key will be uploaded automatically.
Manual Copying
Ifssh-copy-idis unavailable or password login is already disabled, we can add it manually.First, run the following command in your local terminal to view and copy the public key content:
1
cat ~/.ssh/id_ed25519.pub
Then, log in to the server's terminal via Web Console. Create the
.sshdirectory on the server (if it doesn't exist) and set the correct permissions:1
2mkdir -p ~/.ssh
chmod 700 ~/.sshNext, open or create the
authorized_keysfile:1
vim ~/.ssh/authorized_keys
Paste the public key content you just copied from your local machine into the file, then save and exit.
1
echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... [email protected]" >> ~/.ssh/authorized_keys
Finally, set the permissions of the
authorized_keysfile to 600:1
chmod 600 ~/.ssh/authorized_keys
Step 3: Test Key-based Login
Try logging in to the server using the key from your local terminal:
1 | ssh username@server_ip |
If you changed the file name and did not use the default id_ed25519, you must specify the path to the private key manually:
1 | ssh username@server_ip -i ~/.ssh/your_private_key |
If you log in successfully, the configuration is working.
Step 4: Disable Password Login
To further improve server security, we should disable password authentication and only allow key-based logins.
Log in to your server using the SSH key.
Backup the original configuration before modifying it:
1 | sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup |
- Open the SSH configuration file with your preferred editor:
1 | sudo vim /etc/ssh/sshd_config |
Find the following line in the file and modify its value:
FindPasswordAuthentication yesand change it tono.If you need to modify the SSH port, locate
Port 22and change it to another port.Save and exit the file.
Restart the SSH service to apply the changes:
1
sudo systemctl restart sshd
If you modified the SSH port, make sure to open a new terminal window to test if you can connect successfully before disconnecting the current session.
Try logging in to the server using a password to verify if login is indeed disabled.
Step 5: Manage Connections Using the ~/.ssh/config File
If your private key file name is not the default, or if you changed the SSH port number, you will need to specify the path or port number manually every time you connect.
This is highly inconvenient. We can use the ~/.ssh/config file to manage connection details. It allows you to define aliases and specific connection options for each server.
Create or open the
~/.ssh/configfile on your local computer.Add a configuration for your server, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13# 服务器1配置
Host myserver
HostName 192.168.1.100 # 你的服务器 IP 地址或域名
User username # 你的用户名
Port 2222 # SSH 端口(如果不是 default 的 22)
IdentityFile ~/.ssh/id_ed25519 # 指定密钥文件路径
# 服务器2配置
Host webserver
HostName example.com
User webuser
Port 2222
IdentityFile ~/.ssh/webserver_keyThe
Hostfield defines the connection alias, which can be named whatever you like.After saving, you can connect to the server using the alias:
1
2
3ssh myserver
ssh webserver
Troubleshooting
Common issues are usually related to file permissions.
Common Checks
1 | # 检查.ssh目录权限 |
Correct Permission Settings
~/.ssh/directory: 700 (drwx——)~/.ssh/id_ed25519private key: 600 (-rw——-)~/.ssh/authorized_keys: 600 (-rw——-)~/.ssh/config: 600 (-rw——-)
SSH Debugging Mode
1 | # 使用详细模式连接,查看详细信息 |
Comments