当前位置:首页 > 行业动态 > 正文

如何在LINUX下架设防火墙

防火墙简介

防火墙(Firewall)是一种用于保护网络和计算机系统的硬件或软件设备,它可以监控进出网络的数据流,并根据预先设定的规则来允许或阻止特定的数据包通过,在Linux下架设防火墙,可以使用iptables这个功能强大的工具,本文将介绍如何在Linux下安装和配置iptables防火墙。

安装iptables

1、更新系统软件包列表:

sudo apt-get update

2、安装iptables:

sudo apt-get install iptables

3、安装IPTables服务:

sudo apt-get install netfilter-persistent

配置iptables防火墙

1、查看当前iptables规则:

sudo iptables -L -n -v

2、清空所有iptables规则:

sudo iptables -F

3、设置默认策略为DROP,拒绝所有未明确允许的数据包:

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

4、允许已建立的连接和相关的数据包通过:

sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

5、允许本地回环接口(lo)的数据包通过:

sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT

6、允许SSH服务通过:

sudo sshd --permanent --port=22 --pid-file=/var/run/sshd.pid --log-file=/var/log/sshd.log --reuseaddr --nofork --rsakey=/etc/ssh/ssh_host_rsa_key --rsakeyvalidation=accept-new --with-pty --logingracetime=0 --permitrootlogin --banner="SSH Login" --fromall &>/dev/null & echo $! >/var/run/sshd.pid; sudo chmod +x /var/run/sshd.pid; sudo service ssh start; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh stop; sleep 1 && sudo service ssh status; sleep 1 && sudo service ssh restart; sleep 1 && sudo service ssh status; sleep
0