I know I can become root (super user) via the su
command but I have to authorize it after entering the commands. Is there a way I can become root and authorize (with password) in one line
6 Answers
In terminal run the visudo
command to edit the sudoers file:
sudo visudo
and add the following line to the sudoers list
username ALL = NOPASSWD : ALL
Note: Replace username with your real UserName in above line.
-
11This would seem to be a huge security risk. I certainly couldn't recommend this course of action. Mar 27, 2015 at 15:02
-
10@ElderGeek why? Other than risking someone sitting at your computer and doing some sudo evil, is there any other security implications of adding
username ALL = NOPASSWD : ALL
?– brilloutJul 28, 2015 at 15:57 -
1Note: I had to log out and log back in before the password prompt went away using this technique (Mint 17.3) -- Also this should be placed at the end of the sudoers list or it could get clobbered by other settings as the sudoers is processed line by line. Also, be careful with this on production systems (see 'sudo evil' thread above :)– RyanNerdMar 16, 2016 at 22:50
-
11Forgetting to put this line LAST in sudoers burns me every time. PUT IT AT THE END.– moodboomSep 26, 2016 at 1:56
-
2I would consider jiminikiz approach here a safer more well considered approach. askubuntu.com/questions/192050/… Oct 2, 2016 at 12:25
Well, the only thing I can think of is
echo 'password' | sudo -S command
The -S
flag makes sudo
read the password from the standard input. As explained in man sudo
:
-S, --stdin
Write the prompt to the standard error and read the password from the standard input instead of using the terminal device. The password must be followed by a newline character.
So, to run ls
with sudo
privileges, you would do
echo 'password' | sudo -S ls
Note that this will produce an error if your sudo
access token is active, if you don't need to enter your password because you've already done so recently. To get around that, you could use -k
to reset the access token:
echo 'password' | sudo -kS ls
I don't know of any way of getting you into an actual root shell (like su
or sudo -i
) do. This might be enough for what you need though.
-
35It should be noted, too, that this method should be considered extremely insecure, as your password is written in your history and may also be visible to other users through a process listing. I'm not sure how big a deal that is for a single-user desktop system though.– jkt123May 22, 2014 at 2:07
-
1The only use I can see for this is to write a script that includes a command that requires root privileges. This is very bad, as the password is in plain text in the script.– MarcMay 22, 2014 at 2:20
-
1@Marc and everybody else, don't tell me. I would never use it and don't have any idea why the OP would want to. It is extremely insecure and cumbersome and playing with the access token timeout settings is much better. If you want to suggest to the OP that he not do this, please leave a comment under the question. I just answered the thing, I'm not espousing it.– terdonMay 22, 2014 at 2:36
-
14If you don't want something saved to bash history, prepend it with a space. That (by default) will stop it being saved.– Oli ♦May 22, 2014 at 8:21
-
11To anyone reading this answer: please don't do this. You don't automate password input - you avoid the need for passwords for specific situations. Like the other answer says. Nov 13, 2015 at 17:13
The echo 'password' | sudo -kS ls
solution works, but it has a few security drawbacks, most of which have already been mentioned in the comments to terdon's answer. Thus, I would like to suggest a different approach:
If it is only one command that you frequently need to execute, e.g. apt-get upgrade
, you can configure your system such that sudo someCommand
does not require a password.
To do that, run visudo
and enter something similar to the following:
myusername ALL=(ALL) NOPASSWD: /usr/bin/apt-get upgrade
Note that if you enter a command without an argument (e.g. without upgrade
in this example), the user will be allowed to run it with any argument, so use with care.
-
3For extra security: Use this method to give permissions to a read only shell script. This way you can run the commands you need only in the context you need. Feb 12, 2016 at 4:49
-
2
You can do this too:
sudo -S <<< "password" command
-
3
-
1
Instead of removing the password altogether, you can increase the timeout so you only have to type it a few times per day; in a terminal, run:
sudo visudo
At the end of the file, add following line to set a timeout of 30 minutes (replace jsmith
with your username).
Defaults:jsmith timestamp_timeout=30
You can use any number you want; -1
means no timeout (prompted only the first time), and 0
means instant timeout (prompted every time you sudo
). The default timeout is 5 minutes.
Vader, from your comment on your original question, you'd like to switch to an interactive shell running with super-user permissions, right?
Sudo has a specific argument to request a shell:
-s [command]
The -s (shell) option runs the shell specified by the SHELL environment variable if
it is set or the shell as specified in the password database. If a command is
specified, it is passed to the shell for execution via the shell's -c option. If no
command is specified, an interactive shell is executed.
This avoids the already mentioned security drawbacks, and allows to "go root" by using the following command:
sudo -s
IF I really have to run a root shell (in most cases I don't), then I find it very helpful to have the HOME environment variable of the shell set accordingly (to reflect running as "root"), this can be done using the "-H" flag. So the full command would be
sudo -s -H
You can find a lot more details in sudo's man-page.
sudo command
are you?su
-> enter password. It doesn't like look this is a valid command:su -password