为什么命令行上没有显示名称?

希望这是一个简单的问题,我不知道为什么在添加新账户并登录时$后面没有用户名?
 Welcome to Ubuntu 12.04.3 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/

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

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

You have mail.
$

看一下bash配置文件,包括.bashrc。你是怎么登录的?通过ssh吗? - Panther
这里使用哪个shell?你如何以用户身份登录?通过TTY还是可能通过su user命令?[ -f ~/.bashrc ] && cat ~/.bashrc || echo no .bashrc的输出是什么? - user54813
3个回答

如果您使用useradd添加新帐户,那么它很可能将新用户的登录shell设置为/bin/sh,在Ubuntu中,/bin/sh是指向dash shell的符号链接。Dash是一个更简单的Shell,不会读取~/.bashrc文件,并且不会设置user@host命令行提示符。您可以通过查看/etc/passwd文件或使用以下方法进行检查。

getent passwd username

并且你可以使用以下命令将默认shell更改为更常见的bash

chsh -s /bin/bash

如果你已经以要更改其shell的用户身份登录,或者

sudo chsh -s /bin/bash username

更改另一个帐户的登录Shell。为了防止再次发生这种情况,您可以在useradd命令行上使用-s--shell选项指定登录Shell,或者改用更高级的实用程序adduser

基本的Bash提示符只是一个名为PS1的变量。这个变量通常在~/.bashrc文件中设置。当bash shell启动时,它会读取该文件并设置该变量。如果PS1变量没有在.bashrc(或.profile)文件中设置,则您将没有提示符。在您的情况下,PS1变量被设置为$:
export PS1="\$"

你可以尝试对变量进行实验,例如尝试:

  • export PS1="\u\$"
  • export PS1="\u@\h\$"

你将会看到提示符的变化。根据你想要显示的提示方式编辑你的bashrc文件。欲了解更多信息,请访问这里


如在另一个问题中所见,这对我解决了问题。 https://askubuntu.com/a/1064774/212948

As shown on this Ubuntu wiki page, you should run

sudo dpkg-reconfigure dash

and select the option not to use /bin/dash as the default shell. Making the change this way will not only fix the symlink /bin/sh, but will also configure other parts of your system to behave correctly, such as making sure that man sh points to the bash manpage instead of the dash one.