參考博文:html
iptables防火牆只容許指定ip鏈接指定端口、訪問指定網站sql
1、配置防火牆tcp
打開配置文件
post
[root@localhost ~]# vi /etc/sysconfig/iptables
正確的配置文件
網站
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT -A INPUT -j REJECT –reject-with icmp-host-prohibited -A FORWARD -j REJECT –reject-with icmp-host-prohibited COMMIT
-A INPUT -m state –state NEW -m tcp -p tcp –dport * -j ACCEPT
注意點:新開放的端口必定要在端口22後面 this
在配置pgAdmin遠程訪問虛擬機postgresql時發現出問題了,改了半天才發現是由於 防火牆端口開放文件中順序沒放對!汗!
重啓防火牆使配置生效
spa
[root@localhost ~]# service iptables restart
其它 .net
查看開放端口
rest
[root@localhost ~]# /etc/init.d/iptables status
關閉防火牆 postgresql
[root@localhost ~]# /etc/init.d/iptables stop
二、配置防火牆容許指定ip訪問端口
下面三行的意思:
先關閉全部的80端口
開啓ip段192.168.1.0/24端的80口
開啓ip段211.123.16.123/24端ip段的80口
# iptables -I INPUT -p tcp --dport 80 -j DROP # iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT # iptables -I INPUT -s 211.123.16.123/24 -p tcp --dport 80 -j ACCEPT
開放一個IP的一些端口,其它都封閉
iptables -A Filter -p tcp --dport 80 -s 192.168.100.200 -d www.pconline.com.cn -j ACCEPT iptables -A Filter -p tcp --dport 25 -s 192.168.100.200 -j ACCEPT iptables -A Filter -p tcp --dport 109 -s 192.168.100.200 -j ACCEPT iptables -A Filter -p tcp --dport 110 -s 192.168.100.200 -j ACCEPT iptables -A Filter -p tcp --dport 53 -j ACCEPT iptables -A Filter -p udp --dport 53 -j ACCEPT iptables -A Filter -j DROP
多個端口
iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT
指定時間上網
iptables -A Filter -s 10.10.10.253 -m time --timestart 6:00 --timestop 11:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP iptables -A Filter -m time --timestart 12:00 --timestop 13:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT iptables -A Filter -m time --timestart 17:30 --timestop 8:30 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
最經用的這個起做用:2017-8-16
ptables 限制ip訪問 經過iptables限制9889端口的訪問(只容許192.168.1.20一、192.168.1.20二、192.168.1.203),其餘ip都禁止訪問 iptables -I INPUT -p tcp --dport 9889 -j DROP iptables -I INPUT -s 192.168.1.201 -p tcp --dport 9889 -j ACCEPT iptables -I INPUT -s 192.168.1.202 -p tcp --dport 9889 -j ACCEPT iptables -I INPUT -s 192.168.1.203 -p tcp --dport 9889 -j ACCEPT 注意命令的順序不能反了。