Changing to root user inside shell script

switch user and run command shell script
how to become root user in ubuntu
run script as user from root
bash root user
linux switch user with password in script
ubuntu script change user
linux impersonate user
bash elevate to root

I have a shell script which needs non-root user account to run certain commands and then change the user to root to run the rest of the script. I am using SUSE11. I have used expect to automate the password prompt. But when I use spawn su - and the command gets executed, the prompt comes back with root and the rest of the script does not execute.


< non-root commands>
 spawn su -
<root commands>

But after su - the prompt returns back with user as root. How to execute the remaining of the script.

The sudo -S option does not help as it does not run sudo -S ifconfig command which I need to find the IP address of the machine.

I have already gone through these links but could not find a solution: Change script directory to user's homedir in a shell script

Changing unix user in a shell script

sudo will work here but you need to change you script a little bit:

$ cat 
sudo -s <<EOF
echo Now i am root
echo "yes!"

$ bash
uid=1000(igor) gid=1000(igor) groups=1000(igor),29(audio),44(video),124(fuse)
Now i am root
uid=0(root) gid=0(root) groups=0(root)

You need to run your command in <<EOF block and give the block to sudo.

If you want, you can use su, of course. But you need to run it using expect/pexpect that will enter password for you.

But even case you could manage to enter the password automatically (or switch it off) this construction would not work:


In this case root-command will be executed with user, not with root privileges, because it will be executed after su will be finished (su opens a new shell, not changes uid of the current shell). You can use the same trick here of course:

su -c 'sh -s' <<EOF
# list of root commands

But now you have the same as with sudo.

Changing to root user inside shell script, Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su  I have a script that I need to run as my user. Inside I will be reading a text file that is a list of users. I need to change to each of these users and execute some commands as that user. It is necessary to be that user because the commands set up files that must be owned by the owner. Typically I would use the command: sudo su - ${USERNAME}

There is an easy way to do it without a second script. Just put this at the start of your file:

if [ "$(whoami)" != "root" ]
    sudo su -s "$0"

Then it will automatically run itself as root. Of course, this assumes that you can sudo su without having to provide a password - but that's out of scope of this answer; see one of the other questions about using sudo in shell scripts for how to do that.

How to Switch from Local user to root user from a shell script , The -u (user) option causes sudo to run the specified command as a user other than root. To specify a uid instead of a user name, use #uid. When running  yes, utilities that change the current user or group (su, sg, nwegrp, sudo, etc.) always launch a new shell. It is that way because a program can't change the credentials of its parent process. It can only change its own and afterwards, have its descendants inherit them.

The easiest way to do that would be to create a least two scripts.

The first one should call the second one with root privileges. So every command you execute in the second script would be executed as root.

For example:

sudo su-c'./'

apt-get install mysql-server-5.5

or whatever you need.

Linux switch user and execute command immediately, If you are in the shell under your "username", you can change it to another user (​say root) using the su command. This is especially used when  Presented approach is the best of the all and is complete. All is written inside single script, and all uses bash. Effectively, this script is runned twice. At first script test it is root, then prepare environment, and change user by su. But do it with exec command, then there is only single script instance.

Also, note that if you are changing to "root" user inside a shell script like below one, few Linux utilities like awk for data extraction or defining even a simple shell variable etc will behave weirdly.

To resolve this simply quote the whole document by using <<'EOF' in place of EOF.

sudo -i <<'EOF'
echo "I am root now"

Using the "su" command and switching from user to root – KB.IWEB , The su (short for substitute or switch user) command allows you to run utility with the privileges of another user, by default the root user. In this  su command is used to switch the current user to another user from SSH. If you are in the shell under your "username ", you can change it to another user (say root ) using the su command. This is especially used when direct root login is disabled.

Short version: create a block to enclose all commands to be run as root.

For example, I created a script to run a command from a root subdirectory, the segment goes like this:

sudo su - <<EOF
cd rootSubFolder/subfolder

Su Command in Linux (Switch User), To switch the logged-in user in this terminal window, enter If you omit a username, it will default to the root  Before: ----- $ ls -lh -rw-r--r-- 1 root root 761 Aug 19 00:48 -rw-r--r-- 1 root root 159 Aug 19 00:48 Command: ----- $ sudo chown -c daygeek:daygeek changed ownership of '' from root:root to daygeek:daygeek changed ownership of '' from root:root to daygeek:daygeek After

SU Command in Linux: How to Use With Examples {2020 Tutorial}, Use the sudo command in the script. In the form: sudo -u username command. the sudo command runs command as the user username. If the script is being run​  This gives you root access, but maintains your current SHELL. Shell specific settings, including your current directory, are preserved. For instance if you use bash (Ubuntu's default shell), aliases (and any other settings from ~/.bashrc) are kept when you switch to the root user. To leave the root access, type exit as in the cases above.

Run part of a bash script as a different user, Inside I will be reading a text file that is a list of users. Since the script is ostensibly running as sudo/root, why not just set #!/bin/bash while read username do su - $username -c "your commands here" done < users.txt. I am trying to change user password using a shell script as below #!/bin/bash mysql.server start mysql -u root << EOF SET PASSWORD FOR root@'localhost' = PASSWORD(‘admin’); EOF But I am getting below error:-

[SOLVED] Changing user inside of script, Explains how to log in as root user ( super user) under UNIX / Linux You need to use the su or sudo command to switch to root user account.