7

Sometimes it amazes me the "little" things I don't know about Unix. For years now I have occasionally noted that I don't actually know how to deal with the sudo password prompt cleanly. Every once in a while I sudo something only to realize that I don't actually want to run the command at all. This happens rarely enough I always forget to ask but just often enough that I remember I still haven't learned the right way to abort.

Once sudo starts asking for a password it doesn't want to give up. You can't Ctrl+C it. If you give it the right password it will run the command that I've decided I want to abort. The only solution I have found is to give it wrong passwords until it gives up asking and falls back to an su prompt which actually listens to a Ctrl+C. This feels dirty to me. It's sad enough that I changed my mind about a command; not to be able to cancel it cleanly is just embarrassing.

What is the proper way to tell sudo to abort the attempt while at the password prompt?

  • 8
    Ctrl-c exits for me in bash and zsh... – jasonwryan Jul 1 '14 at 7:26
  • @jasonwryan Now that's interesting. I wonder what is different about our configs. That would certainly seem like the obvious thing to do but it doesn't work on any of my systems. – Caleb Jul 1 '14 at 7:29
  • Tested on Arch and Debian. Nothing I can see in sudoers that would account for it. – jasonwryan Jul 1 '14 at 7:34
  • This is the closest thing I could find: ubuntuforums.org/showthread.php?t=1455643 – jasonwryan Jul 1 '14 at 8:57
  • 1
    @ryekayo I'm confused about the "right" approach as well (hence asking this). It seems like it should respond to the first Ctrl+C and for many people it is, but in some situations it obviously isn't. I have not figured out the difference yet. – Caleb Jul 1 '14 at 13:06
0

You can do this with a Ctrl + D.

0

What I usually do is Ctrl + Z to suspend the task, then kill %1 or even kill -9 %1 to stop:

> sudo ls
[sudo] password for user:

Ctrl + Z

[1]  +   512 Suspended                     sudo ls
> kill %1
[1]  + Suspended (tty output)        sudo ls
> kill -9 %1
[1]    Killed                        sudo ls

(In case you have more background tasks in your shell: The number you need to give after the percent sign is the one in brackets []).

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.