SSH Login "clear screen" before MOTD is displayed

4

2

I'm trying to clear the screen contents before the MOTD is displayed so that when you SSH onto the server the MOTD header is at the top of your screen, not the previous commands ran on your local machine.

I have tried adding clear to .bashrc but it clears the MOTD as well.

Example: clear everything above the "Welcome to Ubuntu...."

Screenshot

Question: How can remove all the contents before the MOTD message is displayed?

Jordan Davis

Posted 2017-01-19T21:16:53.323

Reputation: 143

Answers

1

MOTD is displayed by the pam authentication module in modern linux distros, right after your login. No user script will run before that.

You can simply modify the motd file to clear the screen via terminal escape sequences. <esc>[2J<esc>[;H will clear the screen and jump up. Open your motd file with vim, press i (insert mode), press ctrl+v, press the esc button. You will see ^[. You just typed the ascii 27 escape character. Type [2J then ctrl-v esc again then [;H. Now press esc (without ctrl-v, this is to escape the insert mode in vi) then press shift+Z two times.

Your screen will be cleared on every vt100 compatible terminals.

Your another option is to disable motd in pam. Search for the pam_motd.so in the /etc/pam.d directory: grep pam_motd /etc/pam.d/* . Most possibly you will find something like

 session    optional   pam_motd.so

Comment it out with #. Implement your own motd (cat the /etc/motd file in the init script of your choice). Of course now you can clear before cat.

goteguru

Posted 2017-01-19T21:16:53.323

Reputation: 191

1so I just tried editing my /etc/motd file and added [2J[;H to the top of it and it just appeared as text on in the MOTD. – Jordan Davis – 2017-01-19T22:07:04.830

If you (or somebody else reading this) don't like vim you can use the following: echo -e \e[2J\e[;H > motd2 && cat /etc/motd>>motd2 after that you will get a new motd2 (with clearscreen) which can be moved over the original. – goteguru – 2017-01-19T22:12:50.510

I use vim so it's good. One other quick question though. Could I add that to the 00-header file so that the "Welcome to Ubuntu...." info and the package updates aren't cleared out? – Jordan Davis – 2017-01-19T22:14:18.080

You can add it anywhere, its a terminal escape sequence. However if you modify system-managed files, the package manager may overwrite your version in the future. (I'm on gentoo, but ubuntu may warn you I don't know). – goteguru – 2017-01-19T22:17:04.713

Do I need to echo it in the bash script since its not text-based like the /etc/motd I tried at the bottom of the commented lines in the 00-header file, and isn't working. – Jordan Davis – 2017-01-19T22:29:16.357

If you want echo-magic you can use echo -e '\e[2J\e[;H' without vim. But if you modify the shell script anyway (which might be overwritten) you can use the simple clear command. – goteguru – 2017-01-19T22:57:19.903

Yeah I ended up just doing echo -e "[2J[;H" in the 00-header it won't get overwritten there so should be good. – Jordan Davis – 2017-01-19T23:09:08.177

2

Edit (or create) your ~/.ssh/config file and add the lines

PermitLocalCommand yes
LocalCommand /usr/bin/clear

near the top. This runs clear on the client (i.e., your local machine) after a successful connection to the server.

jjlin

Posted 2017-01-19T21:16:53.323

Reputation: 12 964

I like it and will result to if nothing, but I'm managing the server multiple users so I'm trying to get it for everyone not must myself. – Jordan Davis – 2017-01-19T22:02:42.747