11

Say I started a su command, and I want to cancel it. Control+C doesn't work for su like it does for sudo... I have to finish the prompt (either by getting the password wrong enough times or by getting it right).

Is there something that I can type to kill a password prompt?

5
  • Possible duplicate of How can I undo a 'sudo' command before entering the password? Commented May 6, 2016 at 16:07
  • @DisplayName You can't control-c out of a su like you can a sudo... this is a different scenario. Commented May 6, 2016 at 16:10
  • the answer is control-D. Commented May 6, 2016 at 16:21
  • @DisplayName Yes, see Gilles answer. My point is that this should not be marked as a duplicate... Commented May 6, 2016 at 16:38
  • It is a duplicate. The answer for the post I linked also says control-D. Commented May 6, 2016 at 20:18

3 Answers 3

8

You can CTRL-D su's prompt.
This shortcut signals an end-of-file on the console, quitting the prompt; the console will display Authentication error, which is expectable.

1
  • 3
    Control-D would be an EOF (QUIT is a different thing, and is usually caught). But yes, EOF could work. Commented Mar 2, 2016 at 2:26
5

su is running with elevated privileges, and you are not seeing it respond to ^C (which sends a signal with your privileges). You could su to another shell and kill it from the other shell. Also (depending on the system), it might respond to SIGHUP (a hangup signal) if you closed the terminal session where the awkward su is in progress.

There's more than one way that su can ignore your ^C, e.g.,

  • establishing signal handlers or
  • running under a different controlling terminal.

A quick read of Debian's su seems that it uses the latter. Your system of course may be different.

Further reading:

2
  • If I execute su as a regular user and it is waiting for a password input from user, is it "running with elevated privileges" at this moment?
    – techraf
    Commented Mar 2, 2016 at 1:41
  • 1
    Yes: you can see for yourself by doing a ps -ef from another terminal. That happens automatically because su is setuid'd to root. Commented Mar 2, 2016 at 1:42
4

I've found the best way to kill su during entering of an incorrect password is Control+U followed by Control+D. This should avoid annoying delays. You can also just clear the input with Control+U and re-try typing the password in.

Control+U is a command that clears the current line of text, and also works in a shell (and probably other places too). It's also nice that it seems to work with su to clear the currently-entered password.

Using Control+C doesn't seem to work, it appears that for some reason su just ignores it. It is untrue that the user who started the process can no longer kill it because of setuid -- su dies perfectly fine if you kill it with SIGTERM or SIGQUIT from another terminal (as a user that started it). SIGINT doesn't work, so su probably just ignores it.

Control+D with an empty password input seems to be the only way to instantly kill a running su - if any text is entered, then hitting Control+D will induce a 3-second delay (unless you've actually typed in the correct password, in which case... it'll start a root shell).

You must log in to answer this question.

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