DEV Community

EME GUG
EME GUG

Posted on

Tại sao bạn nên dùng SSH key thay vì password

Nhiều developer vẫn đang dùng password để SSH vào server. Đây là lý do bạn nên chuyển sang SSH key ngay.

Password có vấn đề gì?

  1. Brute force: Bot quét port 22 liên tục, thử hàng nghìn password mỗi ngày
  2. Phishing: Password có thể bị lộ qua social engineering
  3. Reuse: Nhiều người dùng cùng password cho nhiều server

SSH Key hoạt động thế nào?

SSH key dùng cặp public/private key. Private key giữ ở máy bạn, public key đặt trên server.

# Tạo key pair (dùng ed25519, nhanh và an toàn hơn RSA)
ssh-keygen -t ed25519 -C "email@example.com"

# Copy public key lên server
ssh-copy-id user@server

# Từ giờ SSH không cần nhập password
ssh user@server
Enter fullscreen mode Exit fullscreen mode

Cấu hình server chặn password login

sudo nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
Enter fullscreen mode Exit fullscreen mode
sudo systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode

SSH Config cho nhiều server

File ~/.ssh/config:

Host prod
    HostName 10.0.1.50
    User deploy
    IdentityFile ~/.ssh/id_ed25519

Host staging
    HostName 10.0.1.51
    User deploy
    IdentityFile ~/.ssh/id_ed25519

Host jump
    HostName bastion.example.com
    User admin

Host internal
    HostName 192.168.1.100
    ProxyJump jump
Enter fullscreen mode Exit fullscreen mode

Giờ chỉ cần ssh prod thay vì ssh deploy@10.0.1.50 -i ~/.ssh/id_ed25519.

Tips bảo mật thêm

  1. Passphrase cho key: ssh-keygen sẽ hỏi passphrase — nên đặt, phòng trường hợp key bị copy
  2. ssh-agent: Không cần nhập passphrase mỗi lần
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode
  1. Fail2ban: Block IP sau N lần login fail
sudo apt install fail2ban
sudo systemctl enable fail2ban
Enter fullscreen mode Exit fullscreen mode

Bạn đã chuyển sang SSH key chưa? Có tip bảo mật nào khác không?

Top comments (0)