跳转至

04-nat-server部署手册

简单介绍

使用iptables防火墙的转发规则使用内部服务器可以通过指网关实现出外网的功能

环境准备

要求所部署服务器可以出外网

安装软件

yum安装iptables

yum install iptables-services -y

内核转发

开启系统内核转发功能

echo net.ipv4.ip_forward = 1  >>/etc/sysctl.conf;sysctl -p

添加规则

iptables -t nat -A POSTROUTING -s 10.30.131.241 -m comment --comment "yum-server os_manage_network" -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.30.131.239 -m comment --comment "ntp-server os_manage_network" -j MASQUERADE
iptables-save >/etc/sysconfig/iptables ; iptables -t nat -S

以下规则是默认允许所有机器出外网

echo net.ipv4.ip_forward = 1  >>/etc/sysctl.conf;sysctl -p
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables-save >/etc/sysconfig/iptables ; iptables -t nat -S

固化配置

# 将规则添加到配置文件中

vi /etc/sysconfig/iptables

*nat
:PREROUTING ACCEPT [193120:14031325]
:INPUT ACCEPT [14036:712209]
:OUTPUT ACCEPT [72178:5846393]
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.30.131.241 -m comment --comment "yum-server os_manage_network" -j MASQUERADE
-A POSTROUTING -s 10.30.131.239 -m comment --comment "ntp-server os_manage_network" -j MASQUERADE
COMMIT

*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 10000 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT

启动服务

systemctl restart iptables
systemctl enable iptables

IP限速

https://www.cnblogs.com/weifeng1463/p/6805994.html

怎样使用IPTABLES限制IP上传下载速度,如何用iptables限速?我们先来看范例:

iptables限制某IP的上传速度为1000KB/秒(8Mbps,流入服务器带宽),即在此IP所在的服务器或VPS上wget的速度


iptables -A FORWARD -m limit -d 208.8.14.53 --limit 700/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP

取消iptables限速:


iptables -D FORWARD -m limit -d 208.8.14.53 --limit 700/s --limit-burst 100 -j ACCEPT 
iptables -D FORWARD -d 208.8.14.53 -j DROP



限制某IP的上传速度为2000KB/秒(16Mbps,流入服务器带宽),即在此IP所在的服务器或VPS上wget的速度


iptables -A FORWARD -m limit -d 208.8.14.53 --limit 1400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP

取消限制:


iptables -D FORWARD -m limit -d 208.8.14.53 --limit 1400/s --limit-burst 100 -j ACCEPT 
iptables -D FORWARD -d 208.8.14.53 -j DROP

如果要限制某IP下载速度(即网友通过网页下载的带宽/速度)参考

iptables -A FORWARD -s 208.8.14.36 -m limit --limit 700/s -j ACCEPT
iptables -A FORWARD -s 208.8.14.36 -j DROP

双向限制:

iptables -A FORWARD -m limit -d 208.8.14.53 --limit 2400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP
iptables -A FORWARD -m limit -s 208.8.14.53 --limit 2400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -s 208.8.14.53 -j DROP

IP流量统计

network-analysis.sh

https://blog.csdn.net/weixin_39598069/article/details/111583290

查看IP流量

iftop

iftop -i eth1

客户端配置

客户端将默认网关指向nat-server的地址即可