如何在Linux终端上隐藏主机名?

11

有没有一种方法可以在不更新/etc/hosts或相关文件的情况下隐藏终端上的主机名。

通常我们会在屏幕上看到服务器的详细信息,如下所示:

[root@ServernamE /]#
所以,我不希望在终端上显示主机名=ServernamE。这样做的原因是,我将记录一个Web会话。我知道我可以更改所需部分,但那需要花费时间并且取决于其他软件。
谢谢。

你使用的是哪个发行版和哪个 shell?你想要为 root 用户还是普通用户安装? - jobin
@Jobin,我使用的是Bash RHEL 5.7(Tikanga)。 - user3578858
3个回答

23

如果你只需要一个临时的解决方案(因为你在谈论屏幕录制),你可以设置PS1变量来更改提示符。

例如,如果你想要的提示符是:

$

然后在终端上将PS1变量设置为以下值:

Then set your PS1 variable as follows on the terminal:

返回:

Then在终端上将PS1变量设置为以下值:

export PS1='$ '
同样地,你可以将它设置为任何你想要的提示的样子。如果你想要显示路径,则将其设置为:

同样地,你可以将它设置为任何你想要的提示的样子。如果你想要显示路径,则将其设置为:

export PS1='\w '

如果您想要一种永久性的解决方案,可以在您的shell配置脚本中进行设置,这个脚本是您的~/.bashrc文件,如果您的shell是bash的话。


3

很多回答只是建议使用paste this PS1='\w....... '而没有告诉我们这些变量是什么,实际上我们只需要知道3个变量:\u = 用户名\h = 主机名\w = 当前目录,例如:

PS1='\u@\h:\w \$ '

将变成

john@mypc:/home/dir $

以下是完整列表:
Special prompt variable characters:
 \d   The date, in "Weekday Month Date" format (e.g., "Tue May 26"). 
 \h   The hostname, up to the first . (e.g. deckard) 
 \H   The hostname. (e.g. deckard.SS64.com)
 \j   The number of jobs currently managed by the shell. 
 \l   The basename of the shell's terminal device name. 
 \s   The name of the shell, the basename of $0 (the portion following the final slash). 
 \t   The time, in 24-hour HH:MM:SS format. 
 \T   The time, in 12-hour HH:MM:SS format. 
 \@   The time, in 12-hour am/pm format. 
 \u   The username of the current user. 
 \v   The version of Bash (e.g., 2.00) 
 \V   The release of Bash, version + patchlevel (e.g., 2.00.0) 
 \w   The current working directory. 
 \W   The basename of $PWD. 
 \!   The history number of this command. 
 \#   The command number of this command. 
 \$   If you are not root, inserts a "$"; if you are root, you get a "#"  (root uid = 0) 
 \nnn   The character whose ASCII code is the octal value nnn. 
 \n   A newline. 
 \r   A carriage return. 
 \e   An escape character (typically a color code). 
 \a   A bell character.
 \\   A backslash. 
 \[   Begin a sequence of non-printing characters. (like color escape sequences). This
      allows bash to calculate word wrapping correctly.
 \]   End a sequence of non-printing characters.

source


3

如果您经常需要暂时隐藏终端信息,可以在 .bashrc 中创建一个 bash 别名:

# hti - hide terminal info
alias hti='clear && export PS1="$ "'

使用别名代替:

$ hti

要获取终端信息,可以执行以下命令(你也可以为其创建别名):
exec "$BASH"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接