Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»Check if a script is running as root user in Linux

    Check if a script is running as root user in Linux

    RahulBy RahulJuly 27, 20222 Mins Read

    Sometimes the shell scripts are required to run as the root user to perform some tasks with administrator privileges. Generally, that tasks can be achieved with the Sudo commands. If in some cases you still need to force the user to run the script as the root user, you can add code to your script to check if the script is running with the root user or not.

    Check if a shell script running as root user

    Add the following code at beginning of the shell script to check if the script is running as the root user. If the script is executed as a non-root account, it will exit with status code 1.

    1
    2
    3
    4
    5
    6
    #!/usr/bin/env bash
     
    if [ "$EUID" -ne 0 ];then
        echo "Please run this script as root user"
        exit 1
    fi

    Here the EUID is the system variable that stored the user id (UID) of the currently logged-in user. The “root” user’s UID is always 0 on the Linux systems.

    Instead of using the UID, you can also match the logged-in user name. The whoami command provides the currently logged user name. The below script will check if the script is running as a root user or not.

    1
    2
    3
    4
    5
    6
    #!/usr/bin/env bash
     
    if [ `whoami` != 'root' ];then
        echo "Please run this script as root user"
        exit 1
    fi

    Check if a shell script running as non-root user

    Sometimes the scripts are required to run as a non-root account. In that case, you can add the following snippet to check for a user account and continue only if the script is running as a normal user.

    1
    2
    3
    4
    5
    6
    #!/usr/bin/env bash
     
    if [ "$EUID" -eq 0 ];then
        echo "Please run this script as a non-root user"
        exit 1
    fi

    Conclusion

    In this quick how-to guide, you have learned about adding restrictions in a shell script to run as a root user or non-root user. If it is running a different user, the script will exit immediately.

    euid root shell script user whoami
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleYYYY-MM-DD format date in a Shell Script
    Next Article Running a Cron Every 5 Minutes

    Related Posts

    How to Import GPG Keys on Ubuntu & Debian (without apt-key)

    2 Mins Read

    Running a Cron job every Sunday (Weekly)

    2 Mins Read

    Scheduling a Python Script with Crontab

    2 Mins Read

    Running cron job every 12 hours (twice a day)

    Updated:August 2, 20221 Min Read

    How to run a command on bash script exits

    1 Min Read

    (Resolved) Key is stored in legacy trusted.gpg Keyring

    Updated:August 9, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Import GPG Keys on Ubuntu & Debian (without apt-key)
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.