I am trying to set net.ipv4.ip_forward
to 1 in /etc/sysctl.conf
.The following works fine but it sure missing some edge cases
我正在尝试设置net.ipv4。ip_forward to 1 in /etc/sysctl.conf。下面的操作很好,但是它确实遗漏了一些边缘情况
#Enable IP packet forwarding so that our VPN traffic can pass through.
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
sed -i 's/#net.ipv4.ip_forward = 1/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
grep -qF "net.ipv4.ip_forward" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
For e.g if the sysctl.conf contain any one of the following it won't match
为e。如果sysctl g。conf包含以下任何一个它不匹配
#net.ipv4.ip_forward=1
# net.ipv4.ip_forward = 1
##net.ipv4.ip_forward=1
.
# # net.ipv4.ip_forward = 1。
Is there a more reliable way to modify settings in config files ?
有更可靠的方法来修改配置文件中的设置吗?
3 个解决方案
#1
2
You can use the -r
switch to enable Extended Regular Expressions(ERE
) in GNU sed
and optionally match the white-space and the #
occurence with the regex ?
optional item anchor,
您可以使用-r开关来启用GNU sed中的扩展正则表达式(ERE),并可选择地将空格和#发生率与regex进行匹配。可选的项锚,
sed -ir 's/#{1,}?net.ipv4.ip_forward ?= ?(0|1)/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
This will match for any of the below input lines and modify it with the replacement part net.ipv4.ip_forward = 1
这将匹配以下任何输入行,并使用替换部分net.ipv4对其进行修改。ip_forward = 1
net.ipv4.ip_forward=0
net.ipv4.ip_forward = 0
#net.ipv4.ip_forward=0
##net.ipv4.ip_forward = 0
#net.ipv4.ip_forward=1
##net.ipv4.ip_forward=1
#net.ipv4.ip_forward = 1
##net.ipv4.ip_forward = 1
See the RegEx Demo for more clarity.
请参阅RegEx演示,以获得更清晰的信息。
#2
1
What about removing all lines matching net.ipv4.ip_forward
, no matter whether commented out or not and then appending what you need?
删除所有匹配的网络。ipv4。ip_forward,无论是否注释掉,然后添加什么?
fgrep -v net.ipv4.ip_forward /etc/sysctl.conf > /etc/sysctl.conf.tmp
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf.tmp
mv /etc/sysctl.conf.tmp /etc/sysctl.conf
This is simple and readable.
这是简单易读的。
#3
0
You can use this setting to do it for zero or more spaces and zero or multiple #(comment)
您可以使用这个设置来为0或更多的空格和0或多个#(注释)
sed -i 's/[#| ]*net.ipv4.ip_forward[ ]*=[ ]*0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
grep -qF "net.ipv4.ip_forward" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
#1
2
You can use the -r
switch to enable Extended Regular Expressions(ERE
) in GNU sed
and optionally match the white-space and the #
occurence with the regex ?
optional item anchor,
您可以使用-r开关来启用GNU sed中的扩展正则表达式(ERE),并可选择地将空格和#发生率与regex进行匹配。可选的项锚,
sed -ir 's/#{1,}?net.ipv4.ip_forward ?= ?(0|1)/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
This will match for any of the below input lines and modify it with the replacement part net.ipv4.ip_forward = 1
这将匹配以下任何输入行,并使用替换部分net.ipv4对其进行修改。ip_forward = 1
net.ipv4.ip_forward=0
net.ipv4.ip_forward = 0
#net.ipv4.ip_forward=0
##net.ipv4.ip_forward = 0
#net.ipv4.ip_forward=1
##net.ipv4.ip_forward=1
#net.ipv4.ip_forward = 1
##net.ipv4.ip_forward = 1
See the RegEx Demo for more clarity.
请参阅RegEx演示,以获得更清晰的信息。
#2
1
What about removing all lines matching net.ipv4.ip_forward
, no matter whether commented out or not and then appending what you need?
删除所有匹配的网络。ipv4。ip_forward,无论是否注释掉,然后添加什么?
fgrep -v net.ipv4.ip_forward /etc/sysctl.conf > /etc/sysctl.conf.tmp
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf.tmp
mv /etc/sysctl.conf.tmp /etc/sysctl.conf
This is simple and readable.
这是简单易读的。
#3
0
You can use this setting to do it for zero or more spaces and zero or multiple #(comment)
您可以使用这个设置来为0或更多的空格和0或多个#(注释)
sed -i 's/[#| ]*net.ipv4.ip_forward[ ]*=[ ]*0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
grep -qF "net.ipv4.ip_forward" /etc/sysctl.conf || echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf