Thursday, March 9, 2017

[Linux] Check- open- block port in Linux

1. check version of iptables 
rpm -q iptables 

2. check iptables có hoạt động không  
lsmod | grep ip_tables 
nếu iptables hoạt động thì có kết quả sau :  
ip_tables 17029 1 iptable_filter 
x_tables 17349 2 xt_tcpudp,ip_tables 

3. lệnh start, stop, restart, check status iptables 

service iptables start 
service iptables stop 
service iptables restart 
service iptables status 

4. Lệnh mở 1 port 
iptables -A INPUT -p tcp --dport 80 -j ACCEPT //port:80 for web 
iptables -A INPUT -p tcp --dport 21 -j ACCEPT //port:21 for ftp 

5. lệnh mở 1 dãy port 
iptables -A INPUT -p tcp --dport 111:333 -j ACCEPT //mở port từ 111 đến 333 

6. Kiểm tra iptables cho phép kết nối: 
iptables -L -n 

7. kiểm tra các port đang m  
netstat -tulpn | less 

8.0 check port dang mo   
[admin@PTUD-S02 ~]$ netstat -an | grep 8080 
tcp        0      0 :::8080                     :::*                        LISTEN       
[admin@PTUD-S02 ~]$  


8. check port open or close  
[root@PTUD-VM02-3 ~]# cat  /etc/services | grep 111 
oem-agent       111/tcp                # OEM Agent 
oem-agent       111/udp                # OEM Agent 
[root@PTUD-VM02-3 ~]#  

9 check port with process  
Kill process running follow port 
example : kill process follow 3872  
Step 1:  
[root@PTUD-VM01-1 em_agent]# sudo netstat -lpn |grep :3872 
tcp        0      0 :::3872                     :::*                        LISTEN      3492/java     
Step 2:  
you have PID = 3492 running on port 3872  
Kill process with command : 
[root@PTUD-VM01-1 em_agent]# kill 3492 


10. Block port 
#### Chặn Port truy cập vào server ( từ ngoài vào ) 
ex1: chặn port 80  
$/sbin/iptables -A INPUT -p tcp --destination-port 80 -j DROP 
$/sbin/service iptables save 
ex2: chặn tất cả các IP truy cập port 80, chỉ cho IP 10.30.132.4  truy cập  
$/sbin/iptables -A INPUT -p tcp -i eth1 -s ! 10.30.132.4 --dport 80 -j DROP 

#### Chặn ra ngoài theo port  
ex1: ể chặn outgoing port 25, sử dụng lệnh sau  
$/sbin/iptables -A OUTPUT -p tcp --dport 25 -j DROP 
$/sbin/service iptables save 

để chặn outgoing port 1234 đối với VIP 10.30.132.4, sử dụng lệnh sau: 
ex2:  
$/sbin/iptables -A OUTPUT -p tcp -d 10.30.164.145 --dport 1234 -j DROP 
$/sbin/service iptables save

0 nhận xét:

Post a Comment