Identify User in a Bash Script Called by Sudo

How to access $USER in inner sudo-bash script?

You might want to try:

#!/usr/bin/env bash
sudo bash -c 'cat - <<EOF >> foo/bar.txt
Hello ${SUDO_USER}
EOF'

sudo utilizes the following environment variables:

  • SUDO_GID: Set to the group ID of the user who invoked sudo.
  • SUDO_USER: Set to the login name of the user who invoked sudo.

source: man sudo

If you cannot use SUDO_USER and it has to be USER, then you have to double quote your string your parse to bash. Double quotes will still substitute the variables:

#!/usr/bin/env bash
sudo bash -c "cat - <<EOF >> foo/bar.txt
Hello ${USER}
EOF"

How do you find the original user through multiple sudo and su commands?

Results:

Use who am i | awk '{print $1}' OR logname as no other methods are guaranteed.

Logged in as self:

evan> echo $USER
evan
evan> echo $SUDO_USER

evan> echo $LOGNAME
evan
evan> whoami
evan
evan> who am i | awk '{print $1}'
evan
evan> logname
evan
evan>

Normal sudo:

evan> sudo -s
root> echo $USER
root
root> echo $SUDO_USER
evan
root> echo $LOGNAME
root
root> whoami
root
root> who am i | awk '{print $1}'
evan
root> logname
evan
root>

sudo su - :

evan> sudo su -
[root ]# echo $USER
root
[root ]# echo $SUDO_USER

[root ]# echo $LOGNAME
root
[root ]# whoami
root
[root ]# who am i | awk '{print $1}'
evan
[root ]# logname
evan
[root ]#

sudo su -; su tom :

evan> sudo su -
[root ]# su tom
tom$ echo $USER
tom
tom$ echo $SUDO_USER

tom$ echo $LOGNAME
tom
tom$ whoami
tom
tom$ who am i | awk '{print $1}'
evan
tom$ logname
evan
tom$

How do I get the current user's username in Bash?

On the command line, enter

whoami

or

echo "$USER"

How to check if running as root in a bash script

A few answers have been given, but it appears that the best method is to use is:

  • id -u
  • If run as root, will return an id of 0.

This appears to be more reliable than the other methods, and it seems that it return an id of 0 even if the script is run through sudo.

Checking sudo in Bash (script with if statements)

If you have sudo credentials caching enabled (that is, after a successful sudo, you don't have to enter the password again for subsequent sudos) you could use the following trick:

Execute sudo true and check the return status. If the correct password was entered, the exit code will always be 0. Otherwise the exit code will be different.

if [[ "$EUID" = 0 ]]; then
echo "(1) already root"
else
sudo -k # make sure to ask for password on next sudo
if sudo true; then
echo "(2) correct password"
else
echo "(3) wrong password"
exit 1
fi
fi
# Do your sudo stuff here. Password will not be asked again due to caching.

At least on my system, root has the UID 0, not 1, so I adapted the if.

What is the difference between sudo whoami and sudo who am i ?

The command who lists the currently logged in user.

The command whoami show the current user.

Since you are the root user via sudo, sudo whoami will list root.

sudo who will list the currently logged in users.
sudo who am i will implicate option -m which is

-m only hostname and user associated with stdin

So this will only filter the one user who are you from all the other users currently logged in.

How to get $HOME directory of user in bash script root mode?

sudo runs the script as the root-user
To get the name of the user who initiated sudo you can call echo $SUDO_USER

To get its home directory:

getent passwd $SUDO_USER | cut -d: -f6


Related Topics



Leave a reply



Submit