一般情况下,debian系统安装后默认未开启iptable,作为网络服务器使用的debian系统有必要开启iptable防止一般性安全问题。
当然,你也可以使用其他防火墙软件,在此就不深入了,一般使用最多的还是iptable。
安装完系统后,首先查看下是否已经安装了iptable,使用命令:
1 | whereis iptables |
如果显示:
1 | iptables: /sbin/iptables /usr/share/iptables /usr/share/man/man8/iptables.8.gz |
则说明系统已经安装了iptable
再查看一下iptable的配置,使用命令:
1 | iptables -L |
如果显示以下信息,则说明iptable已开启,但是没有进行什么安全设置
1 2 3 4 5 6 7 8 9 | Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- anywhere anywhere tcp dpt:www Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination |
修改iptable的默认安全规则,使用以下命令:
1 | nano /etc/iptables.default.rules |
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | *filter # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT # Accepts all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allows all outbound traffic # You could modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allows HTTP and MySQLconnections from anywhere (the normal ports for websites) -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 3306 -j ACCEPT # Allows SSH connections for script kiddies # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Now you should read up on iptables rules and consider whether ssh access # for everyone is really desired. Most likely you will only allow access from certain IPs. # Allow ping -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # log iptables denied calls (access via 'dmesg' command) -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound - default deny unless explicitly allowed policy: -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT |
保存:
1 | ctrl+o |
退出:
1 | ctrl+x |
重新载入配置文件:
1 | iptables-restore < /etc/iptables.default.rules |
如果iptable不是默认启动的,还需要添加自动启动:
1 | nano /etc/network/if-pre-up.d/iptables |
添加以下内容:
1 2 | #!/bin/bash /sbin/iptables-restore </etc/iptables.default.rules |
添加执行权限:
1 | chmod +x /etc/network/if-pre-up.d/iptables |
原文链接:https://xiaohost.com/629.html,转载请注明出处。
评论2