Asked 6 years, 2 months ago
How can I log in with the su command in one line in the terminal?
This is for using sudo with not getting prompted for a password every time :
edit the /etc/sudoers
file via visudo
or sudo visudo
in RHEL/CentOS 7 at least, down at the bottom of the /etc/sudoers
file you will see the COMMANDS section. Specifically this:
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL
by default... for RHEL/CentOS I can't speak for other linux distro's on how they may make use of this... simply comment out the first with #
and put into effect the line having the NOPASSWD: ALL
syntax. Or enter this syntax.
The %wheel
means those accounts in the wheel group as defined in /etc/group
. You can add a line and make it %users
for example to allow any account in the users group this no password functionality, if you use the users group (with gid 100) in that manner.
For the How can I of being just you, the %wheel
syntax with the %
means wheel is a group. To specify a specific user account, just remove the %
and use a user account in place of the group name. For example:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
simply do this if your user account name is abc123
abc123 ALL=(ALL) NOPASSWD: ALL
and best to put this at the bottom of the file so that if said user account is also part of wheel group and that is in effect above that statement doesn't override and undo this if you put this user statement at the top of the file.
I do not know how to undo the password prompt of using su in the above manner but
what you can do is sudo su <username>
and not get prompted for password, along with doing sudo su
and su'ing to the root account without a password.
What I've described is the best way I know on how to gain no password prompting functionality while maintaining decent security at the same time... the /etc/sudoers
file is the security focal point here.
The file permissions of /etc/sudoers
should be root
for both owner and group with -r--r-----
permissions or 440.
note: using abc123 ALL=(ALL) NOPASSWD: ALL
will allow that user account to do a sudo su
and switch to the root account without being prompted for the root account's password.
expect
to solve your problem, but you won't be able to do it in just one line. – YoMismo May 17 '21 at 17:58su
is this:su --login
, but you need to interactively enter the root password. – paladin Sep 16 '21 at 07:18alias stupidsu='sudo -i -u '
- the subtle ways this will bite you in the bum would take a VERY long time to explain. Why don't you just use a properly configured sudo (evensudo su
) – symcbean Jan 18 '24 at 14:28