Ssh – corrupted lines display in ssh terminal

consoledebiandisplayssh

I have problem with my console drawing lines. The problem is when I connect through ssh to server everything draws ok. But when I use up arrow key to show last used commands it often leaves few characters after $ sign like (in bold is what stays after the prompt):

user@host:/var/www_vhosts/instalator-paczek$ cp -r instnano /etc/issue

What is even more irritating is what happens when I type more characters than console width. Cursor somehow goes back to the beginning of line and overwrites prompt:

/d/asd/as/d/asd/asd/as/d/asd/asdww_vhosts/instalator-paczek$ ls -la asdkasdasdasd/asdasdasd/asdasdasdasdasdas/dasdsdaas/d/asd/as/d/asd/as

I can't use any long commands because it overwrites beginning and I can't see if I typed everything correctly. Long command when pasted inside console works even if display is corrupted. It's like only display is corrupted but command itself is ok.

I have ubuntu 12.10 and use Konsole as terminal application. On server there is Debian GNU Linux 6.0

It only happens with this server other servers I connect, works good with Konsole. My coworkers use putty to connect to same server and they also have same problem. So it seem like problem is with server not terminal program.

On server there is no xterm package so I fought that it should install it to fix the problem. But even after I've installed this package same problem exists. Should I restart server after installing xterm? I prefer not to because I'm not server admin.

I need to add that it seems like resizing console window also changes stty on server:

user@host:~$ stty -a
speed 38400 baud; rows 57; columns 151; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel -iutf8
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
user@host:~$ stty -a
speed 38400 baud; rows 57; columns 172; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany -imaxbel -iutf8
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

I tried already solutions including using resize and reset commands but they didn't help.

How to fix this?

I've asked same question on Unix & Linux but no solution found, so I'm writing here.

Best Answer

OK finally I found solution. The problem was that $PS1 didn't have color sequences enclosed with \[ and \]. Before correcting $PS1 was:

export PS1='\e[1;32m\u@\h:\w$ \e[m'

after fix:

export PS1='\[\e[1;32m\]\u@\h:\w$ \[\e[m\]'

I found solution here: https://stackoverflow.com/questions/2024884/commandline-overwrites-itself-when-the-commands-get-to-long