无需 sudo 即可运行 ip 命令

无需 sudo 即可运行 ip 命令

我目前正在编写一个利用 USB CAN 接口与 CAN 总线通信的程序。如果我将用户切换到 root,程序运行正常,但是我想以本地用户 (steven) 的身份运行该程序。该程序建立在 libsocketcan 之上 (https://github.com/lalten/libsocketcan),这就是调用我需要权限的命令的地方。具体来说: can_set_bitrate, can_do_start

我已尝试向我的 sudoers 文件添加权限,如下所示,但这并没有改变结果:

# Cmnd alias specification
Cmnd_Alias ADMIN_CMDS = /usr/sbin/passwd, /usr/sbin/useradd, /usr/sbin/userdel, /usr/sbin/usermod, /usr/sbin/visudo, /usr/bin/link, /sbin/ip
# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
steven ALL =(ALL) NOPASSWD:ALL
# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

我也尝试过使用别名来为 ip 命令添加别名sudo ip,但这也没有解决问题。我一直遇到这个错误:RTNETLINK answers: Operation not permitted 非常感谢任何帮助!谢谢!

答案1

如果您的程序运行 /bin/ip,您可以尝试设置 setuid 粘滞位。

▶ sudo chmod u+s /bin/ip

计算机上的任何用户都能够以 root 权限执行 /bin/ip。

相关内容