Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ...
share|improve this question
    
Are you trying to login to the user from the root account, or another user account? –  Rucent88 Jul 17 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 Jul 17 at 4:43
    
@Rucent88, yes I am logging into another user account from my user account. and I am permitted to do the same. –  Reddy Jul 17 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 –  Reddy Jul 17 at 4:46

2 Answers 2

up vote 9 down vote accepted

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

share|improve this answer

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
share|improve this answer
    
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? –  Valmiky Arquissandas Jul 17 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 Jul 17 at 5:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.