sudo的-E选项不起作用怎么办?

背景

前段时间在ubuntu 20.04上源码编译netopeer2及其配套库,默认安装目录前缀是/usr/local而不是/usr,一般软件这也没啥影响,但是netopeer2需要root权限(需要打开830端口),所以问题就来了。

问题的出现

sudo无法找到/usr/local/lib下的库

sudo启动netopeer2,报错,找不到libyang库,而libyang就安装在/usr/local/lib下,看来是root的LD_LIBRARY_PATH环境变量不包含/usr/local/lib

$ echo $LD_LIBRARY_PATH
/lib:/usr/lib:/usr/local/lib       # 普通用户whp的LD_LIBRARY_PATH包含/usr/local/lib,但我们的程序不能由普通用户启动,必须以root启动
$ sudo -s
[sudo] whp 的密码:
# echo $LD_LIBRARY_PATH
        # 悲催的是root用户的LD_LIBRARY_PATH为空!
#

sudo的-E选项不起作用

sudo -E启动netopeer2,报错信息依旧不变,奇怪,-E选项明明说会保留普通用户的环境变量配置啊!

     -E, --preserve-env
                 Indicates to the security policy that the user wishes to preserve their existing environment variables.
                 The security policy may return an error if the user does not have permission to preserve the environment.

sudo的–preserve-env也没用

再尝试用sudo --preserve-env=LD_LIBRARY_PATH启动netopeer2,报错信息依旧不变😓

     --preserve-env=list
                 Indicates to the security policy that the user wishes to add the comma-separated list of environment
                 variables to those preserved from the user's environment.  The security policy may return an error if the
                 user does not have permission to preserve the environment.

解决

直接在sudo命令行给具体的环境变量赋值,

sudo LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib netopeer2-server

这样就能启动了。
如果有多个环境变量,就要传递多个。

netopeer2-cli无法访问root安装的yang模型

对于netopeer2,还有个特殊障碍,因为sysrepo库的权限机制,netopeer2-cli以普通用户账户连接时,无法访问root安装的yang模型,只能以root账户连接netopeer2-server,而ubuntu是禁用了root账户的
怎么办?启用root账户!

$ sudo passwd root
新的 密码:
重新输入新的 密码:
passwd:已成功更新密码

启用后,问题解决。

后记

其实,如果切换到root登录shell(执行sudo -s命令),就不用给sudo传递什么环境变量了,直接在shell里配置环境变量就行,但这样不推荐。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值