0

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
    1. Phishing: Password có thể bị lộ qua social engineering
    1. 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

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

sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
sudo systemctl restart sshd

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
                                        ```
                                        
                                        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
                                        
                                        ```bash
                                        eval "$(ssh-agent -s)"
                                        ssh-add ~/.ssh/id_ed25519
                                        ```
                                        
                                        3. **Fail2ban**: Block IP sau N lần login fail
                                        
                                        ```bash
                                        sudo apt install fail2ban
                                        sudo systemctl enable fail2ban
                                        ```
                                        
                                        ---
                                        
                                        Bạn đã chuyển sang SSH key chưa? Có tip bảo mật nào khác không?

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí