CentOS禁止指定网卡访问

CentOS禁止指定网卡访问的配置方法

方法一:通过iptables限制网卡访问

使用iptables防火墙规则可精准控制网卡流量。假设需禁用网卡eth0,操作步骤如下:

# 禁止eth0网卡的所有入站流量
iptables -A INPUT -i eth0 -j DROP

# 禁止eth0网卡的所有出站流量
iptables -A OUTPUT -o eth0 -j DROP

# 保存规则使其永久生效
service iptables save
systemctl restart iptables

方法二:禁用网卡服务

通过systemctl停止并禁用网卡服务实现访问限制:

# 停止网卡服务
systemctl stop network-interface@eth0.service

# 禁止开机自启
systemctl disable network-interface@eth0.service

操作验证与恢复

执行以下命令验证配置是否生效:

# 查看iptables规则
iptables -L -n -v

# 测试网络连通性(需切换至其他网卡)
curl --interface eth1 http://example.com

恢复网卡访问权限:

# 删除iptables规则
iptables -D INPUT -i eth0 -j DROP
iptables -D OUTPUT -o eth0 -j DROP

# 重启网卡服务
systemctl start network-interface@eth0.service

注意事项

  • 操作前建议备份/etc/sysconfig/network-scripts/ifcfg-eth0配置文件
  • 若使用NetworkManager,可通过nmcli connection down eth0临时禁用连接
  • CentOS 7及以上版本需确认防火墙服务类型(firewalld/iptables)

寰宇互联服务器4核4G云服务器1元/月,网络稳定、抗DDos、国际BGP、性能强劲,十年服务经验QQ:97295700 微信:huanidc

阅读剩余
THE END