--- title: "Temporary Sudo access for specific user" slug: "temporary-sudo-access-for-specific-user" updated: 2024-10-10T09:34:43Z published: 2024-10-10T09:34:43Z canonical: "help.scalefusion.com/temporary-sudo-access-for-specific-user" --- > ## Documentation Index > Fetch the complete documentation index at: https://help.scalefusion.com/llms.txt > Use this file to discover all available pages before exploring further. # Temporary Sudo access for specific user Use this script to give temporary Sudo access to specific user on Linux machines. 1. Copy and save the contents below to a **UTF-8** editor like **notepad++** OR **Sublime Text** in Windows or **gedit** in Ubuntu. 1. If you are using **notepad++** then use the bottom right panel to change the type to **Unix (LF)**. ![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image-8B8BDBFZ.png) 2. Or click [here](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/Temp_Sudo_Access.sh) to download the file. ```bash #!/bin/bash # Set the username USERNAME="ENTER_USER_NAME" # Check if the user exists if id "$USERNAME" &>/dev/null; then    # Check if the user is in an active session    if who | grep -wq "$USERNAME"; then        # Note: Unsaved data may be lost or may result in data corruption        # Log out the user if in an active session        pkill -KILL -u "$USERNAME"        echo "User $USERNAME logged out."    fi    # Add the user to the sudo group    sudo usermod -aG sudo "$USERNAME"    echo "User $USERNAME added to the sudo group."    # Detect the user's timezone    USER_TIMEZONE=$(sudo -u "$USERNAME" timedatectl | grep "Time zone" | awk '{print $3}')    # Print the current timestamp based on the user's timezone    echo "Current timestamp in $USER_TIMEZONE: $(sudo -u "$USERNAME" date)"    # Schedule the removal of sudo access after X minutes using sleep    (sleep ENTER_SECONDS && sudo gpasswd -d "$USERNAME" sudo && echo "Removed sudo access for $USERNAME." && pkill -KILL -u "$USERNAME") &    echo "Scheduled removal of sudo access in X minutes, and user will be logged out."    # Print the current timestamp based on the user's timezone    echo "Current timestamp in $USER_TIMEZONE: $(sudo -u "$USERNAME" date)" else    echo "User $USERNAME not found." fi exit 0 ``` 3. In the script, replace the following placeholder: 1. Provide the username in the line: USERNAME="ENTER_USER_NAME" 2. Provide the time in seconds in the line: sleep ENTER_SECONDS && 3. On executing the script, the user will get logged out automatically from ongoing session and once they log in, they will become sudo. 4. After XX minutes (that is the time specified in the script) user will again get logged out automatically and again login back will make them Standard user. 1. The users will need to perform the logout-login action to ensure that the sudo group setting is updated. 5. The script also includes a command to auto detect the Time zone i.e. when the user was logged out - followed by at what time they became sudo user, followed by the time the user will be a sudo user. 6. The script will also revert back with a message if the mentioned user is not present. ![](https://cdn.document360.io/175c0eac-012f-4410-bdc5-ac60794f82d8/Images/Documentation/image-51Q9SGAU.png) 4. Follow [our guide](/docs/deploy-linux-shell-scripts-using-scalefusion#step-1-upload-and-deploy-the-script) to upload & publish the script using Scalefusion Dashboard. > [!CAUTION] > Note: > > 1. Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT. > 2. Please validate the scripts on a test machine before deploying them on all your managed devices. > 3. Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.