104

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

Zanna
  • 72,312
Vader
  • 1,462

6 Answers6

107

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.

αғsнιη
  • 36,350
Din
  • 2,131
98

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.

terdon
  • 104,119
33

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.

Heinzi
  • 433
18

You can do this too:

sudo -S <<< "password" command
Jahid
  • 347
14

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.

2

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.

he1ix
  • 164