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
  • 1,021

4 Answers4

54

Use sudo -v:

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

muru
  • 207,228
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

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
  • 506
-1

I use

sudo bash

That's simple enough!

Zanna
  • 72,312
tkow
  • 89