Mismatch between ~ (user's home dir) and logged in user
Nagios is a IT infrastructure monitoring system. I am writing a simple plug-in script for it. A special account, nagios, has been created for it, and the script gets run by Nagios under that account.
For debugging purposes, the script is very simple:
#!/bin/bash
echo "OK - Running okay" ~ `id`
It echoes the value of ~
, i.e. the home directory, and the result of the id
command.
I log into the nagios account (su -l nagios
), and test it from the command line:
OK - Running okay /home/nagios uid=1005(nagios) gid=1007(nagios) groups=1007(nagios),1008(nagcmd)
That is exactly what I expect.
I run it from wit开发者_高级运维hin the Nagios system, the reported results are:
OK - Running okay /home/julian uid=1005(nagios) gid=1007(nagios) groups=1007(nagios),1008(nagcmd)
Wait, /home/julian
is my account's home directory; but it is logged into the nagios account. That contradiction makes no sense to me; I obviously don't understand how this is meant to work.
Any suggestions how this might be happening?
Is your script ran by sudo
by any chance?
rpinson@rpinson:~$ echo ~; id
/home/rpinson
uid=61367(rpinson) gid=100(users) groupes=24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),100(users),119(admin)
rpinson@rpinson:~$ sudo -u toto -s
toto@rpinson:~$ echo ~; id
/home/rpinson
uid=1001(toto) gid=1001(toto) groupes=1001(toto)
In some cases (like sudo -u
), the environment is exported so $HOME
is not resetted for the user executing the command.
In the case of sudo
, there are ways of using the user's environment:
rpinson@rpinson:~$ sudo -u toto -i
toto@rpinson:~$ echo ~; id
/home/toto
uid=1001(toto) gid=1001(toto) groupes=1001(toto)
or
rpinson@rpinson:~$ sudo -u toto -H -s
toto@rpinson:/home/rpinson$ echo ~; id
/home/toto
uid=1001(toto) gid=1001(toto) groupes=1001(toto)
Nagios is calling into a remote system so probably you are running the script through ssh julian@remote.
精彩评论