Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As part of my normal workflow I ssh into another user's machine, switch user to them, run a command, then exit out to my own machine again:

ssh hostname
sudo su user

Is there a way to cut this down to a single line command? e.g.

ssh --someflags "runcommand"

I have tried this but get prompted for the other user's password which I do not have:

sudo ssh user@hostnme "runcommand"
share|improve this question
Something like ssh myaccount@somehost "su -u <user> -c <command>" wouldn't work? – Fiisch Oct 4 '13 at 18:56
You could always publish your key into their authorized_keys file. Then you can connect to using that users account directly. – Zoredache Oct 4 '13 at 19:28
up vote 1 down vote accepted

Do you have a user on all of the remote computers? I guess this should work, but im not sure i understand your setup correctly.

ssh youruser@hostname "sudo -u remoteuser runcommand"

share|improve this answer
And how exactly does this address the Esker's problem? – Fiisch Oct 4 '13 at 18:59
Yeah, I misread. Updated the answer. – piksel bitworks Oct 4 '13 at 19:00
With a bit of tweaking, that worked, thanks. ssh -t hostname "sudo su user -c runcommand" – Esker Oct 4 '13 at 19:03

It's often the case that a terminal is required as well. The following should work in this case:

commands='cd myfolder;ls -l'

ssh -ttt "youruser@remotehost" "sudo -u remoteuser sh -c ${commands}"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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