Why Use SSH Key Login (Password-Free Login)¶
In Linux server management, SSH (Secure Shell) is a common tool for remote connection. While traditional password login is simple, it carries the risk of password brute-force attacks. SSH key login (password-free login) uses asymmetric encryption, offering higher security and eliminating the need to enter a password repeatedly, making it more convenient.
Prerequisites¶
- Client: Local computer (Windows/Linux/macOS). Ensure an SSH client is installed: Linux/macOS have it pre-installed; Windows needs tools like Git Bash or PuTTY.
- Server: Target Linux server (e.g., CentOS, Ubuntu). Verify the SSH service is installed (usually pre-installed; check with
ssh -V).
Step 1: Generate SSH Key Pair (Client)¶
Generate a private key (local-only, for authentication) and a public key (upload to the server) on the local client.
- Open the local terminal (Terminal for Linux/macOS, Git Bash for Windows) and run:
ssh-keygen -t rsa -b 4096
-t rsa: Specifies the key type as RSA.-b 4096: Key length (4096 bits for higher security; optional, default is 2048 bits).
-
Follow the prompts:
- Press Enter to accept the default path for the key file (~/.ssh/id_rsa, where~is the user’s home directory).
- Leave the passphrase empty (otherwise, you’ll be prompted for the passphrase every time you connect; recommended for beginners). -
After generation, two files appear in
~/.ssh:
-id_rsa: Private key (keep this secure; set permissions to600).
-id_rsa.pub: Public key (safe to share; used for uploading to the server).
Step 2: Copy Public Key to Server¶
Upload the client’s public key to the server’s ~/.ssh/authorized_keys (where the server stores allowed public keys).
Case 1: Linux/macOS Client (Recommended, Automated)¶
Run the command (replace username and server_ip):
ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_ip
-i: Specifies the public key file path (default~/.ssh/id_rsa.pub).- Enter the server password, and the public key will be automatically copied to
~/.ssh/authorized_keyswith proper permissions.
Case 2: Windows Client (Manual Copy)¶
- Open the local public key file
id_rsa.pub(via Notepad or a text editor) and copy its content. - Log in to the server (via password), then run in the terminal:
mkdir -p ~/.ssh # Create .ssh directory if it doesn’t exist
cd ~/.ssh
echo "public_key_content" >> authorized_keys # Paste the copied public key
chmod 600 authorized_keys # Set permissions for the public key file (critical!)
chmod 700 ~/.ssh # Set permissions for the .ssh directory
Step 3: Server-Side Configuration (Allow Key Login)¶
Ensure the server’s SSH service (sshd) allows public key authentication and optionally disables password login.
- Edit the SSH config file
sshd_config:
sudo vim /etc/ssh/sshd_config
-
Verify/modify these key options (ensure they are uncommented and set to
yes):
-PubkeyAuthentication yes: Enable public key authentication (default isyes).
-AuthorizedKeysFile .ssh/authorized_keys: Specify the public key file path (default is correct).
- To disable password login (only allow key login), setPasswordAuthentication no(retainyestemporarily for safety). -
Save the file and restart the SSH service:
sudo systemctl restart sshd # CentOS/RHEL
# or
sudo systemctl restart ssh # Ubuntu/Debian
Step 4: Test Client Connection (Password-Free Login)¶
After configuration, connect to the server without a password:
- Run the connection command:
ssh username@server_ip
- On the first connection, you’ll be prompted: “Are you sure you want to continue connecting (yes/no)?” Enter
yes. - You’ll log in successfully without entering a password!
Common Issues & Solutions¶
-
Permission Errors:
- Server:authorized_keysmust have600permissions (chmod 600 ~/.ssh/authorized_keys); otherwise, you’ll get “Permission denied”.
- Client:id_rsamust have600permissions (chmod 600 ~/.ssh/id_rsa); otherwise, SSH will warn “Bad permissions on private key file”. -
Still Prompted for Password:
- Check ifPubkeyAuthentication yesinsshd_config(server-side).
- Confirm the public key is correctly copied toauthorized_keyswith no extra spaces/newlines. -
Forgot Private Key Passphrase:
- If you set a passphrase during key generation, enter it when connecting. If not (left empty in Step 1), no passphrase is needed.
- To reset, delete~/.ssh/id_rsaandid_rsa.pub, then re-generate keys via Step 1.
Summary¶
SSH key login avoids password leakage risks while simplifying the login process. The core steps are: generate key pair → upload public key to server → configure server to allow key login. Pay attention to file permissions and configuration correctness. Practice on a local environment before deploying to production.
Tip: To fully disable password login, set PasswordAuthentication no in sshd_config and restart the SSH service.