[Solved]如何能让net.ipv4.ip_forward=1永久生效?

Web、Mail、Ftp、DNS、Proxy、VPN、Samba、LDAP 等基础网络服务
回复
gnwd
帖子: 74
注册时间: 2012-10-04 15:35

[Solved]如何能让net.ipv4.ip_forward=1永久生效?

#1

帖子 gnwd » 2024-01-01 17:43

【背景】虚拟机kvm中安装的win10系统。系统采用NAT模式建立的网络。突然一天客户机上不了网了,原因不明。
【解决办法】宿主机中将ip forward打开就好了,这个解决办法是正确的,经过多次验证。
【具体步骤】

代码: 全选

1. 检查宿主机上是否开启数据包转发功能

cat /proc/sys/net/ipv4/ip_forward

2. 临时开启:

echo 1 > /proc/sys/net/ipv4/ip_forward

3. 永久开启,开机自动加载配置:

echo net.ipv4.ip_forward=1 >>/etc/sysctl.conf
【存在的问题】
永久开启设置后,重新启动电脑并不能生效。但是执行sysctl -p后,sysctl.conf的内容被读取并生效。
【问题的解决办法】
启用/etc/rc.local脚本,里面写入sysctl -p一句。同时配置systemctl enable rc-local 和 systemctl start rc-local,均成功配置(需要一点额外的工作,不赘述)。
【结果】
重新启动电脑后,cat /proc/sys/net/ipv4/ip_forward的结果还是0,表明转发没有启用成功。正常来说,这里有两次设置转发的时机:①/etc/sysctl.conf的内容 ②rc.local中手动调用sysctl -p。

现在能确认rc.local的内容没有问题并在系统启动后能自动执行,因为手动运行system restart rc-local后,cat /proc/sys/net/ipv4/ip_forward的值马上就修改为1了。并且在/var/log/boot.log或者/var/log/syslog中都有类似下面的日志。

代码: 全选

Jan  1 17:06:10 aug-HP-ZBook-15u-G5 rc.local[8463]: net.ipv4.ip_forward = 1
Jan  1 17:06:10 aug-HP-ZBook-15u-G5 rc.local[8463]: net.ipv6.conf.all.forwarding = 1
现在搞不定了,ufw没有启用。

===========================

【后续结论】
还是跟启动顺序有关,后续有别的东西再次改动了转发设定。
上次由 gnwd 在 2024-01-03 7:43,总共编辑 1 次。
头像
astolia
论坛版主
帖子: 6451
注册时间: 2008-09-18 13:11

Re: 如何能让net.ipv4.ip_forward=1永久生效?

#2

帖子 astolia » 2024-01-02 9:22

至少要把用的是什么系统说一下吧。

在我的ubuntu 24.04系统上,systemd版本是255。sysctl.conf文件是由systemd-sysctl.service服务来处理,取代procps包提供的/etc/init.d/procps,用systemctl status systemd-sysctl可以看到服务状态是static。同样rc-local服务的状态也是static。所以根本不需要你用systemctl干什么事,直接修改sysctl.conf或创建一个可执行的/etc/rc.local文件就能生效。你的这句
gnwd 写了: 2024-01-01 17:43需要一点额外的工作,不赘述
在我看来等同于“我的系统被我瞎改过了”。这两个服务都要满足一定的条件才会运行,你改过的系统在启动时还满不满足就不好说了。具体条件用systemctl cat xxx来看Unit段下Condition开头的。还有就是你用systemctl list-dependencies来看看依赖关系被破坏没有
gnwd
帖子: 74
注册时间: 2012-10-04 15:35

Re: 如何能让net.ipv4.ip_forward=1永久生效?

#3

帖子 gnwd » 2024-01-02 18:28

astolia 写了: 2024-01-02 9:22 至少要把用的是什么系统说一下吧。

