Sudo Su automated login
Solution 1
Do not do that! That will leave your password in your shell's history!
If you really have to do that, what I recommend is that you configure your sudoers
file to allow a passwordless login.
To do that, run the command sudo visudo
and add a line like this one:
reddy ALL=NOPASSWD: /bin/su - *
(where reddy
would be your username).
If you need to give this access to the whole team, create a (UNIX) group with the team, and replace reddy
with %team
(the team group).
(Also, a little off-topic: the echo
command already adds the \n
you needed. If you actually wanted the opposite (which you don't), the command would be echo -n
.)
Solution 2
I agree with Valmiky that you're going about it the wrong way, but the sudoers line there isn't what I'd recommend. With his line, you are all authorized to sudo to anybody else including root without password. This effectively gives you full access to the server, meaning that the /bin/su
part of the line is redundant.
If you should only be able to sudo to this one particular user without giving your password, the correct line should be
%team ALL = (user) NOPASSWD: ALL
Now you can run anything as that user without giving a password, while e.g. anything requiring you to sudo to root will still require a password.
You can now in your .bashrc do
sudo -u user /bin/bash
Related videos on Youtube
Comments
-
RaceBase over 1 year
I want to automate the login process with
sudo su
. I have seen lot of questions on these, but none of them are working for me.Automate login with sudo and su
Here's my what I tried so far
echo "mypassword" | sudo -S su - user echo "mypassword\n" | sudo -S su - user echo mypassword | sudo -S su - user echo "mypassword" | sudo -S su user echo mypassword | sudo -S su user
I am just getting the below response
-bash: line 1: mypassword: command not found
My Bash version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc.
As a note, my password contains special character
@
@ sudo usage: sudo [-D level] -h | -K | -k | -V usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user name] [-u user name|#uid] [-g groupname|#gid] [command] usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ...
-
Rucent88 almost 10 yearsAre you trying to login to the user from the root account, or another user account?
-
Lawrence almost 10 yearsWhat are you trying to do exactly ? Do you want to use sudo to run a command in a script ? Sounds like an XY problem
-
RaceBase almost 10 years@Rucent88, yes I am logging into another user account from my user account. and I am permitted to do the same.
-
RaceBase almost 10 years@Lawrence, in our team, we have common user for application. so we all sudo to the same user to do any stuff on that server/application. i want to automate this one through my .bashrc
-
-
Valmiky Arquissandas almost 10 yearsIndeed, I assumed they would be sudo'ing to more than one user, and that they didn't want to type
sudo
every time they wanted to run a command as those users, but it looks like it will be just one user. This means that changing the*
to the username should yield the same result, right? -
Jenny D almost 10 yearsYes, but to be honest I don't see the point of having "/bin/su" as a command spec, since the
sudoers
format already allows for specifying the target user.