Skip to content

Check if current user is root

I have a script that mounts my campus network drive to my machine, but in order to mount successfully, I must have root/super user privileges and run the script as one. In order to do this, it is important to know that each user has a number assigned to it stored as UID. A superuser is assigned 0 and all others are positive integers. Seeing this in practice:

pjob@mario:~$ whoami
pjob@mario:~$ echo $EUID
pjob@mario:~$ su
mario:/home/pjob$ whoami
mario:/home/pjob$ echo $EUID

To accomplis the task if a user is root, all you need to do is check if the user id is 0. This would look like:

if [[ $EUID -ne 0 ]]; then
        echo "You must be a root user" 2>&1
        exit 1
        # Do something that requires root

Posted in Tutorials. Tagged with , , .

One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. I want to be a superuser in all aspects of my life. Please advise.

You must be logged in to post a comment.