bash SSH登录后如何清除终端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28874593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:28:45  来源:igfitidea点击:

How to clear terminal after SSH login?

bashsshterminalclear

提问by prankousky

I am trying to connect to a server via ssh. Once connected, terminal should be cleared.

我正在尝试通过 ssh 连接到服务器。连接后,应清除端子。

Due to generated keys, I can connect to the server via ssh usr@svrwithout being prompted a password. This works.

由于生成了密钥,我可以通过连接到服务器ssh usr@svr而不会提示输入密码。这有效。

In order to get rid off

为了摆脱

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.

Debian GNU/Linux 系统中包含的程序是免费软件;每个程序的确切分发条款在 /usr/share/doc/*/copyright 中的各个文件中进行了描述。

在适用法律允许的范围内,Debian GNU/Linux 不附带任何保证。

I would usually just type clear. However, I would prefer not to type this every time but automate the procedure instead.

我通常只会输入clear. 但是,我不希望每次都键入此内容,而是自动执行该过程。

ssh usr@svr "clear"--> "TERM environment variable not set.". I googled several solutions about unset environment variables, but without success.

ssh usr@svr "clear"--> “未设置 TERM 环境变量。”。我用谷歌搜索了几个关于未设置环境变量的解决方案,但没有成功。

So instead, I tried ssh -t usr@vr "clear"; this successfully clears the terminal, but also closes the connection right away ("Connection to IP closed."). Computer connects to server, clears the screen, closes the connection.

所以相反,我尝试了ssh -t usr@vr "clear"; 这成功地清除了终端,但也立即关闭了连接(“IP 连接关闭。”)。计算机连接到服务器,清除屏幕,关闭连接。

Next attempt was to create a bash script on the server to be run after connecting to it.

下一次尝试是在服务器上创建一个 bash 脚本,以便在连接到它后运行。

#/bin/bash
clear
## cl.sh, chmod +x

ssh usr@svr ./cl.sh--> "TERM environment variable not set.".

ssh usr@svr ./cl.sh--> “未设置 TERM 环境变量。”。

Another attempt was to create a bash script connecting to the server and clearing the terminal via ENDSSH.

另一种尝试是创建一个连接到服务器的 bash 脚本并通过ENDSSH.

#/bin/bash
ssh usr@svr <<'ENDSSH'
clear
ENDSSH
## sc.sh, chmod +x

Running this results in:

运行这个结果:

> ./sc.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Linux raspberrypi 3.18.7-v7+ #755 SMP PREEMPT Thu Feb 12 17:20:48 GMT 2015 armv7l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
TERM environment variable not set.

I am a beginner at this, so please be patient if I have made a very obvious mistake. I tried to be as detailed as possible and researched this before posting, but could not find an answer to my question. For example, commands other than "clear" work (ssh usr@svr ls), but that does not help me.

我是这方面的初学者,所以如果我犯了一个非常明显的错误,请耐心等待。我试图尽可能详细,并在发布之前对此进行了研究,但找不到我的问题的答案。例如,“清除”以外的命令有效 ( ssh usr@svr ls),但这对我没有帮助。

采纳答案by prankousky

I have found another easy solution

我找到了另一个简单的解决方案

ssh -t usr@svr 'clear;bash'

回答by Michael Jaros

The text your question refers to is part of the message-of-the-day (MOTD).

您的问题所指的文本是每日消息 (MOTD) 的一部分。

If you can become root on the server, you can just modify that message in /etc/motd. Note that depending on the server's distribution, this file will usually be generated somehow (overwriting any changes), e.g. on Debian it is generated from /etc/motd.tailat boot, so you might have to change that file instead.

如果您可以成为服务器上的 root,您只需在/etc/motd. 请注意,根据服务器的分布,该文件通常会以某种方式生成(覆盖任何更改),例如在 Debian 上,它是/etc/motd.tail在启动时生成的,因此您可能必须更改该文件。

See manpage motd(5).

请参阅联机帮助页motd(5)

回答by ccarton

To prevent that message from being printed you can create a file named .hushloginin your home directory (on the server). SSH to the server and run the command touch ~/.hushlogin. If that file exists then the login shell will no longer print the motd (message of the day) which is what you are seeing.

为了防止打印该消息,您可以.hushlogin在您的主目录(在服务器上)中创建一个文件。SSH 到服务器并运行命令touch ~/.hushlogin。如果该文件存在,则登录 shell 将不再打印您所看到的 motd(当天消息)。

回答by kenorb

All startup messages are defined in motdfile (/etc/motd).

所有启动消息都在motd文件 ( /etc/motd)中定义。

However if you'd like to having your console cleared to increase privacy, add the following code:

但是,如果您希望清除控制台以增加隐私,请添加以下代码:

 [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q

Either to your .bash_profileor .bash_logout(on logout). Or by using clearcommand.

要么到您的.bash_profile要么.bash_logout(在注销时)。或者通过使用clear命令。

Clearing screen on logout is the default behaviour on Debian Linux distribution.

注销时清除屏幕是 Debian Linux 发行版的默认行为。