Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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

mickey (who is logged in as root) tries to use goofy's account to ssh into

root@mickey:/#su goofy && ssh

however this fails to login goofy on, instead: goofy@mickey:/$ (only su goofy worked, so goofy exits:

goofy@mickey:/$ exit
Permission denied (publickey).

I'd expect the permission error for any user other than goofy. How can I do

su goofy

in one line?

share|improve this question

Because su goofy opens a new session, but the && ssh will go to the original. So the && ssh command is executed under the original, not su-ed user.

Try in one line like this: sudo -u goofy -H sh -c "ssh"

share|improve this answer
That example will do exactly the same except it isn't dependent on the first command exiting with a zero-status. Your reasoning is correct though. – Oli Sep 3 '13 at 9:35
See my updated solution. – Frantique Sep 3 '13 at 9:38
thanks a ton for your answer Frantique & your suggestions oli. for those who do not understand the solution right away: The ‑c (class) option causes sudo to run the specified command with resources limited by the specified login class. The ‑H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. sh means "it's" run in a new sub-shell. anything that follows after "sh" in frantique's answer is exec'd in a new shell right? i hope i'll manage to combine it with rsync ;) – Pascal Sep 3 '13 at 12:30

A few reasons:

  • If you just mean to ssh as another user remotely, specify a username:

    ssh goofy@server
    # or 
    ssh -u goofy server
  • If you need to change user locally and then run ssh as that user, use su correctly. su forks out into its own session so commands you tail it with will run after it ends. You could translate your command to:

    Turn into goofy and after I'm done there (and assuming it worked), run ssh.

    Look at the -c/--command argument of su (run man su for the manual) or look at the sudo command which has a more logical syntax in my opinion. Here is how you would properly write your command (assuming that's really what you want to do):

    su -c "ssh" goofy
    # or
    sudo -u goofy -- ssh
share|improve this answer
i need a one-line-solution that chowns as root, ssh's and rsync's as "goofy" ;) why? simply 'cuz "Command to run after backup" in virtualmin's backup & restore GUI is run by root and is just one line. why not do it as root? being paranoid doesn't mean that no one is following you ^^ – Pascal Sep 3 '13 at 12:31

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.