Super User is a question and answer site for computer enthusiasts and power users. 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

I have access to a linux server where I can sudo to 'foo' like this without a password:

sudo su - foo

Once I am the 'foo' user I am able to run a script like this:


The 'foo_user_script' is only executable by the foo user and not by my user. Thus I can run the script by first changing to the 'foo' user and then running the script.

However, I am trying to automate some processes using the Java SSH client ganymed. Ganymed's faq suggests that to run multiple commands I should use a contruct like:

Session.execCommand("echo Hello && echo again")

I tried something like this, but the second command (the foo_user_script) did not execute:

Session.execCommand("sudo su - foo && /dir/")

I think this doesn't work because su launches a new shell.

Is there any way for me to run the /dir/ script in a single command?

"sudo -l" shows me this:

(root) NOPASSWD: /bin/su - foo

Thanks for the help!

share|improve this question
You should ask the server provider to provide a less broken configuration of sudo. IMHO, the sudo su combination is broken and defeats the purpose of using sudo in the first place (limiting access, providing audit trails and accountability). – Slartibartfast Mar 26 '14 at 5:05
After fiddling with this for a while, I realized that because I can sudo to the user I can generate and use my own SSH keys. I generated a new key and now I can SSH to the server directly as 'foo'. – George Mar 26 '14 at 5:28
Which hopefully illustrates why 'sudo su' is not a security measure. – Slartibartfast Mar 26 '14 at 5:30

sudo -u foo /dir/

share|improve this answer
Sadly, though this is the better answer in many senses, the user's system is broken (see the output of sudo -l) to reduce security. – Slartibartfast Mar 26 '14 at 5:00

You must log in to answer this question.

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