Internet sharing (简体中文)

From ArchWiki
Jump to navigation Jump to search
翻译状态:本文是 Internet sharing翻译。上次翻译日期:2017-06-28。如果英文版本有所更改,则您可以帮助同步翻译。


附注: 初创页面,尚未翻译(在 Talk:Internet sharing (简体中文)# 中讨论)




  • 如果想给若干台机器分享网络连接,switch可以提供数据连接。
  • 一个无线设备同样可以给若干台机器分享网络访问,参见Software access point
  • 如果只需分享网络给一台机器,一根交叉网线就可以了。 如果两台电脑的网卡支持MDI-X,交叉网线也不是必须的,一根普通直连网线也可以。 运行 ethtool interface | grep MDI 确认网卡是否支持MDI-X。


这个章节假定,连接到客户机的网络设备被命名为 net0而连接到互联网的网络设备被命名为internet0

提示: 为了此事,你可以使用Udev#Setting static device names所述的方法重命名你的设备。




# ip link set up dev net0
# ip addr add dev net0 # arbitrary address

为了使你的静态IP在启动时被分配,你可以使用network manager



# sysctl -a | grep forward



# sysctl net.ipv4.ip_forward=1
提示: 要想选择性地为某一个具体的网卡提供包转发,使用sysctl net.ipv4.conf.interface_name.forwarding=1来代替。
警告: 如果系统使用了systemd-networkd来控制网卡,就不可能为单个网卡配置IPv4的设置,换言之,systemd的操作规则会将所有配置过的转发发送到一个全局(针对所有网卡)的IPv4的设置。建议的变通方法是使用防火墙在选中的网卡上禁止转发。参考手册页以获取更多信息。在之前的systemd发行版本220/221中引入的执行内核设置的IPForward=kernel已不再适用。[1] [2]




Enable NAT

With iptables

Install the iptables package. Use iptables to enable NAT:

# iptables -t nat -A POSTROUTING -o internet0 -j MASQUERADE
# iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i net0 -o internet0 -j ACCEPT
注意: Of course, this also works with a mobile broadband connection (usually called ppp0 on routing PC).

Read the iptables article for more information (especially saving the rule and applying it automatically on boot). There is also an excellent guide on iptables Simple stateful firewall.

With nftables

Install the nftables package. To enable NAT with nftables, you will have to create the prerouting and postrouting chains in a new/exisiting table (you need both chains even if they are empty):

# nft add table ip nat
# nft add chain ip nat prerouting { type nat hook prerouting priority 0 \; }
# nft add chain ip nat postrouting { type nat hook postrouting priority 100 \; }
注意: If you want to use IPv6, you have to replace all occurences of ip with ip6.

After that, you have to masquerade the net0 adresses for internet0:

# nft add rule nat postrouting oifname internet0 masquerade

You may want to add some more firewall restrictions on the forwarding (assuming the filter table already exists, like configured in Nftables#Server):

# nft add chain inet filter forward { type filter hook forward priority 0 \; policy drop}
# nft add rule filter forward ct state related,established accept
# nft add rule filter forward iifname net0 oifname internet0 accept

You can find more information on NAT in nftables in the nftables Wiki. If you want to make these changes permanent, follow the instructions on nftables


If you are planning to regularly have several machines using the internet shared by this machine, then is a good idea to install a DHCP server, such as dhcpd or dnsmasq. Then configure a DHCP client (e.g. dhcpcd) on every client PC.

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements. See Help:Style for reference.Tango-edit-clear.png

Reason: This is not an iptables guide. Expanding the chain with iptables -I might skip other important rules; if you need to script an ON/OFF switch for this, use custom chain with a jump placed carefully in the INPUT chain. (Discuss in Talk:Internet sharing (简体中文))

Incoming connections to UDP port 67 has to be allowed for DHCP server. It also necessary to allow incoming connections to UDP/TCP port 53 for DNS requests.

# iptables -I INPUT -p udp --dport 67 -i net0 -j ACCEPT
# iptables -I INPUT -p udp --dport 53 -s -j ACCEPT
# iptables -I INPUT -p tcp --dport 53 -s -j ACCEPT

If you are not planing to use this setup regularly, you can manually add an IP to each client instead.

Manually adding an IP

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements. See Help:Style for reference.Tango-edit-clear.png

Instead of using DHCP, on each client PC, add an IP address and the default route:

# ip addr add dev eth0  # arbitrary address, first three blocks must match the address from above
# ip link set up dev eth0
# ip route add default via dev eth0   # same address as in the beginning

Configure a DNS server for each client, see resolv.conf for details.

That's it. The client PC should now have Internet.


If you are able to connect the two PCs but cannot send data (for example, if the client PC makes a DHCP request to the server PC, the server PC receives the request and offers an IP to the client, but the client does not accept it, timing out instead), check that you do not have other Iptables rules interfering.

See also