77

When I ssh into another machine with Debian with my account(with sudo permissions), my backspace key generates some awkward symbols on pressing. Also Tab & del keys don't work too.

On the other hand, I also have another account on the same machine & when I ssh through this account, its terminal works perfectly fine. I couldn't figure out why is this happening.

0

11 Answers 11

92

Beside "stty" solution, you may try the "TERM" solution.

You ssh to your Debian from some terminal (putty, solaris dterm, debain xterm, you-name-it), this termninal announce capabilities (which includes keys such as Backspace and Tab) via TERM environment variable.

So, after ssh to unix host (it doesn't depend debian it or other host) set the TERM variable according to your terminal. Consider you're using bash as shell and vt100 as terminal:

export TERM=vt100

ps: TERM should be announced via ssh automagically, but in some circumstances this magic fails.

8
  • 2
    still doesn't work :(
    – gopi1410
    Aug 14, 2012 at 17:49
  • What terminal are you using? And what the value of TERM you have tried?
    – paul
    Aug 15, 2012 at 6:23
  • I tried vt100. How do I get to know the value of TERM? I tried ssh from cygwin (in windows) to Debian. If there are problems with cygwin, I even tried it from ubuntu terminal, but same problem.
    – gopi1410
    Aug 15, 2012 at 12:30
  • Not sure cygwin has good terminal emulation, as far as I know, cygwin just using regular windows console which lacks terminal emulation (may be wrong). From ubuntu all should works just fine. Something wrong with shell or termcap library on debian. Login from Ubuntu and give me output of commands: 1. echo $TERM 2. stty -a
    – paul
    Aug 16, 2012 at 4:13
  • 3
    While this didn't directly help me, since TERM was correctly set, it did help identify the problem. I was missing the terminfo entries for my terminal. I am using urxvt and on arch you need to install rxvt-unicode-terminfo to fix this particular issue
    – Xandaros
    Nov 13, 2015 at 11:32
63

I have seen such problems before.

Take the backspace for example, the remote host expects some character to be used as "erase/backspace" , while you pressing backspace in the terminal , the terminal program will send some character to the remote host, if what the remote host expects diffs with the characters sent by the terminal program, you would encounter this issue. So a quick fix is as below:

  1. run command #stty -a in the remote host, and find what is expected to be an erase code in the output. Say erase=^?.
  2. In the terminal, press Ctrl + v and press your backspace. You'll see what code is sent as "erase". Say it is ^H.
  3. In the remote host, run #stty erase ^H.
    (Note: use Ctrlv + Backspace, do not type the ^ manually)

You can fix the Tab issue with the same as above.

5
  • My backspace character is some special symbol something like an inverted triangle. So its not working
    – gopi1410
    Jul 16, 2012 at 10:26
  • 1
    This simple comment is certainly the best solution I have seen to this. It feels like a kludge but it works. Aug 13, 2015 at 8:08
  • This worked for me! What I don't understand though is why my shell handles backspace as expected, but reading from stdin in my app does not. Doing your suggested change results in backspace working for both. My understanding is that the shell handles stdin buffering, so shouldn't the backspace-handling be the same for both the shell and an app?
    – Samuel
    Aug 18, 2015 at 22:24
  • 1
    What if no code at all is sent? Ctrlv + backspace produces nothing...
    – Manfredo
    Jul 31, 2017 at 8:24
  • 2
    For me, the backspace code is the same as stty -a output, but still does not work
    – Matej J
    Nov 15, 2019 at 13:04
28

This is because your default shell is sh, to use bash, just run bash from your shell.

bash

To set bash as your default shell:

chsh -s /bin/bash 

or

sudo chsh -s /bin/bash yourusername
2
  • 3
    This one fixed it for me. Never even though to look at my login shell!
    – James Pack
    Jul 3, 2017 at 17:19
  • 3
    I am seeing this problem, but my shell is bash.
    – fraxture
    Jul 29, 2019 at 0:22
8

I usually use this to fix any funky character output in my terminal. It resets all the special characters to their default values.

stty sane

From the stty man page:

same as cread -ignbrk brkint -inlcr -igncr icrnl -iutf8 -ixoff -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke, all special characters to their default values

0
7

Your shell may be set to /bin/sh instead of /bin/bash

4
  • 1
    Related to this answer: unix.stackexchange.com/questions/50542/…
    – Samuel
    Sep 3, 2015 at 18:36
  • 1
  • 1
    This would be more helpful if it explained a little more and how to change it, but it was the problem I had. Thanks @Tom for your link that fixed the issue for me.
    – Jake
    Mar 28, 2016 at 14:40
  • This was the reason in my case.. for that particular user. No shell was defined in /etc/passwd, when I added :/bin/bash to his line it worked. You can find out what shell is being used with echo $0
    – MSpreij
    Sep 4, 2019 at 20:51
5

Verify that the remote host shell TERM setting matches your terminal's term setting (localhost) and that the remote host supports the TERM setting.

E.g.:

  1. From iTerm (or another terminal app)
$ echo $TERM
xterm-256color

You don't need to open tmux. I am using tmux to force the error since it uses an relatively uncommon terminal config.

  1. Open tmux (basically another terminal within a terminal app)
$ echo $TERM
tmux-256color
  1. From tmux run ssh command & check TERM setting
$ ssh <remote host>
.
.
# Logged into remote host now run...
$ echo $TERM
tmux-256color

So tmux communicated to the remote host properly but we have backspace or some other matching key issue. Lets check which terminals the remote host supports.

  1. From remote host
$ toe -a
xterm-256color  xterm with 256 colors
xterm-color     generic "ANSI" color xterm (X Window System)
xterm           X11 terminal emulator
linux           linux console
dumb            80-column dumb tty
ansi            ansi/pc-term compatible with color

Looks like the remote host does not support tmux-256color, the TERM value sent by tmux. It does support xterm-256color which we know our terminal app supports.

  1. From remote host change TERM value
export TERM=xterm-256color
2
  • 1
    The question doesn't mention tmux so although the setting of $TERM might be the issue, fixing it in tmux probably isn't so useful Feb 3, 2023 at 7:03
  • Your answer could be improved by explaining why your solution correctly would resolve it for the user in the question. Note that tmux is not mentioned by the user.
    – Kusalananda
    Feb 3, 2023 at 7:32
4

on top of paul's answer, if you want to retain the ability to have colorful output, you can use xterm-88color

echo 'export TERM=xterm-88color' >> ~/.bashrc
source ~/.bashrc
2
  • 3
    toe -a shows a list of supported terminal types ... xterm-88color is by far not the only option and in fact may be unavailable on particular systems. Oct 16, 2019 at 19:31
  • In my case, I had to switch from xterm-256color to xterm-88color and it is far better than other options.
    – рüффп
    Jul 5, 2021 at 8:06
1

I followed the advice in this old reddit post:

I copied the .terminfo folder from my user directory to the .terminfo folder under my root user directory. (I had the problem specifically when doing su -i)

For some reason the color terminal setting I was using was present under my regular user ie file called .terminfo/x/xterm-24bit, but not under the root user.

1

I encountered a similar issue, that when ssh to a remote machine from tmux, my Backspace key starts to generate spaces instead. If this is your case, check in the tmux config for:

set -g default-terminal "screen-256color"

The above would set the environment variable TERM to "screen-256color", which would be used by the SSH client to communicate with the remote machine. After establishing the connection, the remote shell would contain the same $TERM value, while setting it to an unsupported value is one of the reason why Backspace stops working.

In my case, the remote machine doesn't support "screen-256color":

$ toe -a
xterm-xfree86   xterm terminal emulator (XFree86)
xterm-vt220     xterm emulating vt220
xterm-r6        xterm X11R6 version
xterm-r5        xterm R5 version
xterm-mono      monochrome xterm
xterm-color     generic color xterm
xterm-256color  xterm with 256 colors
xterm           xterm terminal emulator (X Window System)
wsvt25m         NetBSD wscons in 25 line DEC VT220 mode with Meta
vt100           dec vt100 (w/advanced video)
linux           linux console

so either modifying the tmux config or setting directly export TERM="xterm-256color" would solve the problem.

2
  • 1
    Your answer could be improved by explaining why your solution correctly would resolve it for the user in the question. Note that tmux is not mentioned by the user.
    – Kusalananda
    Feb 3, 2023 at 7:31
  • 1
    @roaima @Kusalananda Thanks for pointing it out that OP didn't mention tmux. I've modified my answer to not assume it's certainly a tmux issue and added more info on the relationship of SSH and $TERM.
    – davidhcefx
    Feb 4, 2023 at 20:01
0

After connecting from Fedora to an OpenWrt host with ssh the remote bash
terminal had problems with some control characters. TERM variable in source
host was set to rxvt-unicode-256color. Since it seemed that OpenWrt repos did
not provide terminfo file for that terminal I copied rxvt-unicode-256color
terminfo file from source host to remote host directory:
/usr/share/terminfo/r/ .
It worked.

-1

Update:

Thanks for pointing out the shortcomings of my previous answer.

The user's problem with the terminal in a remote machine and the problem I faced in a docker container may be caused by the same issue, which is related to the terminfo (about terminfo).

  • The USERA account works well in that machine may set up terminfo correctly while another USERB account doesn't. So using USERA's terminfo may fix this problem.

But, where is the terminfo?

  • Here is what I found in a ubuntu:20.04 docker container:
root@9a6c3b2bf6ea:/etc/terminfo# cat README
This directory is for system-local terminfo descriptions. By default,
ncurses will search ${HOME}/.terminfo first, then /etc/terminfo (this
directory), then /lib/terminfo, and last not least /usr/share/terminfo.

Solution:

  1. Check the following locations and find the terminfo you need:
    • ${HOME}/.terminfo (associated with accounts, check this one first)
    • /etc/terminfo
    • /lib/terminfo
    • /usr/share/terminfo
  2. Copy the terminfo from USERA to USERB
  3. Open a new session to refresh your terminal

Original Answer:

I encountered this problem when I tried to add bash and use it in a distroless image. (Even though this behavior is unsafe ...)

I build my application image with the multi-stages build. I run:

docker run -it <my_image> bash

And I found that <backspace> doesn't work.

Solution:

In my Dockerfile:

COPY --from=builder /lib/terminfo /lib/terminfo
2
  • 1
    This answer is specific to running is a Docker container and will not help in the OP's environment.
    – doneal24
    Feb 2, 2023 at 20:33
  • Welcome to Unix & Linux! Your answer could be improved by explaining the issue, i.e., the underlying cause of the problem and why your solution correctly would resolve it for the user in the question. Note that Docker is not mentioned by the user.
    – Kusalananda
    Feb 3, 2023 at 7:31

You must log in to answer this question.

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