Arun Chettoor
Arun Chettoor

Reputation: 1151

Difference between EUID and UID?

EUID is not the same as UID. At what context are these both are used in the script?

I tried to get the values by echo "UID is $UID and EUID is $EUID", but only space came as output. My machine runs Ubuntu 12.04 LTS. Seen at some sites that this is usually used to check whether it is root user and all but not able to get proper difference.

Upvotes: 44

Views: 60313

Answers (2)

Fernando
Fernando

Reputation: 1450

UID is the ID of the user that executed the program.

EUID (Effective UID) is the user ID the process is executing. Usually both are equal, unless using a program with SetUID to for example increase your privileges. A common case where UID and EUID are different would be executing sudo.

EUID and UID variables only work on bash, not in dash (in Debian based distros as Ubuntu sh is usually a symlink to dash).

If you are running the script interactively you might not have bash configured as your default shell, run bash before trying.

If you are running it from console:

bash script.sh

If you are running it using its path (for example ./script.sh) ensure the first line of the script is:

#!/bin/bash

And not:

#!/bin/sh

For a more generic way to do it -that works on any shell- check: https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root

In that post the command id is mentioned, where:

id -u    # is the EUID
id -u -r # is the UID

Upvotes: 24

Barmar
Barmar

Reputation: 782693

They're different when a program is running set-uid. Effective UID is the user you changed to, UID is the original user.

Upvotes: 56

Related Questions