Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

root@mickey:/#su goofy && ssh goofy.com

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

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

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

su goofy
ssh goofy.com 

in one line?

share|improve this question

2 Answers 2

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

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

share|improve this answer
1  
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
1  
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.com" goofy
    # or
    sudo -u goofy -- ssh goofy.com
    
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

 
discard

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.