10
2
I have several machines I ssh into regularly only for the purpose of using sudo su
to spend the rest of my session logged in as some special-purpose user. The general workflow is:
mymachine:~ me$ ssh me@othermachine
othermachine:~ me$ sudo su - specialuser # note: no password needed
othermachine:~ specialuser$ # do stuff
I'd like to boil this down into a one-liner that I can alias, so I can just set up an alias for each machine and get to where I need to be in a single command, without having to type the sudo su - specialuser
boilerplate. I could maybe set up me@othermachine
to sudo su
on login, but I'd like to keep the flexibility to operate as me
if I need to.
(Note: I don't have any control over othermachine
or the way it's set up; this is an established workflow that I came in on when I was hired.)
My first thought was just
ssh me@othermachine "sudo su - specialuser"
and this sort of works, but it gets me no prompt, ^C
kills it and logs me out, and I assume various other things are probably wrong too.
After reading Run Remote ssh command with Full Login Shell I tried a couple of more exotic things like
ssh me@othermachine 'bash -l -c "sudo su - specialuser"'
and
ssh me@othermachine 'bash -l -c "sudo su - specialuser"; bash'
-- neither of which I expected to work, and they didn't, but I thought I should try them for completeness (and to avoid close-as-duplicate); they produced the same prompt-less shell (the second with an added bonus prompt-less shell for me
after exit
-ing from the one for specialuser
). And I tried
ssh me@othermachine "sudo su - specialuser -c bash -l"
but it just got me
sudo: no tty present and no askpass program specified
Better ideas?
What about adding the sudo command to
~/.profile
after a short delay? – Alex – 2015-02-06T23:09:24.280The idea being that in the odd case I don't want to
sudo
I should hit^C
? If I can't come up with anything better I might try that. – David Moles – 2015-02-06T23:13:45.9431You could
su me
fromspecialuser
. Or in.profile
or.bashrc
, if you don't follow thesudo
withexit
, your firstexit
will take you back tome
, with a second to end the session. Or even use a flag file, sosudo
is preceded by[ -f ~/.keep.me ] && del ~/.keep.me
and followed by[ \! -f ~/.keep.me ] && exit
: you then need only a script or alias for a commandme
as:>~/.keep.me; exit
. Nowexit
will end your session andme
will go back to your login session. – AFH – 2015-02-06T23:52:30.5631
Please consider to write in your final version
– Hastur – 2015-02-07T10:42:16.453/bin/bash
and not a simplebash
for security reason (to avoid trojan ). Especially if there is asudo
before...