网络安全 linux 运维 网络 Linxu服务器配置出站规则(服务器能请求的IP) - aliyun
服务器配置出站规则(服务器能访问的IP)
停止并禁用Firewalld服务 要停止 Firewalld 服务并禁用它在系统启动时自动运行。
sudo systemctl stop firewalld
sudo systemctl disable firewalld
设置默认出站策略为DROP 使用 iptables 设置默认出站策略为 DROP,将拒绝所有出站流量:
sudo iptables -P OUTPUT DROP
允许已建立的连接 如果还需要允许已建立的连接继续工作(例如,已经建立的SSH会话),可以添加规则来允许这些连接:
sudo iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
允许到特定IP地址的出站流量
假设想要允许出站流量到IP地址192.168.1.100的TCP端口80(HTTP): sudo iptables -A OUTPUT -d 192.168.1.100 -p tcp --dport 80 -j ACCEPT
如果想要允许所有出站TCP流量到该IP地址(不限制端口): sudo iptables -A OUTPUT -d 192.168.1.100 -p tcp -j ACCEPT
如果还想要允许UDP流量: sudo iptables -A OUTPUT -d 192.168.1.100 -p udp -j ACCEPT
或者,允许所有协议到该IP地址: sudo iptables -A OUTPUT -d 192.168.1.100 -j ACCEPT
这里设置了百度的ip可以访问,因为不能直接使用域名来设置,可以通过以下方式获取到域名对应的IP: root@DG:/etc# dig +short www.baidu.com
www.a.shifen.com.
180.101.50.242
180.101.50.188
可以得到百度域名对应的IP为: 180.101.50.242 180.101.50.188
这里我们设置了允许所有出站TCP流量到该IP地址(不限制端口) sudo iptables -A OUTPUT -d 180.101.50.242 -p tcp -j ACCEPT
sudo iptables -A OUTPUT -d 180.101.50.188 -p tcp -j ACCEPT
保存iptables规则
对于Debian/Ubuntu系统,可以使用 iptables-persistent 包来保存规则: sudo apt-get install iptables-persistent
# 安装过程中,它会提示保存规则
在安装过程中,它会提示你保存当前的iptables规则,选择“是”即可。 手动保存规则 如果已经安装了 iptables-persistent,可以手动保存当前的规则: sudo netfilter-persistent save
或者: sudo service iptables-persistent save
对于CentOS/RHEL系统
安装iptables-services 对于CentOS/RHEL系统,可以安装 iptables-services 包以确保规则在重启后保留: sudo yum install iptables-services
保存规则 将当前规则保存到文件: sudo service iptables save
或者 /sbin/iptables-save > /etc/sysconfig/iptables
这将规则保存到 /etc/sysconfig/iptables 文件中 启用和启动iptables服务
确保 iptables 服务在启动时加载: sudo systemctl enable iptables
sudo systemctl start iptables
检查保存的规则
无论使用哪个系统,都可以通过以下命令来检查保存的规则: sudo iptables -L -v
注意事项 如果安装不了iptables–service就更换镜像源 备份原有的镜像源文件: 为了防止误操作,可以先备份原来的 CentOS-Base.repo 文件: cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
编辑 CentOS-Base.repo 文件: 使用文本编辑器(例如 vim 或 nano)编辑该文件: vim /etc/yum.repos.d/CentOS-Base.repo
替换内容为阿里云的镜像源: 将原有的内容替换为以下内容(不是所有,只是包含方括号的内容):
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
enabled=0
清理缓存并重新生成缓存:
替换镜像源后,清理现有的yum缓存,并重新生成缓存:
yum clean all
yum makecache
安装 iptables-services:
完成上述步骤后,再次尝试安装 iptables-services:
yum install iptables-services
查看已经设置的规则 root@DG:/home# iptables -L -v
Chain INPUT (policy ACCEPT 238K packets, 21M bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 30 packets, 1200 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 5676 packets, 341K bytes)
pkts bytes target prot opt in out source destination
525K 107M ACCEPT all -- any any anywhere anywhere ctstate RELATED,ESTABLISHED
8 480 ACCEPT tcp -- any any anywhere 180.101.50.188
6 360 ACCEPT tcp -- any any anywhere 180.101.50.242
INPUT Chain:入站规则(进入服务器的流量)的策略是 ACCEPT,意味着允许所有入站流量。目前没有看到具体的规则,是因为没有添加任何除默认策略外的规则。FORWARD Chain:转发规则(通过服务器转发的流量)的策略也是 ACCEPT,允许所有转发流量。OUTPUT Chain:出站规则(从服务器出去的流量)的策略是 DROP,意味着拒绝所有出站流量。但是,可以自己添加具体的规则,允许特定的出站流量:
第一条规则允许所有与已有连接(RELATED,ESTABLISHED)相关的出站流量。这是通过连接跟踪机制实现的,通常用于响应入站请求(如SSH会话中的返回流量)。第二条规则允许从任何地址到特定IP地址(180.101.50.188)的TCP出站流量。第三条规则允许从任何地址到另一个特定IP地址(180.101.50.242)的TCP出站流量。 测试规则 此时已经ping不通了(包括百度),如果有管理页面的话,打开浏览器访问百度,测试能否访问,结果是能正常访问的,但是访问里面的内容却是被禁止的,到这里就算是完成了。 root@DG:~# ping www.baidu.com
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
ping: sendmsg: 不允许的操作
ping: sendmsg: 不允许的操作
ping: sendmsg: 不允许的操作
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2045ms
如果只有命令行,可以用curl来测试: root@DG:~# curl -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 17 Jun 2024 09:43:19 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
结果就是200表示通了 我们使用curl来测试京东。 root@DG:~# curl -I www.jd.com 拼多多帮助力入口
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 17 Jun 2024 09:42:16 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: https://www.jd.com/
Timing-Allow-Origin: *
X-Trace: 302-1718617336640-0-0-0-0-0
Strict-Transport-Security: max-age=3600
如果没有设置白名单的结果就是302。 删除创建的规则
确认规则编号:首先,使用 iptables -L OUTPUT --line-numbers 命令查看 OUTPUT 链中规则的编号,以确定要删除的规则的具体位置。 iptables -L OUTPUT --line-numbers
这将列出 OUTPUT 链中所有规则,并显示它们的编号。例如: Chain OUTPUT (policy DROP)
num target prot opt source destination
1 ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
2 ACCEPT tcp -- anywhere 180.101.50.188 tcp dpt:80
3 ACCEPT tcp -- anywhere 180.101.50.242 tcp dpt:80
在这个例子中,第二条规则(允许发送到 180.101.50.188 的 TCP 数据包)的编号是 2。 删除规则:使用 iptables -D OUTPUT 2 命令删除编号为 2 的 OUTPUT 链规则。 iptables -D OUTPUT 2
这条命令将删除 OUTPUT 链中编号为 2 的规则,即允许发送到 180.101.50.188 的 TCP 数据包的规则。 确认删除:删除后,可以再次运行 iptables -L OUTPUT --line-numbers 命令确认规则已经被删除。 iptables -L OUTPUT --line-numbers
确保规则列表中不再包含被删除的规则。 通过这些步骤,可以成功删除 OUTPUT 链中的指定规则。记得在删除前确认规则的准确编号,以免误删其他规则。 恢复请求所有IP
清空所有规则:首先,清空所有的 iptables 规则,这将删除当前所有自定义的规则。 iptables -F
iptables -X
iptables -Z
-F:清空所有链(包括默认规则)的规则。-X:删除所有自定义的链。-Z:将所有链的计数器归零。 设置默认策略:将默认策略设为接受所有数据包。默认情况下,INPUT 和 FORWARD 链的默认策略通常是 ACCEPT,而 OUTPUT 链的默认策略通常是 DROP。为了确保接受所有数据包,可以显式设置 OUTPUT 链的默认策略为 ACCEPT。 iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
-P:设置链的默认策略。 确认设置:运行 iptables -L 命令确认当前的 iptables 设置已经恢复为接受所有数据包的状态。 iptables -L
确保所有链的默认策略都被正确设置为 ACCEPT,并且没有其他规则在影响流量的接受状态。 通过这些步骤,可以将 iptables 设置为接受所有数据包,恢复到默认的开放状态。记得在清空规则和设置默认策略之前,确保你理解现有规则的影响,避免对系统的其他功能产生不必要的影响。
测试规则是否保存成功
为了测试规则是否在重启后仍然存在,可以执行以下步骤:
重启系统: sudo reboot
验证重启后规则是否生效: 重启后,登录到系统并使用以下命令查看 iptables 规则: sudo iptables -L -v
确认规则是否依然存在。 注意事项
谨慎操作:在更改防火墙设置之前,请确保你完全理解这些更改的影响,并在非生产环境中进行测试。备份规则:在更改规则之前,最好先备份当前的iptables规则,以便在需要时可以恢复。确保您有替代访问方法:始终确保你有访问服务器的替代方法(如物理控制台、VNC、SSH密钥等),以防不小心锁定了自己。
文章来源