Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing to root user inside shell script

Tags:

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.

Eg.

< 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

like image 615
Deepti Jain Avatar asked Jul 24 '12 18:07

Deepti Jain


People also ask

How do I change the root user in shell script?

To give root privileges to a user while executing a shell script, we can use the sudo bash command with the shebang. This will run the shell script as a root user. Example: #!/usr/bin/sudo bash ....

How do I switch users in Linux shell script?

The su command lets you switch the current user to any other user. If you need to run a command as a different (non-root) user, use the –l [username] option to specify the user account. Additionally, su can also be used to change to a different shell interpreter on the fly.


2 Answers

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

$ cat 1.sh  id  sudo -s <<EOF echo Now i am root id echo "yes!" EOF  $ bash 1.sh 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) yes! 

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:

user-command su  root-command 

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 EOF 

But now you have the same as with sudo.

like image 196
Igor Chubin Avatar answered Sep 19 '22 07:09

Igor Chubin


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

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

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.

like image 26
Benubird Avatar answered Sep 17 '22 07:09

Benubird