24

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?

10
  • 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 at 15:36
  • 7
    OK. sudo true is better. – user1424739 Feb 25 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 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 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 at 22:49
54

Use sudo -v:

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

1
  • 6
    and the opposite, -k, --reset-timestamp to forget about cached credentials – ilkkachu Feb 26 at 22:28
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.

4
  • 1
    BTW logging ismgenerally regarded as a side effect – eckes Feb 27 at 15:23
  • 3
    @eckes That's usually not a side effect of true, though, but of sudo, so the answer is technically still correct. – das-g Feb 28 at 10:17
  • 1
    Yes true, I guess it was not a useful answer of me :) – eckes Feb 28 at 10:22
  • @eckes No worries! The wording could have been clearer. (Fixed!) I had trouble drawing the line, because technically, drawing power for a nanosecond is indeed a side-effect --- as well as a host of other normal things, like returning any exit code. I almost used a more reckless wording than you know. – jpaugh Mar 2 at 17:40
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:~$ 
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).

-1

I use

sudo bash

That's simple enough!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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