无法为 sudo -u "user" 设置环境变量

Not able to set environment variable for sudo -u "user"

我已经无计可施了 当我执行以下命令时:

sudo -u icinga '/usr/lib//nagios/plugins/check_db2_health' '--database' 'mydatabase' '--environment' 'DB2DIR=/opt/IBM/db2/V11.1.4fp5a' '--environment' 'DB2INSTANCE=mydatabase' '--environment' 'INSTHOME=/srv/db2/home/mydatabase' '--report' 'short' '--username' 'icinga' '--mode' 'connection-time' '--warning' '50'

输出结果如下

[DBinstance : mydatabase] Status : CRITICAL - cannot connect to mydatabase. install_driver(DB2) failed: Can't load '/usr/lib/nagios/plugins/PerlLib/lib/perl5/site_perl/5.18.2/x86_64-linux-thread-multi/auto/DBD/DB2/DB2.so' for module DBD::DB2: libdb2.so.1: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.18.2/x86_64-linux-thread-multi/DynaLoader.pm line 190.
 at (eval 10) line 3.
Compilation failed in require at (eval 10) line 3.
Perhaps a required shared library or dll isn't installed where expected
 at /usr/lib//nagios/plugins/check_db2_health line 2627.

但是当我使用 su - icinga

登录到用户 icinga 时

和运行

'/usr/lib//nagios/plugins/check_db2_health' '--database' 'mydatabase' '--environment' 'DB2DIR=/opt/IBM/db2/V11.1.4fp5a' '--environment' 'DB2INSTANCE=mydatabase' '--environment' 'INSTHOME=/srv/db2/home/mydatabase' '--report' 'short' '--username' 'icinga' '--mode' 'connection-time' '--warning' '50'

它工作正常。

如何在触发 sudo -u icinga 命令时设置环境变量? 我在 SUSE linux

我有点想设置一个全局环境变量,就像 icinga 中的环境变量一样,它可以跨服务器上执行的所有命令工作,而不必使用 sudo -E 等,因为我无法更改 icinga 调用插件

当你执行 sudo

时你需要 运行 db2profile

sudo -u icinga sqllib/db2profile; '/usr/lib//nagios/plugins/check_db2_health' ...