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

命令提示符组成

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

有关内容如下:

...
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    #xterm-color) color_prompt=yes;; #增加|*-256color可激活彩色显式
    xterm-color|*-256color) color_prompt=yes;;
esac

# 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主机第一个名字
\t24小时时间,HH:MM:SS
\T12小时时间
\A24小时时间,HH:MM
\u当前用户名
\vbash版本信息
\w完整工作目录
\W利用basename获取工作目录名称,所以只会显示最后一个目录
#下达的第几个命令
$提示字符,如果是root时,提示符为:# ,普通用户则为:$

颜色

PS1颜色表
颜色前景背景
3040
3141
绿3242
3343
3444
紫红3545
青蓝3646
3747
透明11

格式: \[\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 ln -s /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 //非常重要,如果没有这一步的话,后面时间还是不准
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值