5

I wanted to run two sudo commands, piping the output from one to the other. However, when I haven't entered my password for sudo recently, it prompts me for the password. The pipes and two sudos seem to screw it up, so I can't correctly enter my password.

My solution to this is to run "sudo ls" or something else that prompts me for my password, so that the piped commands will work without asking me to enter it. This got me wondering if there was a "correct" way to run sudo such that you're not also running some other pointless command. The manpage for sudo doesn't seem to say anything about this.

  • Do you also have root access to the server? – Sree Dec 17 '14 at 15:06
  • I do. I know of many ways around this, like the NOPASSWD example in your answer, or by just becoming root. I just wanted to know if there was a "right" way. – zymhan Dec 17 '14 at 15:43
5

What you are looking for is sudo -v. From the man page:

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

(And the counterpart to explicitly remove the credentials: sudo -k)

  • Perfect! That's exactly what I was looking for. – zymhan Dec 17 '14 at 15:43
  • 1
    @WildVelociraptor for longer sessions, you may also use sudo -s which will open a root console, the classic alternative is also su to explicitly log in as root, but you will need to use the root password (which may be not set or different). Just type exit after you are done doing system tasks and return to your user. – crasic Dec 17 '14 at 19:30
-1

Adding to Anthon's answer, if you have root access to the server, you can add NOPASSWD: in the line where you have defined your user in /etc/sudoers. Once NOPASSWD: is added, sudo won't prompt you for the password.

If this is your current line:

myuser ALL=(ALL) <commands>

change it to:

myuser ALL=(ALL) NOPASSWD: <commands>

P.S: Use visudo to edit the /etc/sudoers file.

  • This would allow myuser to do litterally anything on the system, without any password needed. That's basically like making him root, which is what sudo is here to avoid. – John WH Smith Dec 17 '14 at 16:41
  • That was only an example. The point was NOPASSWD . I have updated my answer to avoid confusion for upcoming viewers. – Sree Dec 17 '14 at 16:46

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.