11

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?

13
  • 8
    Ctrl-c exits for me in bash and zsh...
    – jasonwryan
    Jul 1, 2014 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, 2014 at 7:29
  • Tested on Arch and Debian. Nothing I can see in sudoers that would account for it.
    – jasonwryan
    Jul 1, 2014 at 7:34
  • This is the closest thing I could find: ubuntuforums.org/showthread.php?t=1455643
    – jasonwryan
    Jul 1, 2014 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, 2014 at 13:06

2 Answers 2

3

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 []).

1
  • 1
    It depends on your system settings if it supports CTRL-C. However, this is how you kill any task that won't respond to CTRL-C. Jan 10, 2020 at 20:12
1

You can do this with a Ctrl + D.

1
  • Just tested and neither ctrl+c nor ctrl+d would work at the password prompt Apr 16, 2022 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .