Why Can't I Use 'Sudo Su' Within a Shell Script? How to Make a Shell Script Run with Sudo Automatically

Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically

Command sudo su starts an interactive root shell, but it will not convert the current shell into a root one.

The idiom to do what you want is something along the lines of this (thanks to @CharlesDuffy for the extra carefulness):

#check for root
UID=$(id -u)
if [ x$UID != x0 ]
then
#Beware of how you compose the command
printf -v cmd_str '%q ' "$0" "$@"
exec sudo su -c "$cmd_str"
fi

#I am root
mkdir /opt/D3GO/
#and the rest of your commands

The idea is to check whether the current user is root, and if not, re-run the same command with su

How to sudo su; then run command

Unless you have an unusual setup, you can't normally string su with other preceding commands like that. I would imagine it is running sudo su, then hanging in the root environment/session, because it's waiting for you to exit before preceding to the pm2 commands. Instead, I would consider something along the lines of this using the -c option:

CMD="sudo su -c 'pm2 restart 0; pm2 restart 1'"
ssh -i somepemfile.pem ubuntu@1.1.1.1 "$CMD"

As suggested in another answer, it would also probably be useful to encapsulate the $CMD variable in quotes in the ssh call.

Execute a shell script in current shell with sudo permission

What you are trying to do is impossible; your current shell is running under your regular user ID (i.e. without root the access sudo would give you), and there is no way to grant it root access. What sudo does is create a new *sub*process that runs as root. The subprocess could be just a regular program (e.g. sudo cp ... runs the cp program in a root process) or it could be a root subshell, but it cannot be the current shell.

(It's actually even more impossible than that, because the sudo command itself is executed as a subprocess of the current shell -- meaning that in a sense it's already too late for it to do anything in the "current shell", because that's not where it executes.)

How do I run a shell script as root (sudo)?

I was searching around and found this useful solution:

Edit your sudoers file to allow running certain commands without a password.

It's best to split your post-commit script into two parts, one of which will be run through sudo.

  • entry in /etc/sudoers:

    loqman    ALL=(root) NOPASSWD: /usr/local/bin/svn-postcommit-export
  • Your post-commit hook:

    #!/bin/sh
    sudo /usr/local/bin/svn-postcommit-export
  • Script /usr/local/bin/svn-postcommit-export:

    #!/bin/sh
    svn export --force file:///home/repository/trunk/ /home/memarexweb/public_html/devel/
    chmod -R 777 /home/memarexweb/public_html/devel/

    (You can choose any name and put the script anywhere; I just suggested svn-postcommit-export as an example, and /usr/local/bin as a common location.)

How do I use su to execute the rest of the bash script as that user?

The trick is to use "sudo" command instead of "su"

You may need to add this

username1 ALL=(username2) NOPASSWD: /path/to/svn

to your /etc/sudoers file

and change your script to:

sudo -u username2 -H sh -c "cd /home/$USERNAME/$PROJECT; svn update" 

Where username2 is the user you want to run the SVN command as and username1 is the user running the script.

If you need multiple users to run this script, use a %groupname instead of the username1

Running sudo su within a gitlab pipeline

In this case, you need to put your script as a multi-line string in your YAML. Alternatively, commit a shell script to repo and execute that.

and one of the commands needs to be run after doing sudo su. If I run it as a regular user, but with sudo in front of it - it doesn't work.

As a side note, you can probably use sudo -E instead of sudo su before the command. But what you have should also work with the multi-line script.

MyJob:
script: |
ssh -o -i id_rsa -tt user@host << EOF
sudo -E my_command
EOF
exit 0

Alternatively, write your script into a shell script committed to the repository (with executable permissions set) and run it from your job:

MyJob:
script: “my_script.sh”


Related Topics



Leave a reply



Submit