7

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 ...
5
  • Are you trying to login to the user from the root account, or another user account?
    – Rucent88
    Commented Jul 17, 2014 at 4:30
  • 1
    What are you trying to do exactly ? Do you want to use sudo to run a command in a script ? Sounds like an XY problem
    – Lawrence
    Commented Jul 17, 2014 at 4:43
  • @Rucent88, yes I am logging into another user account from my user account. and I am permitted to do the same.
    – RaceBase
    Commented Jul 17, 2014 at 4:45
  • @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
    – RaceBase
    Commented Jul 17, 2014 at 4:46
  • 1
    Note that there is never any good reason to run sudo su. That's pointless and needlessly complex. Just use sudo -i to start a root shell, or if your system has an active root account, use su -. To log in as another user, use sudo -iu user. I have no idea why sudo su is so popular.
    – terdon
    Commented Apr 10, 2015 at 13:56

2 Answers 2

13

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.)

7

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
2
  • Indeed, 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? Commented Jul 17, 2014 at 5:34
  • Yes, 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.
    – Jenny D
    Commented Jul 17, 2014 at 5:38

You must log in to answer this question.

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