Chủ Nhật, 16 tháng 10, 2016

Hướng dẫn mở và khóa port trên Linux

Trong quá trình sử dụng hoặc quản lý server linux, tùy theo mục đích và yêu cầu công việc mà bạn cần thực hiện mở hoặc khóa một cổng (port) trên server để phục vụ cho tính an toàn và bảo mật.
Bài viết sẽ hướng dẫn cách cơ bản để có thể mở hoặc khóa các port.
Một số port phổ biến thường được sử dụng:
TCP port 80 – HTTP Server
TCP port 443 – HTTPS Server
TCP port 25 – Mail Server
TCP port 22 – OpenSSH (remote) secure shell server
TCP port 110 – POP3 (Post Office Protocol v3) server
TCP port 143 – Internet Message Access Protocol (IMAP) - Management of email messages
TCP / UDP port 53 – Domain Name System (DNS)
Bạn có thể sử dụng lênh nano /etc/services (vi /etc/services) để xem danh sách các port

Mở port 80 Firewall trên Centos

Enable Firewall port 80 Centos – Mở port cho phép truy cập web trừ internet
Mặc định Centos/RedHat… chặn port 80 truy cập từ internet vào server của bạn. Vì vậy bạn cần phải mở Firewall port 80 cho truy cập vào web từ bên ngoài

# /sbin/iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

# service iptables save

1.     Khóa cổng vào (Block Incoming Port)
 
/sbin/iptables -A INPUT -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP
 
### interface section use eth1 ###
/sbin/iptables -A INPUT -i eth1 -p tcp --destination-port {PORT-NUMBER-HERE} -j DROP
 
### only drop port for given IP or Subnet ##
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP
Ví dụ: Để khóa imcoming port 80 (HTTP server), ta sử dụng lệnh sau:
 
/sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP
/sbin/service iptables save
Để khóa port 80 với tất cả IP, chỉ cho duy nhất IP có địa chỉ 10.7.3.2 truy cập
 
/sbin/iptables -A INPUT -p tcp -i eth1 -s ! 10.7.3.2 --dport 80 -j DROP

2.     Khóa cổng ra (Block Outgoing Port)
 
/sbin/iptables -A OUTPUT -p tcp --dport {PORT-NUMBER-HERE} -j DROP
 
### interface section use eth1 ###
/sbin/iptables -A OUTPUT -i eth1 -p tcp --dport {PORT-NUMBER-HERE} -j DROP
 
### only drop port for given IP or Subnet ##
/sbin/iptables -A OUTPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP-ADDRESS-HERE} -j DROP
/sbin/iptables -A OUTPUT -i eth0 -p tcp --destination-port {PORT-NUMBER-HERE} -s {IP/SUBNET-HERE} -j DROP
Ví dụ: Để khóa outgoing port 25, ta sử dụng lệnh sau:
 
/sbin/iptables -A OUTPUT -p tcp --dport 25 -j DROP
/sbin/service iptables save
Để khóa outgoing port 279 đối với IP 10.7.3.5, sử dụng lệnh sau:
 
/sbin/iptables -A OUTPUT -p tcp -d 10.7.3.5 --dport 279 -j DROP
/sbin/service iptables save
Chúc các bạn thành công!

Không có nhận xét nào:

Đăng nhận xét