104

Is there a way to delete all characters that I entered in a hidden password prompt in Linux? For example, when I SSH to a server, it asks for my password where the entered keys are not shown:

$ ssh root@somehost
root@somehost's password:

Is there a way to delete all my entered text without having to press backspace for an unknown amount of time? When I think I entered something wrong I want to start over and pressing backspace for a few seconds is annoying. I tried Esc, CtrlA to hopefully select the whole text and Home. CtrlC cancels the whole command and I have to send the command again to retry. This is almost the best and fastest solution but still not satisfying. Insert does not work in my shell either.

bugybunny
  • 1,503

2 Answers2

161

You can delete the entire typed password with Ctrl+U.

Ipor Sircer
  • 4,194
14

Unlike bash, ssh's password prompt doesn't use any special terminal-input library like readline. The line-editing features are just the baseline POSIX TTY line-editing features.

So you have a POSIX TTY in "cooked" mode (not raw), aka canonical mode, and the only line editing that's available is what's provided by the kernel. See stty(1), and notice that
kill = ^U. This is also where the backspace character is defined (erase = ^?). Word-erase (^W) is convenient when you're not typing blind.

lnext = ^V means you can type control-v then anything (including control-c) to get a literal control-c.

To debug what you were trying to do blindly, run cat or cat > /dev/null in your terminal. Type stuff, then see what works and what doesn't to edit it.


readline (used by bash) reads raw character and does the line-editing in user-space. Its default bindings are compatible with the default TTY control characters, though, for the subset of editing features that they both provide.

readline goes way beyond the simple line editing of a plain TTY. (e.g. a TTY can only delete characters at the end of the line, so there's no ^a and delete or left/right arrow)

When bash runs a command in the foreground, it puts the TTY into canonical mode first (because that's the default). So running stty -a (with no redirection) will always see its own terminal in canonical mode. But if you redirect input from some other TTY that has bash running on it, you can see what terminal settings bash + readline applied. e.g. stty -a < /dev/pts/12 shows -icanon for raw mode because I have a bash running on that terminal. (I switched to another tab and ran tty, then used that device file path from the first terminal). If I ran cat in that other terminal, I'd see icanon for canonical mode.

Related: The TTY demystified

https://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html

https://en.wikipedia.org/wiki/POSIX_terminal_interface

Peter Cordes
  • 6,345