🔒 Cấu Hình SSH Server
📋 Giới Thiệu
SSH (Secure Shell) là giao thức bảo mật cho phép kết nối từ xa đến server. SSH sử dụng port 22/TCP.
🛠️ Cài Đặt SSH Server
# Cài đặt OpenSSH Server
sudo apt -y install openssh-server
# Kiểm tra trạng thái service
sudo systemctl status ssh
Output:
root@ivt-db-master:~# sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-10-17 14:46:06 +07; 3 weeks 5 days ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 854 (sshd)
Tasks: 1 (limit: 28801)
Memory: 7.6M
CGroup: /system.slice/ssh.service
└─854 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
🔐 Phương Thức Xác Thực
1. Xác Thực Bằng Mật Khẩu
a. Cho Phép Root Login (Không Khuyến Nghị)
# Chỉnh sửa file cấu hình SSH
sudo nano /etc/ssh/sshd_config
# Thêm hoặc sửa dòng sau
PermitRootLogin yes
# Khởi động lại SSH
sudo systemctl restart sshd
# Đặt mật khẩu cho root
sudo passwd
Hoặc thay đổi nhanh bằng lệnh:
echo 'PermitRootLogin=yes' | sudo tee -a /etc/ssh/sshd_config
b. Vô Hiệu Hóa Root Login (Khuyến Nghị)
# Trong /etc/ssh/sshd_config
PermitRootLogin no
# Khởi động lại SSH
sudo systemctl restart sshd
2. Xác Thực Bằng SSH Key
a. Tạo Cặp Khóa SSH
# Tạo key pair
ssh-keygen
# Output sẽ hiển thị:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
mẹo
- Nhấn Enter để sử dụng đường dẫn mặc định
- Có thể để trống passphrase nếu không muốn nhập mỗi lần kết nối
b. Thiết Lập Thư Mục .ssh
# Tạo thư mục .ssh
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Di chuyển và đổi tên public key
mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
c. Chuyển Private Key sang Client
# Trên máy client
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Copy private key từ server
scp user@server:/home/user/.ssh/id_rsa ~/.ssh/