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

  • Thêm IP của bạn trước khi DROP toàn bộ SSH, tránh bị lockout.
  • Đảm bảo bạn có quyền sudo và một phiên SSH đang mở để rollback nếu cần.

Sliding Sidebar

About Me

About Me

Hello, my name is Dũng (Johnny). Welcome to my blog.

As I’m a developer, I write about topics related to the field of programming, mainly from a technical point of view. On this blog you’ll find posts which encourage discussion, information about development trends, case studies, reviews, tutorials, tips on how to improve your effectiveness, and anything else that might be fascinating to people from the IT industry.
I love PHP, NodeJS, Java,... and Fullstack.

LCDUNG
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.