背景
前段时间在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里配置环境变量就行,但这样不推荐。