This is my command that I have in my script:

ssh -i $KEY $USER@$HOST "sudo su tom; echo $DATA >> /home/user/file.txt

These commands work fine on there own but together in the script it just hangs.

If you run this command by itself sudo su tom; cat /home/user/file.txt It will only execute cat when you logout of the user tom.

How do you change user and execute a command in a bash script ?

  • 1
    Have you configured the sudoers file to permit password-less logins for whatever login you're using? (i.e. does there exist the chance that a password prompt is blocking the rest of the commands from proceeding?) – Paul Calabro Jan 18 '16 at 15:27

In addition to what Paul Calabro suggested, which I don't think is your main problem , you should define what command-line interpreter(shell) to use for execute the command on the remote machine, since there could be a different shell from localhost.

I think you will also need the -t param of ssh command:

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Something as follow should get closer to what you are looking for:

ssh -it $KEY $USER@$HOST 'sudo su tom; echo $DATA >> /home/user/file.txt; bash'

Unfortunately, in the above solution the "concat" of sudo su && echo still doesn't work, but using -c parameter of su command should fix

-c, --command=COMMAND pass a single COMMAND to the shell with -c

  • 1
    Yeah, those should be the correct flags to use in this situation! – Paul Calabro Jan 18 '16 at 16:12
  • tee has solved my issue, although it wouldv been nice to use the use instead of root. ssh -i $KEY $USER@$HOST "echo "$DATA" |sudo tee /home/user/file.txt;" – Narayana Jan 21 '16 at 8:50
  • The -t flag still did not enable me to do that. – Narayana Jan 21 '16 at 8:51
  • ok, thank you for the feedback, glad you solved – lese Jan 21 '16 at 8:59

Here is the command you specified:

ssh -i $KEY $USER@$HOST "sudo su tom; echo $DATA >> /home/user/file.txt

A couple minor notes about this:

  • If $KEY or $USER or $HOST contain spaces, the command will produce unexpected behavior. (Unlikely.)
  • $DATA is expanded on the host you are running the ssh command on, not on the host you are logging into remotely. (I'll assume this is what you intended.)
  • You are missing a closing " at the end of your command. I'll assume this is a copy-paste error.

Now the big point about this command, and the one that solves your problem:

sudo isn't just for running commands as root. It is for, and I quote the man page:

sudo, sudoedit - execute a command as another user

So, the simple command you want is:

ssh -i $KEY $USER@$HOST "sudo -u tom echo $DATA >> /home/user/file.txt"

This assumes that $USER has permissions to write to /home/user/file.txt. If it's only tom who has those permissions, then use:

ssh -i $KEY $USER@$HOST "sudo -u tom -i 'echo $DATA >> /home/user/file.txt'"

For more, read up on man sudo.


I was unable to switch users, executing as root seemed to do the trick.

ssh -i $KEY $USER@$HOST "echo "$DATA" |sudo tee $DIRECTORY"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.