服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - linux共享上网设置实例详解

linux共享上网设置实例详解

2022-01-19 17:25遥远的绿洲 Linux

这篇文章主要介绍了linux共享上网设置实例详解的相关资料,需要的朋友可以参考下

linux共享上网设置

1、打开内核ip转发

?
1
2
3
vi /etc/sysctl.conf
 
net.ipv4.ip_forward = 1

执行sysctrl -p生效 

2、如果主机未启用防火墙,那么如下设置iptables

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@Web-Lnmp02 ~]# iptables -F
[root@Web-Lnmp02 ~]# iptables -P INPUT ACCEPT
[root@Web-Lnmp02 ~]# iptables -P FORWARD ACCEPT
[root@Web-Lnmp02 ~]# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
或者
           iptables -t nat -A POSTROUTING -s ip -o eth0 -j MASQUERADE 
 
//指定某ip或ip段可以转发
iptables -t nat -A POSTROUTING -s 192.168.0.170 -o enp1s0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o enp1s0 -j MASQUERADE
 
 
[root@Web-Lnmp02 ~]# /etc/init.d/iptables save
[root@Web-Lnmp02 ~]# /etc/init.d/iptables restart
说明:
 iptables -F #清除原有的filter有中的规则
 iptables -t nat -F #清除原有的nat表中的规则
 iptables -P FORWARD ACCEPT #缺省允许IP转发

 如果主机上启用了防火墙,需加上下面两句:  

Code:  

?
1
2
iptables -A FORWARD -s 192.168.122.0/24 -o eth0 -j ACCEPT
iptables -A FORWARD -d 192.168.122.0/24 -m state --state ESTABLISHED,RELATED -i eth0 -j ACCEPT

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.cnblogs.com/zjd2626/p/6796220.html

延伸 · 阅读

精彩推荐