0

So far I have this:

sshpass -p "password" ssh -q [email protected] " [ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@" ; whoami ; [run some commands as root]"

I keeps giving me username as answer from whoami. I want to be root as soon as I am connected to the server (but I can only connect to it with username). How can I be root throughout the connection to the server?

Clarification:

I want to access a remote server. It is mandatory that I connect as "username" and then switch to root to run and copy files that only root is able to do. So while I am connected to that server via ssh, I want to be root until my commands are over in the remote server. My problem is that I am not able to do so because I don't have the knowledge, hence I am posting it here.

Restrictions:

  • can't use rsync.

  • have to connect to the server as "username" and then switch to root

  • this code is in a for loop so I don't want to use su because it will keep asking for a password

1
  • 2
    I'm not sure exec sudo --... is doing what you think it is
    – user9517
    Jun 4, 2014 at 16:55

3 Answers 3

2

I don't think sudo is going to work they way you are hoping it will work.

If you have a command like sudo ... ; blah the initial shell is what interprets the ;. So the sudo command is issued, and after that closes, then the blah command is executed by the shell.

I would probably be tempted to change your command so you do something like this. The examples below assumes that sudo will not prompt you for a password, because you have setup NOPASSWD.

echo "commands to run on remote" | sshpass -p "password" ssh -q [email protected] "exec sudo /bin/bash"

A simple example would be to do something like echo whoami | sudo bash.

You also do something like this.

cat <<REMOTECOMMANDS | sshpass -p "password" ssh -q [email protected] "exec sudo /bin/bash"
whoami
command1
command2
command2
REMOTECOMMANDS
1

Use su.

The su (short for substitute user) command makes it possible to change a login session's owner (i.e., the user who originally created that session by logging on to the system) without the owner having to first log out of that session.
[...]
A simplified expression of the syntax of the su command is:
su [options] [commands] [-] [username]

Although, really, you should just sudo all your commands, but... su is how you do what you asked.

3
  • Already have, check the last restriction I wrote
    – Nosscire
    Jun 4, 2014 at 16:46
  • I am not sure why you think su would make a difference over sudo? Replacing one for the other in his command wouldn't changing anything, other then requiring the root password.
    – Zoredache
    Jun 4, 2014 at 17:16
  • @Zoredache I figured he was going to SSH in as a normal user, su to switch the logged in session to root, and then run his commands, rather than use su or sudo within his command. Jun 4, 2014 at 17:27
1

Use sudo -i for an interactive root shell.

Use sudo command or sudo -i command to run a command as root. The -i option sets up a shell and environment before running the command.

2
  • Is that really going to help though? He wants to run the commands following the sudo invocation as root. If you look at his example sudo -i; whoami The whoami command wouldn't get run with root privileges.
    – Zoredache
    Jun 4, 2014 at 16:56
  • Not really. He's got to write a script. Jun 4, 2014 at 16:58

Not the answer you're looking for? Browse other questions tagged .