Site icon LCDUNG

iptables

Tuyệt vời, để giới hạn SSH chỉ cho các IP trong whitelist, bạn có thể dùng iptables như sau:


🧱 1. Mặc định chặn SSH

Chặn tất cả kết nối đến port SSH (mặc định là 22):

sudo iptables -A INPUT -p tcp --dport 22 -j DROP

2. Cho phép IP cụ thể truy cập SSH

Thay YOUR_IP_ADDRESS bằng địa chỉ IP bạn muốn cho phép:

sudo iptables -I INPUT -p tcp -s YOUR_IP_ADDRESS --dport 22 -j ACCEPT

Bạn có thể lặp lại lệnh này cho từng IP trong whitelist.


💾 3. Kiểm tra cấu hình hiện tại

sudo iptables -L -n --line-numbers

💡 4. Lưu cấu hình iptables vĩnh viễn

Ubuntu không tự lưu iptables khi reboot. Để lưu lại:

Cách 1: Dùng iptables-persistent (khuyên dùng)

sudo apt install iptables-persistent
sudo netfilter-persistent save

Cách 2: Dùng iptables-save

sudo sh -c "iptables-save > /etc/iptables.rules"

Rồi thêm vào /etc/rc.local hoặc systemd script để load lại sau reboot:

iptables-restore < /etc/iptables.rules

⚠️ Khuyến cáo

Exit mobile version