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.

7
  • 7
    Regarding Ctrl+A, in the terminal this usually means "go to start of line". The set of keys used in the terminal (especially bash) is often closer to Emacs than to Windows. Nov 1, 2017 at 14:20
  • 18
    Sshing as root is generally considered a very very bad practice.
    – Sam
    Nov 1, 2017 at 15:18
  • To delete characters from the screen, you will need to use cursor control sequences (if your terminal supports them). By running ssh from a script, you can analyse the parameter string before you run it.
    – AFH
    Nov 1, 2017 at 15:25
  • 4
    Please pay attention to what @Sam told. You should disable root logins everywhere. Log in as a regular user with a complicated password and then su to become root. The next step is to disable password-based authentication schemes in SSH and use keys for login.
    – kostix
    Nov 2, 2017 at 8:29
  • @kostix I'm pretty sure disabling passwords should be the first step. If you are using passwords, then with su and disabled root, it's just a matter of entering the password which the attacker already guessed a second time after login, so you only gain something if the user name is hard to guess (which it often isn't, I assume without having statistics). And without password login it adds a second secret, the password, but this is worth less than the private key which is a longer secret.
    – Nobody
    Nov 3, 2017 at 17:17

2 Answers 2

161

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

10
  • 6
    This also works in the regular terminal prompt too!
    – user487867
    Nov 1, 2017 at 16:16
  • 35
    For reference, this is the default key-binding in readline's "emacs mode" for unix-line-discard, described as "Kill backward from the cursor to the beginning of the current line." Ref: cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC17 GNU readline is the input library used by most shells and many other interactive programs (but is not built into the tty, so this won't work everywhere).
    – IMSoP
    Nov 1, 2017 at 16:35
  • 22
    @IMSoP However, Ctrl-U itself is built into the tty (as the default character for the stty kill function), which is why it works with the ssh password prompt.
    – Random832
    Nov 1, 2017 at 23:53
  • 5
    BTW, Ctrl+K is the equivalent for deleting from the cursor to the end of the line.
    – wjandrea
    Nov 2, 2017 at 3:40
  • 2
    @DennisJaheruddin: That's not possible, ssh doesn't use readline to provide line-editing. See my answer. Nov 5, 2017 at 13:45
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

2
  • You can actually just type 'stty' to see all the current settings. Using 'stty rows ##' or 'stty cols ##' will let you change on the Fly how many rows or columns the terminal window has available to it. Which is especially handy when you're working in a window in a window through something like VNC which doesn't necessarily catch how big your exterior window is properly. You can basically Define your active area to be smaller than the window it's in and then not have to scroll around. Allowing VI and other things to still work properly. It can also remap backspace and delete on the Fly. Nov 2, 2017 at 14:18
  • @RowanHawkins: my last paragraph was poorly edited. Fixed now. I was trying to make the point that by redirecting from another tty, you can see the stty / ioctl settings that bash+readline itself has applied in raw mode. (And the fact that it's in raw mode at all, where most of the special characters don't apply) Nov 2, 2017 at 14:26

You must log in to answer this question.

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