I haven't been able to find a question that describes this specific scenario.

I am trying to execute a very basic bash script to retrieve logging from multiple machines. I am running the script locally but need to access an external machine via ssh, as well as sudo into a privileged user once on that machine...

ssh myuser@machine.net
sudo su  - privledged_user
cat logs > file.txt

Running this with sh -x reveals bash is getting stuck on the 'ssh' line. So I tried revising it to this:

ssh myuser@machine.net sudo su - privledged_user cat logs > file.txt

This also seems to stall indefinitely. Is there a better solution to this problem?? I don't see a way around using sudo su from what I can tell...

Thanks for any help!

  • Why the close vote ? This question is on topic: managing the hardware or software of servers, workstations, storage or networks , tools used for administering, monitoring, or automating these – jlliagre Dec 30 '14 at 10:38

The way I achieve this in my current environment, is to run ssh with the -t flag which forces tty allocation, and to then run sudo -u root within it, as follows:

ssh -t hostname << EOF
  sudo -u root command2
  sudo -u otheruser "command3 | command4"
  sudo -u root /bin/bash -c "command5; command6; command7"
  command8 && ( sudo -u otheruser /bin/bash -c "cmd1 ${1}; cmd2 {$2}" ) || echo cmd2 did not work


I have my account in sudoers on the remote side so that no password is required.

This example shows you different ways to do this within a single ssh session, including running multiple commands with bash or within a subshell. Note also that if you put the above code into an executable script, you can pass command line arguments ($1 and $2) to ssh and these will be expanded and then referenced on the remote side.

  • Unfortunately because I don't manage the remote server, I believe I'm stuck using sudo su here. Would that still work in this format? – Matt124234 Dec 29 '14 at 22:55
  • yes it will work too. you can enter the password when prompted. – Michael Martinez Dec 29 '14 at 22:59
  • I've tried: ssh -t mrhyner@stg-app3.indeed.net << EOF sudo su - adcentrl egrep 'ERROR\|WARN' /home/adcentrl/cronjobs/logs/*/* EOF But I received: "Pseudo-terminal will not be allocated because stdin is not a terminal." – Matt124234 Dec 29 '14 at 23:11
  • perfectly okay. ignore that warning and proceed. if you want you can disable "RequireTty" in /etc/sudoers on the remote end – Michael Martinez Dec 29 '14 at 23:14

If you don't want or can't stop sudo from asking you the password, one simple trick is to read it locally and store it in a local variable:

read -p 'Password: ' -s password

ssh -t user@ <<EOF
  echo "$password" | sudo -S whoami

If sudo is configured to allow passwordless commands, this should do what you want:

ssh myuser@machine.net "sudo su - privileged_user -c 'cat logs'" > file.txt


ssh myuser@machine.net "sudo su - privileged_user -c 'cat logs > file.txt'"

depending on whether you want file.txt file be created locally or remotely.

Otherwise, here is a way to pass the remote user's password to sudo:

echo mrhyner_password | \
  ssh mrhyner@test-server.net \
  "sudo -S su - adcentrl -c 'egrep ERROR\|WARN /home/adcentrl/cronjobs/logs/*/*'"
  • Very interesting. However using ssh without -t tells me "no tty present". Adding in -t removes this error but doesn't seem to honor 'sudo su' as it prompts me for a password. Here's specifically what I'm doing: ssh -t mrhyner@test-server.net "sudo su - adcentrl -c egrep "ERROR\|WARN" /home/adcentrl/cronjobs/logs/*/*" This gives me: bash: WARN: command not found [sudo] password for mrhyner: – Matt124234 Dec 29 '14 at 22:48
  • Sorry, I apparently don't know how to format my comments in this thing – Matt124234 Dec 29 '14 at 22:49
  • I was assuming sudo was configured to be passwordless for your remote account. – jlliagre Dec 29 '14 at 23:14
  • and you are missing extra single quotes. – jlliagre Dec 29 '14 at 23:17
  • It is, which caused me to reason that bash isn't honoring the ssh command for some reason. It's acting like I'm trying to sudo from my local host =\ – Matt124234 Dec 29 '14 at 23:17

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.