在我的ubuntu 24.04系统上,systemd版本是255。sysctl.conf文件是由systemd-sysctl.service服务来处理,取代procps包提供的/etc/init.d/procps,用systemctl status systemd-sysctl可以看到服务状态是static。同样rc-local服务的状态也是static。所以根本不需要你用systemctl干什么事,直接修改sysctl.conf或创建一个可执行的/etc/rc.local文件就能生效。你的这句
gnwd 写了: 2024-01-01 17:43需要一点额外的工作,不赘述
在我看来等同于“我的系统被我瞎改过了”。这两个服务都要满足一定的条件才会运行,你改过的系统在启动时还满不满足就不好说了。具体条件用systemctl cat xxx来看Unit段下Condition开头的。还有就是你用systemctl list-dependencies来看看依赖关系被破坏没有
反正直接修改sysctl.conf不生效,不知道为什么。只要手动执行一下sysctl -p就可以起作用(读取配置文件)。系统是22.04。另外我搜索了/etc目录,没有别的地方有ip_forward关键字了。

每次启动结果都是这样

代码: 全选

aug@aug-HP-ZBook-15u-G5:~$ cat /proc/sys/net/ipv4/ip_forward
0
systemd-sysctl和rc-local的状态如下

代码: 全选

aug@aug-HP-ZBook-15u-G5:~$ systemctl status systemd-sysctl
● systemd-sysctl.service - Apply Kernel Variables
     Loaded: loaded (/lib/systemd/system/systemd-sysctl.service; static)
     Active: active (exited) since Tue 2024-01-02 18:06:08 CST; 13min ago
       Docs: man:systemd-sysctl.service(8)
             man:sysctl.d(5)
    Process: 284 ExecStart=/lib/systemd/systemd-sysctl (code=exited, status=0/SUCCESS)
   Main PID: 284 (code=exited, status=0/SUCCESS)
        CPU: 5ms
aug@aug-HP-ZBook-15u-G5:~$ systemctl status rc-local.service 
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; enabled; vendor preset: enabled)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Tue 2024-01-02 18:06:19 CST; 13min ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 1602 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
        CPU: 3ms

1月 02 18:06:19 aug-HP-ZBook-15u-G5 systemd[1]: Starting /etc/rc.local Compatibility...
1月 02 18:06:19 aug-HP-ZBook-15u-G5 rc.local[1604]: net.ipv4.tcp_syncookies = 1
1月 02 18:06:19 aug-HP-ZBook-15u-G5 rc.local[1604]: net.ipv4.ip_forward = 1
1月 02 18:06:19 aug-HP-ZBook-15u-G5 rc.local[1604]: net.ipv6.conf.all.forwarding = 1
1月 02 18:06:19 aug-HP-ZBook-15u-G5 systemd[1]: Started /etc/rc.local Compatibility.
rc-local.service文件内容如下:

代码: 全选

aug@aug-HP-ZBook-15u-G5:/etc$ find ./ -name "rc-local*"
find: ‘./libvirt/secrets’: Permission denied
find: ‘./polkit-1/localauthority’: Permission denied
find: ‘./v2raya’: Permission denied
find: ‘./cups/ssl’: Permission denied
./systemd/system/multi-user.target.wants/rc-local.service
find: ‘./ssl/private’: Permission denied
aug@aug-HP-ZBook-15u-G5:/etc$ cat ./systemd/system/multi-user.target.wants/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target

gnwd
帖子: 74
注册时间: 2012-10-04 15:35

Re: 如何能让net.ipv4.ip_forward=1永久生效?

#4

帖子 gnwd » 2024-01-02 18:31

我把v2raya.service停用了就好了……
gnwd
帖子: 74
注册时间: 2012-10-04 15:35

Re: 如何能让net.ipv4.ip_forward=1永久生效?

#5

帖子 gnwd » 2024-01-02 18:36

ksnip_20240102-183402.png
同时这玩意儿有个选项。我没留意,通常让他在后台运行就没管它,一直没开启。
回复