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?
Use
sudo -v
: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.will run the
true
command, which always succeeds, and has no additional side effects, like printing to the screen, etc. There's also thefalse
command, which always fails. These are both useful in shell scripting.You have to run something, but you can run a nothing command like
true
.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).
I use
That's simple enough!