linux-networking – 如何使用systemd在Linux中正确永久启用ip转发?

前端之家收集整理的这篇文章主要介绍了linux-networking – 如何使用systemd在Linux中正确永久启用ip转发?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试启用ip转发(在enp0s3和tun0接口之间)并在/etc/sysctl.conf中写入net.ipv4.ip_forward = 1.重启后我有
$cat /proc/sys/net/ipv4/ip_forward
1

但转发仍然无法正常工作.我尝试在/etc/sysctl.conf中添加net.ipv4.conf.default.forwarding = 1.现在重启后我有了

$cat /proc/sys/net/ipv4/ip_forward
1
$cat /proc/sys/net/ipv4/conf/default/forwarding
1
$cat /proc/sys/net/ipv4/conf/all/forwarding
1
$cat /proc/sys/net/ipv4/conf/enp0s3/forwarding
0
$cat /proc/sys/net/ipv4/conf/tun0/forwarding
0

我无法在sysctl.conf中启用/ proc / sys / net / ipv4 / conf / enp0s3 / forwarding和/ proc / sys / net / ipv4 / conf / tun0 / forwarding,因为这些文件在这样的早期启动时不存在:

systemd-sysctl[85]: Couldn't write '1' to 'net/ipv4/conf/enp0s3/forwarding',ignoring: No such file or directory
systemd-sysctl[85]: Couldn't write '1' to 'net/ipv4/conf/tun0/forwarding',ignoring: No such file or directory)

,更多关于tun0是动态接口(可以随时添加删除).

如果我手动启用enp0s3和tun0的转发,则转发按预期工作.

那么,如何正确启用接口转发?

PS:内核4.1.15和systemd 226的Gentoo

PPS:如果我的记忆在一段时间之前为我服务,net.ipv4.ip_forward = 1就足够了.

解决方法

我终于解决了问题.
我使用systemd(带网络)和systemd-221中引入了关于ip转发的新功能:“IPForwarding =” – 见 https://github.com/systemd/systemd/blob/a2088fd025deb90839c909829e27eece40f7fce4/NEWS

来自man systemd.network:

[NETWORK] SECTION OPTIONS

IPForward=

Configures IP forwarding for the network interface. If enabled incoming packets on the network interface will be forwarded to other interfaces according to the routing table. Takes either a boolean argument,or the values “ipv4” or “ipv6”,which only enables IP forwarding for the specified address family,or “kernel”,which preserves existing sysctl settings. This controls the net.ipv4.conf..forwarding and net.ipv6.conf..forwarding sysctl options of the network interface (see ip-sysctl.txt[1] for details about sysctl options). Defaults to “no”.

Note: unless this option is turned on,or set to “kernel”,no IP forwarding is done on this interface,even if this is globally turned on in the kernel,with the net.ipv4.ip_forward,net.ipv4.conf.all.forwarding,and net.ipv6.conf.all.forwarding sysctl options.

所以现在我使用如下的网络文件来启用ip转发(每个接口):

# cat /etc/systemd/network/tun0.network
[Match]
Name=tun0

[Network]
IPForward=ipv4
原文链接:/linux/400514.html

猜你在找的Linux相关文章