Ubuntu下使用UFW,以及CentOS7的默认防火墙firewalld

  UFW是一个简化版的iptables,基于iptables,配置比iptables简单

  默认UFW是关闭状态,即Ubuntu默认打开所有端口,比较危险。

  检测状态

  ufw status

  设置默认状态,默认禁止所有连接(所有入站连接)

  ufw default deny

  启用某个端口

  ufw allow 22

  删除某个规则

  ufw delete allow 22

  只打开使用tcp/ip协议的22端口:

  ufw allow 22/tcp

  打开来自192.168.0.1的tcp请求的80端口:

  sudo ufw allow proto tcp 192.168.0.1 port 80 to 192.168.0.2 port 80

  更详细的使用说明见:

  http://wiki.ubuntu.org.cn/Ufw%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

  其实调用的还是iptables,因为使用后生成了很多iptables的规则。

 

 

# 清空所有防火墙规则
iptables --flush
iptables --delete-chain
 
# 接受所有到达 22 端口的 TCP 请求
# 假如你改了 SSH 的端口,记得修改这里的端口号。
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
# 接受所有到达 8888 端口的 TCP 请求
# 记得将 8888 改为你为 Shadowsocks server 设定的端口
iptables -A INPUT -p tcp --dport 8888 -j ACCEPT
 
# 接受所有由 VPS 发出的请求收到的回应
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许 ping
iptables -A INPUT -p icmp -j ACCEPT
 
# 不接受其余一切网络请求
iptables -A INPUT -j DROP
然后我发现 /etc/init.d 文件夹下不存在 iptables 服务文件,看来是要自行添加了,真麻烦。在 Google 了一下之后,没有找到别人提供的服务文件,却找到了利用开机网络前的配置 hook 运行自定义脚本来实现的方法,听说是因为 Debian 已经不再建议使用 /etc/init.d/iptables 服务了。
 
那么便添加 hook 吧,创建脚本 /etc/network/if-pre-up.d/iptables 并为其添加可执行属性,文件内容如下:
 
#!/usr/bin/env bash
 
# 从 iptables.conf 中读取防火墙配置
/sbin/iptables-restore < /etc/iptables.conf
接着,执行 bash iptables.sh 运行防火墙,然后执行 iptables-save > /etc/iptables.conf 生成配置文件。
 
最后,别忘了限制配置文件的读写权限,chmod 600 /etc/iptables.conf 足矣。搞定。
posted @ 2015-09-24 21:33  光闪  阅读(4739)  评论(1编辑  收藏  举报