SSH Key là phương thức xác thực an toàn hơn password. Thay vì nhập mật khẩu mỗi lần kết nối, bạn dùng cặp khóa mật mã: public key đặt trên server, private key giữ trên máy mình. Bài viết này hướng dẫn chi tiết cách tạo, cài đặt và sử dụng SSH Key.

SSH Key là gì?

SSH Key là cặp khóa mật mã gồm:

  • Private key (khóa bí mật): Lưu trên máy tính của bạn. KHÔNG chia sẻ cho ai.
  • Public key (khóa công khai): Đặt trên server. Có thể chia sẻ thoải mái.

Khi kết nối, server dùng public key để mã hóa thử thách. Máy bạn dùng private key để giải mã. Nếu khớp → đăng nhập thành công.

Tại sao nên dùng SSH Key?

  • Bảo mật hơn password: Không thể brute-force được (vì không có password để đoán).
  • Tiện lợi: Không cần nhập password mỗi lần kết nối.
  • Dùng chung cho nhiều server: Một key đăng nhập nhiều máy chủ.
  • Ứng dụng rộng: Git, CI/CD, automated scripts.

Kết hợp với SSH Hardening để bảo mật server tối đa.

1. Tạo SSH Key

Dùng lệnh ssh-keygen để tạo cặp khóa:

# Tạo key RSA 4096 bit
ssh-keygen -t rsa -b 4096 -C "your-email@domain.com"

# Hoặc dùng Ed25519 (nhanh hơn, khuyến nghị)
ssh-keygen -t ed25519 -C "your-email@domain.com"

Lệnh sẽ hỏi:

  • Location: Nhấn Enter để dùng mặc định ~/.ssh/id_ed25519
  • Passphrase: Đặt mật khẩu bảo vệ private key (nên đặt)

Kết quả:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub

The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The key's randomart image is:
+---[ED25519 256]----+
|     .o.+.          |
|     .oB++           |
+----[SHA256]-----+

2. Copy Public Key lên Server

Cách 1: ssh-copy-id (đơn giản nhất)

# Copy key lên server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip

# Nếu dùng port khác 22
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@server-ip

Cách 2: Dùng cat và SSH

# Chuyển public key sang server bằng pipe
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Cách 3: Thêm thủ công

Đăng nhập vào server bằng password, sau đó:

# Tạo thư mục .ssh
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# Tạo file authorized_keys
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# Copy nội dung public key vào file
vi ~/.ssh/authorized_keys
# Paste nội dung file id_ed25519.pub vào

3. Kết nối bằng SSH Key

# Kết nối với key mặc định
ssh user@server-ip

# Kết nối với key cụ thể
ssh -i ~/.ssh/mykey user@server-ip

# Kết nối với port khác
ssh -i ~/.ssh/mykey -p 2222 user@server-ip

4. Cấu hình SSH Config

Thay vì gõ dài dòng mỗi lần, tạo alias trong ~/.ssh/config:

# Tạo file config
nano ~/.ssh/config

# Thêm cấu hình
Host myserver
    HostName 123.123.123.123
    User admin
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Host production
    HostName 45.67.89.10
    User deploy
    Port 22
    IdentityFile ~/.ssh/production_key

Giờ chỉ cần:

ssh myserver
ssh production

5. SSH Key cho Git

GitHub

# Copy public key
cat ~/.ssh/id_ed25519.pub

# Paste vào GitHub: Settings → SSH and GPG keys → New SSH key

Clone repository:

git clone git@github.com:username/repo.git

GitLab

# Copy public key
cat ~/.ssh/id_ed25519.pub

# Paste vào GitLab: Preferences → SSH Keys

6. Quản lý nhiều SSH Keys

Nếu dùng nhiều keys cho các server khác nhau:

# ~/.ssh/config
Host work-server
    HostName work.example.com
    User admin
    IdentityFile ~/.ssh/work_key

Host personal-server
    HostName personal.example.com
    User root
    IdentityFile ~/.ssh/personal_key

Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_key

7. Đổi Passphrase

# Đổi passphrase cho key
ssh-keygen -p -f ~/.ssh/id_ed25519

# Output:
# Enter old passphrase:
# Enter new passphrase:
# Enter same passphrase again:

8. Xóa SSH Key

# Xóa cặp key
rm ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub

# Xóa entry trong known_hosts (nếu server đổi key)
ssh-keygen -R server-ip

9. Troubleshooting

Permission denied

# Kiểm tra quyền file
ls -la ~/.ssh/

# Phải có:
# - ~/.ssh/ : drwx------ (700)
# - ~/.ssh/* : -rw------- (600)

# Sửa quyền
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Too many authentication failures

# Thử với PreferredAuthentications
ssh -o PreferredAuthentications=publickey -i ~/.ssh/mykey user@server

Server refused our key

  • Kiểm tra public key đã thêm đúng vào ~/.ssh/authorized_keys chưa.
  • Đảm bảo file ~/.ssh/authorized_keys có quyền 600.
  • Kiểm tra sshd_config có cho phép key auth: PubkeyAuthentication yes

Best Practices

  • [ ] Đặt passphrase: Bảo vệ private key nếu bị đánh cắp.
  • [ ] Dùng Ed25519: An toàn hơn RSA, key nhỏ hơn.
  • [ ] Mỗi server một key: Dễ quản lý, revoke khi cần.
  • [ ] Sao lưu private key: Lưu ở nơi an toàn.
  • [ ] Tắt password auth: Sau khi setup key, tắt password trong sshd_config.

Kết Luận

SSH Key là cách an toàn và tiện lợi để đăng nhập server. Thay vì nhớ password, bạn dùng cặp khóa mật mã.

  • Tạo key bằng ssh-keygen
  • Copy public key lên server bằng ssh-copy-id
  • Cấu hình ~/.ssh/config để đỡ gõ lệnh
  • Tắt password auth sau khi setup key

Kết hợp với SSH HardeningUFW Firewall để bảo mật server toàn diện.

Chào các bạn mình là Quốc Hùng , mình sinh ra thuộc cung song tử ,song tử luôn khẳng định chính mình ,luôn luôn phấn đấu vượt lên phía trước ,mình sinh ra và lớn lên tại vùng đất võ cổ truyền ,đam mê của mình là coder ,ngày đi học tối về viết blog ...