Ubuntu命令提示符(含系统时间修改)

  • Post author:
  • Post category:其他




命令提示符组成

ubuntu默认提示符:

root@DESKTOP:~#

注释:

  • root: 当前登录用户
  • DESKTOP:主机名
  • ~: 当前目录名(即用户主目录)
  • #:超级用户权限(root用户显示)
  • $: 普通用户权限(非root用户显示)

可在终端打印默认提示符配置:

root@DESKTOP:~# echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$



修改命令提示符



参数

命令提示符的配置文件在用户主目录下的

.bashrc

root@DESKTOP:~# vim ~/.bashrc

有关内容如下:

...
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
# force_color_prompt=yes 取消这行注释可以激活彩色显示

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\A \u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\A \u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\A \u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

...

找到

PS1

变量,加入\t或\A就可显示时间。



PS1常用参数表

提示符 含义
\d 日期,格式:weekday MM DD
\H 主机全名
\h 主机第一个名字
\t 24小时时间,HH:MM:SS
\T 12小时时间
\A 24小时时间,HH:MM
\u 当前用户名
\v bash版本信息
\w 完整工作目录
\W 利用basename获取工作目录名称,所以只会显示最后一个目录
# 下达的第几个命令
$ 提示字符,如果是root时,提示符为:# ,普通用户则为:$



颜色



PS1颜色表

颜色 前景 背景
30 40
31 41
绿 32 42
33 43
34 44
紫红 35 45
青蓝 36 46
37 47
透明 1 1


格式:


\[\e[F;Bm]\w\[\e[m]


含义:

修饰目录名颜色,

\[\e[F;Bm]

为颜色设置,F为字体颜色,B为背景颜色,

\[\e[m]

为结束颜色显示


示例:

将工作目录修改为蓝色:

PS1="[\u@\H: \[\e[34;1m\]\w\[\e[m\]]\$"


注:

有时候

\e

写作

\033

,是采用了ANSI标准,此时F和B含义反转,且F位置有时候表示亮度控制:普通亮度0,高亮度1,闪烁2。可参考

ANSI的屏幕控制



日期

加入\d参数可显示本地时间:

Fri Mar 18 16:14 root@DESKTOP:~#

此时如果你的日期显示中文,可查看

LC_TIME

变量:

318 16:14 root@DESKTOP:~# cat /etc/default/locale | grep LC_TIME
LC_TIME="zh_CN.UTF-8"

修改

LC_TIME

变量为:

LC_TIME=en_US.UTF-8

退出重新登陆即可变成英文。


注:

  1. 更改时区:
Fri Mar 18 16:15 wq@DESKTOP:~$ sudo tzselect  #按提示操作

Fri Mar 18 16:16 wq@DESKTOP:~$ sudo timedatectl set-timezone Asia/Shanghai

若提示

wq@DESKTOP:~$ sudo tzselect
/usr/bin/tzselect: line 180: /usr/share/zoneinfo/iso3166.tab: No such file or directory
/usr/bin/tzselect: time zone files are not set up correctly

则安装tzdata

wq@DESKTOP:~$ sudo apt install tzdata

修改时区后,更新本地时区:

sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
  1. 修改时间
wq@DESKTOP:~$ sudo date -s {MM/DD/YY} //先修改日期,因为修改日期会将时刻置0
wq@DESKTOP:~$ sudo date -s {hh:mm:ss} //修改时间

在修改时间以后,修改硬件CMOS的时间

wq@DESKTOP:~$ sudo hwclock --systohc //非常重要,如果没有这一步的话,后面时间还是不准



版权声明:本文为wq_0708原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。