I write a script for the network dispatcher in /etc/NetworkDispatcher/dispatcher.d/.
How can I get the current logged user?
I already tried these commands :
$USER
$LOGNAME
Thank you for help.
I write a script for the network dispatcher in /etc/NetworkDispatcher/dispatcher.d/.
How can I get the current logged user?
I already tried these commands :
$USER
$LOGNAME
Thank you for help.
If running a script with /usr/bin/sudo you can access the original user from the $SUDO_USER environment variable.
For example if this is the contents of a script test.sh:
#!/bin/sh
echo "USER: $USER"
echo "SUDO_USER: $SUDO_USER"
Then if you run it as "alex":
alex@yuzu:~$ ./test.sh
USER: alex
SUDO_USER:
And if you run it via sudo:
alex@yuzu:~$ sudo ./test.sh
USER: root
SUDO_USER: alex
This command should be ideal in your case:
logname
The logname command displays the login name of the current process. This is the name that the user logged in with.
Source: https://www.ibm.com/docs/zh/aix/7.1?topic=l-logname-command
You can display or print the name of the current user (also know as calling user) using logname command. This command reads var/run/utmp or /etc/utmp file to display the name of the current user.
Source: https://www.cyberciti.biz/faq/unix-linux-logname-command-examples-syntax-usage/
The logname utility explicitly ignores the LOGNAME and USER environment variables because the environment cannot be trusted.
Source: https://man.openbsd.org/logname.1