0

At my job, the only way that I can switch users is by doing the following:

sudo su - *USER*

In order to run multiple commands as the user (in a script or something), I use here strings and here docs:

sudo su - *USER* <<< 'ls ; whoami'

Since I frequently have to log in as a user and then ssh to a server, I want to make an alias to do this in one shot, but I can't figure out how. My best guess is this, which doesn't work (it logs me in but I can't run commands):

sudo su - *USER* <<< 'ssh -tty *SERVER*'

I've also tried:

sudo su - *USER* <<< 'ssh -tty *SERVER* < /dev/null'

and

sudo su - *USER* <<< 'ssh -tty *SERVER* bash'

I think I am misunderstanding something either about ssh or su. Let me know your thoughts!

4
  • On your specific question, the problem is that here_strings and here_docs redirect the standard input. They, so to speak, become your terminal. So, when they are used up, so is the standard input. That's why you can log in, but not type. Your situation calls for automation, but I don't know that's the way to go. You might want to open another question asking on how to do it, telling what you have done so far (basically this post, with another question). Commented Nov 6, 2019 at 17:10
  • What if you do sudo su -l USER? Commented Nov 6, 2019 at 19:26
  • @JimL. according to the man pages for su, "-l" is the same as "-". Anyways, we can only do "sudo su - USER", so any deviation from that will not work. Commented Nov 6, 2019 at 19:51
  • Doh! My goof, you're correct. But on my system, sudo su -l USER works: it gives me a shell prompt as the designated user. @EduardoTrápani's advice is correct: if you use <<< you won't be able to use the terminal, and the converse. Commented Nov 6, 2019 at 20:23

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.