25

When I run a command with sudo like the following, subsequent sudo commands will not ask for a password anymore.

sudo ls

But this still runs ls. If I don't want to run any command at the beginning, but just stop subsequent sudo commands from asking a password later on. Is there a way to do so?

user1424739
  • 991
  • 4
  • 12
  • 20
  • Assign a password to the root account which is disabled by default. THIS IS CONSIDERED DANGEROUS – David Feb 25 '21 at 15:33
  • 3
    No. I'd like a command that temporarily disables the password. sudo echo -n probably is a way but it is not very elegant. – user1424739 Feb 25 '21 at 15:36
  • Like he said <) – David Feb 25 '21 at 15:37
  • 7
    OK. sudo true is better. – user1424739 Feb 25 '21 at 15:43
  • 17
    That suspiciously sounds like an XY problem. What's wrong with entering the password the first time you actually need sudo? – danzel Feb 25 '21 at 16:47
  • 1
    I am with @danzel on this one : this seems a XYproblem. And the only reason I could see is to enjoy a root session, which defeats the security and purpose of using a regular user most of the time and only sudo the commands that requires it. – Olivier Dulac Feb 26 '21 at 17:01
  • 5
    @danzel It's useful if sudo is needed in a script and some output comes before it. If you just leave it like that, the output is going to get garbled with the sudo password prompt. In this case, you can add something like sudo true before all the parts of the script that cause meaningful output so it doesn't break the output format. – JoL Feb 26 '21 at 22:49
  • It could be used in the rare case, where you start a script, which performs a first task which takes 10 minutes, then uses the first time sudo, but you want to go shopping, while the script is running, and you don't want to run the entire script with sudo. – user unknown Feb 27 '21 at 15:30
  • @JoL that also sounds like a recipe for disaster. While there are some exceptions, in the vast majority of cases instead of having sudo inside a script, you would simply run the script with sudo. See How do I run a 'sudo' command inside a script?. – terdon Feb 27 '21 at 17:34
  • @terdon You're right that in the general case, with nuanced exceptions, it's better to require the explicit sudo when calling the script. The script I was thinking of where I use sudo inside it has a formality almost like a shell alias. Also, I said "script" in my original comment to be more relatable, but where I've actually used sudo true is actually interactively. It happens often that I build one-off loops in the shell of the form for x in ...; do echo "=== $x"; sudo .... Having the sudo prompt come after the header is irksome, so I run sudo true before or write sudo true; for .. – JoL Feb 28 '21 at 19:35

5 Answers5

54

Use sudo -v:

-v, --validate
Update the user's cached credentials, authenticating the user if necessary.

muru
  • 197,895
  • 55
  • 485
  • 740
13

While muru's answer does exactly what you want, there's also a more general way to "do nothing," even when sudo is not involved.

sudo true

will run the true command, which always succeeds, and has no additional side effects, like printing to the screen, etc. There's also the false command, which always fails. These are both useful in shell scripting.

jpaugh
  • 552
0

You have to run something, but you can run a nothing command like true.

jasen@crackle:~$ sudo true
[sudo] password for jasen: 
jasen@crackle:~$ 
jasen@crackle:~$ sudo whoami
root
jasen@crackle:~$ 
Jasen
  • 101
0

Have you considered just creating a copy of the command you want to run as root (or any other user) and setting "Special Permissions"? (either SUID or SGID)

For example:

sudo cp /bin/touch /bin/plex-touch; sudo chown plex /bin/plex-touch; sudo chmod 4755 /bin/plex-touch

Now, every time you run the command "plex-touch", regardless of your userid/sudo, it runs the command as user "plex". This works for commands owned by root, so I don't like giving super-powers willy-nilly, but there are some legitimate reasons to run commands as another user (and Linux/Unix provides this ability).

Daniel
  • 396
  • 3
  • 4
-1

I use

sudo bash

That's simple enough!

Zanna
  • 70,465
tkow
  • 69