为什么 sudo 在 rc.local 中使用时不需要密码?

Vic*_*r S 5 permissions init

我已经添加了

sh /home/victor/startupAP.sh
Run Code Online (Sandbox Code Playgroud)

into/etc/rc.localstartupAP.shcontains 语句需要sudo

$ cat startupAP.sh 
#!/bin/bash
sudo hostapd -B /etc/hostapd/hostapd.conf 2>&1 >/home/victor/startupap.log
#sudo ifconfig wlan1 192.168.0.1 netmask 255.255.255.0; sudo dhcpd wlan1 -pf /var/run/dhcp-server/dhcpd.pid; sudo bash -c "echo 1 >/proc/sys/net/ipv4/ip_forward"; sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo ifconfig wlan1 192.168.0.1 netmask 255.255.255.0 2>&1 >>/home/victor/startupap.log
sudo dhcpd wlan1 -pf /var/run/dhcp-server/dhcpd.pid 2>&1 >>/home/victor/startupap.log
sudo bash -c "echo 1 >/proc/sys/net/ipv4/ip_forward" 2>&1 >>/home/victor/startupap.log
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 2>&1 >>/home/victor/startupap.log
Run Code Online (Sandbox Code Playgroud)

如果我在终端中运行这个脚本,它需要密码,

$ sudo ./startupAP.sh
[sudo] password for victor: 
Run Code Online (Sandbox Code Playgroud)

但是如果我重新启动,这个脚本在没有密码的情况下运行并按预期工作,为什么?

Bra*_*iam 6

正如 Javier 在评论中指出的那样,包含在 rc.local(和 init.d)中的脚本被运行root,然后其中一些会删除权限或更改用户,因为使用 root 帐户运行服务通常是一个安全漏洞。如果您希望脚本即使在手动调用时也由 root 运行,您可以使用SETUID

sudo chown root /home/victor/startupAP.sh
sudo chmod +s /home/victor/startupAP.sh
Run Code Online (Sandbox Code Playgroud)

但请记住,如果您使用 shebang,则setuid 将被忽略