温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iptable详解

发布时间:2020-07-17 18:17:10 来源:网络 阅读:1363 作者:运维小当家 栏目:数据库

查看iptables状态-重启


iptables 所在目录 /etc/sysconfig/iptables

service iptables status 查看iptables状态
service iptables restart iptables服务重启
service iptables stop iptables服务禁用


启动iptables  

modprobe ip_tables  

关闭iptables(关闭命令要比启动复杂)  

iptalbes -F  

iptables -X  

iptables -Z  

iptables -P INPUT ACCEPT  

iptables -P OUTPUT ACCEPT  

iptables -P FORWARD ACCEPT  

modprobe -r ip_tables  

依次执行以上命令即可关闭iptables,否则在执行modproble -r ip_tables时将会提示  FATAL: Module ip_tables is in use.
 
 
 
 iptables -L -n
 
 iptables -F 清除预设表filter中的所有规则链的规则
 iptables -X 清除预设表filter中使用者自定链中的规则
 
 
 iptables -L -n
 
 #抛弃所有不符合三种链规则的数据包
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP


#设置:本地进程 lo 的 INPUT 和 OUTPUT 链接 ; eth0的INPUT链
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -jACCEPT
iptables -A INPUT -i eth0 -m state --state NEW,INVALID -j LOG
iptables -A OUTPUT -o lo -j ACCEPT

#开放22端口ssh
 iptables -A INPUT -p tcp -i eth0 --dport ssh -j ACCEPT

#开放80端口web
iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT

#开放21、20端口ftp
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
 
#开放其他一些端 口  
iptables -A INPUT -p tcp --dport 1935 -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

#同上,开放需要端口的出口
iptables -A OUTPUT -p tcp --sport 1935 -j ACCEPT
。。。。
。。。。
。。。。

# 如使用vsftpd 使用了pasv 方式,如 pasv_min_port=6000 mx=7000 pasv_enable=YES之类
 iptables -A INPUT -p tcp --dport 6000:7000 -j ACCEPT
 iptables -A OUTPUT -p TCP --sport 6000:7000 -j ACCEPT
# 2个都要设,只设第一个不能下载,只设第二个不能上传



#限制 .37 可以连接哪些端 口,
 iptables -A INPUT -s 192.168.0.37 -p tcp --dport 21 -j ACCEPT
 iptables -A INPUT -s 192.168.0.37 -p tcp --dport 20 -j ACCEPT
 
 #注:因上方设置的iptables -A INPUT -p tcp --dport 20 -j ACCEPT  & iptables -A INPUT -p tcp --dport 21 -j ACCEPT
 #允许开放20.21到所有用户
 #所以要删除掉该规则
 iptables -D INPUT -p tcp --dport 20 -j ACCEPT
 iptables -D INPUT -p tcp --dport 21 -j ACCEPT
 
 
#允许loopback!(不然会导致DNS无法正常关闭等问题)

IPTABLES -A INPUT -i lo -p all -j ACCEPT (如果是INPUT DROP)

IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)
 
 
 
 #将以上规则保存到 文件 sudo 是不行的,需要root权限(没有设过的话, sudo passwd root 输入新的root密码即可。 然后su )
 iptables-save > /etc/iptables.up.rules
 
 修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的)

auto eth0
iface eth0 inet dhcp
pre-up iptables-restore <  /etc/iptables.up.rules
post-down iptables-save >/etc/iptables.up.rules #关机时,把当前iptables 储存


附 vsftpd.conf 主要项
listen=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
pasv_min_port=6000
pasv_max_port=7000
pasv_enable=YES
ls_recurse_enable=YES
local_umask=022
file_open_mode=0755

这个FTP只供于管理员进行管理及上传工作,因此本地账号权限较大,要注意。
在/etc/vsftpd.chroot_list 只放root及该账号




代码:

#删除原来 iptables 里面已经有的规则
iptables -F
iptables -X

#抛弃所有不符合三种链规则的数据包
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

#设置:本地进程 lo 的 INPUT 和 OUTPUT 链接 ; eth2的INPUT链
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i eth2 -m state --state ESTABLISHED,RELATED -jACCEPT
iptables -A INPUT -i eth2 -m state --state NEW,INVALID -j LOG
iptables -A OUTPUT -o lo -j ACCEPT

#对其他主要允许的端口的 OUTPUT设置:
# DNS
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 53 -jACCEPT
iptables -A OUTPUT -o eth2 -p UDP --sport 1024:65535 --dport 53 -jACCEPT

#HTTP
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 80 -jACCEPT

#HTTPS
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 443 -jACCEPT

#Email 接受 和发送
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 110 -jACCEPT
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 25 -jACCEPT

# FTP 数据和控制
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 20 -jACCEPT
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 21 -jACCEPT

#DHCP
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 68 -jACCEPT
iptables -A OUTPUT -o eth2 -p UDP --sport 1024:65535 --dport 68 -jACCEPT

#POP3S Email安全接收
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 995 -jACCEPT

#时间同步服务器 NTP
iptables -A OUTPUT -o eth2 -p TCP --sport 1024:65535 --dport 123 -jACCEPT

#拒绝 eth2 其他剩下的
iptables -A OUTPUT -o eth2 --match state --state NEW,INVALID -jLOG



最后是有关于iptables存储的命令:

代码:

iptables-save >/etc/iptables.up.rule # 存在你想存的地方


代码:

iptables-restore </etc/iptables.up.rules #调用



因为iptables 在每次机器重新启动以后,需要再次输入或者调用,为了方便操作,使用

代码:

sudo gedit /etc/network/interfaces



代码:

auto ath0
    iface ath0 inet dhcp

后面加上

代码:

pre-up iptables-restore </etc/iptables.up.rules #启动自动调用已存储的iptables


代码:

post-down iptables-save >/etc/iptables.up.rule #关机时,把当前iptables 储存


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI