ubuntu下iptables的用法

Ubuntu认安装是没有开启任何防火墙的,为了服务器的安全,建议大家安装启用防火墙设置,这里推荐使用iptables防火墙.如果MysqL启本地使用,可以不用打开3306端口.

# whereis iptables #查看系统是否安装防火墙可以看到:

iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz #表示已经安装iptables
apt-get install iptables #如果认没有安装,请运行此命令安装防火墙

# iptables -L #查看防火墙配置信息,显示如下:

Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

# vi /etc/iptables.rules

添加以下内容(备注:80是指web服务器端口,3306是指MysqL数据库链接端口,22是指SSH远程管理端口.)

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:syn-flood - [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT

# iptables-restore < /etc/iptables.rules #使防火墙规则生效

# vi /etc/network/if-pre-up.d/iptables #创建文件添加以下内容,使防火墙开机启动

#!/bin/bash
iptables-restore < /etc/iptables.rules

# chmod +x /etc/network/if-pre-up.d/iptables #添加执行权限

# iptables -L -n查看规则是否生效.


参考:

http://abublog.com/ubuntu_iptables.html

查看
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及该账号

参考:

http://blog.sina.com.cn/s/blog_537517170102vkpy.html

http://abublog.com/ubuntu_iptables.html

http://blog.csdn.net/just_young/article/details/47816387

http://blog.csdn.net/renwotao2009/article/details/51225359

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...