Linux 终端美化

1. ZSH

1.1. 安装zsh

zsh是一个强大的shell,具有很多强大的功能,如自动补全、语法高亮、命令提示等。

zsh官网:https://www.zsh.org/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看系统存在的shell
cat /etc/shells

# 查看当前使用的shell
echo $SHELL

# 安装zsh
sudo apt install -y zsh

# 切换默认shell,重启终端生效(SSH则断开重连)
chsh -s $(which zsh)

# 确认是否切换成功
echo $SHELL

1.2. 安装oh-my-zsh

oh-my-zsh是zsh的插件管理器,可以方便的安装各种主题和插件

1
2
3
4
5
6
7
sudo apt install git -y
sh -c "$(curl -fsSL https://mirror.ghproxy.com/https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 下载失败可以尝试以下命令
curl -Lo oh-my-zsh.sh https://blog.lololowe.com/file/oh-my-zsh.sh
chmod 755 oh-my-zsh.sh
./oh-my-zsh.sh

1.3. 安装p10k主题和命令插件

powerlevel10k是一款灵活,开箱即用的zsh主题

1
2
3
4
5
6
7
8
9
10
11
# powerlevel10k主题
git clone https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
# zsh-autosuggestions自动提示插件
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# zsh-syntax-highlighting语法高亮插件
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

# 下载失败可以尝试以下命令
git clone https://ghproxy.net/https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
git clone https://ghproxy.net/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://ghproxy.net/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

1.4. 安装字体

p10k主题自带的一些图标需要特殊字体支持,推荐使用MesloLGS字体

如果使用SSH客户端连接SSH服务器,则SSH客户端也需要安装字体,否则无法正常显示SSH服务器的图标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 下载字体
wget https://mirror.ghproxy.com/https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Regular.ttf &&
wget https://mirror.ghproxy.com/https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold.ttf &&
wget https://mirror.ghproxy.com/https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Italic.ttf &&
wget https://mirror.ghproxy.com/https://github.com/romkatv/powerlevel10k-media/raw/master/MesloLGS%20NF%20Bold%20Italic.ttf

# 安装字体
sudo mkdir /usr/share/fonts/truetype/MesloLGS
sudo mv *.ttf /usr/share/fonts/truetype/MesloLGS

# 安装fontconfig工具
sudo apt install fontconfig

# 刷新字体缓存
fc-cache -fv

# 测试字体显示
echo $'\uf115' # 能打印出文件夹图标则成功

1.5. 应用主题和插件

1
2
3
4
5
6
7
8
9
10
11
12
# 编辑配置文件,修改主题和插件
vim ~/.zshrc

# 修改主题
ZSH_THEME="powerlevel10k/powerlevel10k"

# 启用插件
plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
)

编辑并保存后,重启终端即可进入p10k主题的交互式配置向导,也可以执行p10k configure命令进行配置。

2. Tmux 状态栏

Tmux 是一个终端复用器,具体用法可以参考阮一峰大佬的文章:https://www.ruanyifeng.com/blog/2019/10/tmux.html

Tmux提供了一个状态栏,通过配置Tmux的状态栏,可以使其在终端的顶部实时显示各项硬件参数,如网速、温度、功率、硬件利用率等,如下图:

alt text

嫌Tmux配置麻烦可以去用byobu,启动byobu后直接按F9并选择 “Toggle status notifications”,即可自定义底部状态栏。

2.1. 安装Tmux

1
2
apt update
apt install tmux sysstat ifstat lm-sensors -y

2.2. 编写脚本

创建5个获取硬件信息的脚本,并放在/root/shell_scripts目录下,5个脚本内容分别如下:

2.2.1. CPU利用率获取脚本

cpu_usage.sh

1
2
#!/bin/bash
mpstat 1 1 | awk 'NR==4 {printf "CPU: %.1f%%", 100 - $12}'

2.2.2. 内存利用率获取脚本

memory_usage.sh

1
2
#!/bin/bash
free | awk 'NR==2 {printf "Mem: %.1f%%", ($2 - $4)/$2 * 100.0}'

2.2.3. 上下行网速获取脚本

net_speed.sh

1
2
#!/bin/bash
ifstat -S 1 1 | awk 'NR==3 {printf "D: %.2f MB/s | U: %.2f MB/s", $1 / 1024, $2 / 1024}'

如果上行网速获取不对,可以修改脚本内容为以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 计算当前网络流量                                                                                                                                                                 for interface in "${network_dir}"/*; do
interface=${interface##*/}
if [[ ! " ${excluded_interfaces[@]} " =~ " ${interface} " ]]; then
if [[ -d "${network_dir}/${interface}/statistics" ]]; then
rx_bytes=$(cat "${network_dir}/${interface}/statistics/rx_bytes")
tx_bytes=$(cat "${network_dir}/${interface}/statistics/tx_bytes")
total_rx_bytes_current=$((total_rx_bytes_current + rx_bytes))
total_tx_bytes_current=$((total_tx_bytes_current + tx_bytes))
fi
fi
done

# 从文件读取之前的流量数据 if [ -f "$stats_file" ]; then
read prev_time total_rx_bytes_previous total_tx_bytes_previous < "$stats_file"
interval=$((current_time - prev_time))

# 确保间隔大于0
if (( interval > 0 )); then
rx_bytes_delta=$(( total_rx_bytes_current - total_rx_bytes_previous )) tx_bytes_delta=$(( total_tx_bytes_current - total_tx_bytes_previous ))

# 计算每秒的流量 rx_bytes_per_sec=$(( rx_bytes_delta / interval ))
tx_bytes_per_sec=$(( tx_bytes_delta / interval ))
else
rx_bytes_per_sec=0
tx_bytes_per_sec=0
fi
else
# 初次运行时
rx_bytes_per_sec=0
tx_bytes_per_sec=0
fi

# 保存当前数据
echo "${current_time} ${total_rx_bytes_current} ${total_tx_bytes_current}" > "$stats_file"

# 输出结果,除以2
printf "D: %.2f MB/s | U: %.2f MB/s\n" "$(echo "scale=2; $rx_bytes_per_sec / 2 / 1024 / 1024" | bc)" "$(echo "scale=2; $tx_bytes_per_sec / 2 / 1024 / 1024" | bc)"

2.2.4. CPU+GPU功率获取脚本

power_usage.sh

1
2
3
#!/bin/bash
sleep 1
sensors | grep -i "PPT" | awk '{print "PPT: " $2}' | sed 's/W//'

2.2.5. CPU和GPU温度获取脚本

temperature.sh

1
2
3
4
5
#!/bin/bash
cpu_temp=$(sensors | grep 'Tctl' | awk '{print $2}')
gpu_temp=$(sensors | grep 'edge' | awk '{print $2}')

echo "CPU: $cpu_temp, GPU: $gpu_temp"

创建完5个脚本之后,编辑Tmux配置文件~/.tmux.conf,将5个脚本添加到Tmux状态栏配置中,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
set -g status on
set -g status-interval 2
set -g status-position top
# set -g base-index 1
# set -g pane-base-index 1
set -g status-bg colour8 # '#61AFEF'
set -g status-fg white
set -g mouse on # 允许滚轮滚动屏幕,但是使用右键复制的时候要加shift
# set -g status-justify centre # 中间对齐
# set -g status-left-length 30 # 左侧长度
set -g status-right-length 120 # 右侧长度
# set -g default-shell /usr/bin/zsh # 默认shell

set -g status-right '#(/root/shell_scripts/net_speed.sh) | #(/root/shell_scripts/temperature.sh) | #(/root/shell_scripts/power_usage.sh) | #(/root/shell_scripts/memory_usage.sh) | #(/root/shell_scripts/cpu_usage.sh) | #(date +"%Y-%m-%d %H:%M")'

保存配置文件后,使用 tmux source-file ~/.tmux.conf 命令重载配置即可看到状态栏。

为了进入终端后能自动启动Tmux,可以编辑~/.zshrc文件,追加以下内容:

1
2
3
4
# 检查是否已经在 tmux 会话中,如果没有,则启动 tmux
if command -v tmux>/dev/null; then
[ -z "$TMUX" ] && exec tmux
fi

3. 下一代ls命令

lsd是一个更美观的ls命令,可以替代默认的ls命令,如下图:

alt text

注意:安装lsd之前需要参考上文安装第三方字体,否则lsd的图标会显示为乱码。

安装:

1
2
3
4
5
6
wget https://github.com/lsd-rs/lsd/releases/download/v1.1.5/lsd-musl_1.1.5_amd64_xz.deb -o /tmp/lsd.deb
# 下载失败可以尝试使用以下命令
wget https://ghproxy.net/https://github.com/lsd-rs/lsd/releases/download/v1.1.5/lsd-musl_1.1.5_amd64_xz.deb -O /tmp/lsd.deb

sudo apt install /tmp/lsd.deb
lsd -V

编辑配置文件,修改默认的ls命令为lsd

1
2
echo "alias ll='lsd -lh'\nalias la='lsd -alh'" >> ~/.zshrc
source ~/.zshrc

由于将其它命令写在了p10k的配置命令下面,会导致登陆终端时出现警告,因此需要执行以下命令忽略警告

1
2
echo "typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet" >> ~/.zshrc
source ~/.zshrc

4. cat命令输出高亮

bat是一个带语法高亮的的cat命令,可以替代默认的cat命令,如下图:

alt text

安装:

1
apt install bat

编辑配置文件,修改默认的cat命令为batcat:

1
2
echo "alias cat=batcat" >> ~/.zshrc
source ~/.zshrc

自定义bat的配置文件:

1
2
3
echo --style=plain >>  ~/.config/bat/config  # 输出不带行号
echo --paging=never >> ~/.config/bat/config # 禁止分页,一次性输出整个文件
echo --color=always >> ~/.config/bat/config # 强制开启颜色高亮,即使输出被管道传递

5. fastfetch显示系统信息

fastfetch是一个持续维护、功能丰富、面向性能、类似 neofetch 的系统信息展示工具。效果如下图:

alt text

安装:

1
2
3
4
5
6
wget https://github.com/fastfetch-cli/fastfetch/releases/download/2.35.0/fastfetch-linux-amd64.deb -O /tmp/fastfetch.deb
# 下载失败可以尝试使用以下命令
wget https://ghproxy.net/https://github.com/fastfetch-cli/fastfetch/releases/download/2.35.0/fastfetch-linux-amd64.deb -O /tmp/fastfetch.deb

sudo apt install /tmp/fastfetch.deb
fastfetch -V

编辑配置文件,使用户登陆终端时显示系统信息

1
echo "fastfetch --pipe false" >> ~/.zshrc

修改显示的信息,vim 编辑 ~/.config/fastfetch/config.jsonc 文件,写入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{
"$schema": "https://github.com/fastfetch-cli/fastfetch/raw/dev/doc/json_schema.json",
"logo": {
// "type": "builtin",
// "source": "windows",
"height": 18,
"padding": {
"top": 1
}
},
"display": {
"separator": " : "
},
"modules": [
{
// https://www.nerdfonts.com/cheat-sheet
"type": "custom",
"format": "\u001b[36m  OS Info"
},
{
"type": "custom",
"format": "┌────────────────────────────────────────────────────────┐"
},
{
"type": "os",
"key": "  OS",
"keyColor": "red"
},
{
"type": "kernel",
"key": " 󰻹 Kernel",
"keyColor": "red"
},
{
"type": "host",
"key": "  Machine",
"format": "{name}{?vendor}",
"keyColor": "red"
},
// {
// "type": "display",
// "key": " 󰹑 Display",
// "format": "{1}x{2} @ {3} Hz",
// "keyColor": "red"
// },
{
"type": "uptime",
"key": " 󱫐 Uptime ",
"keyColor": "red"
},
{
"type": "shell",
"key": "  Shell",
"keyColor": "red"
},

{
"type": "custom",
"format": "└────────────────────────────────────────────────────────┘"
},
"break",
{
"type": "title",
"key": " "
},
{
"type": "custom",
"format": "┌────────────────────────────────────────────────────────┐"
},

{
"type": "cpu",
"format": "{1}",
"key": "  CPU",
"keyColor": "blue"
},
{
"type": "gpu",
"key": " 󰋵 GPU",
"keyColor": "blue"
},

{
"type": "disk",
"key": "  Disk",
"keyColor": "green"
},
{
"type": "memory",
"key": "  Memory",
"keyColor": "green"
},
{
"type": "swap",
"key": " 󰿣 Swap",
"keyColor": "green"
},
{
"type": "bluetooth",
"key": "  Bluetooth",
"keyColor": "magenta"
},
{
"type": "wifi",
"key": "  WiFi",
"keyColor": "magenta"
},
{
"type": "localip",
"showIpv6": false,
"showMac": false,
"showSpeed": false,
"showMtu": false,
"showLoop": false,
"showFlags": false,
"showAllIps": false,
"key": " 󱦂 LAN",
"keyColor": "magenta"
},
{
"type": "publicip",
"timeout": 500,
"url": "http://4.ipw.cn",
"key": " 󰩟 WAN",
"keyColor": "magenta"
},
{
"type": "dns",
"key": " 󰮌 DNS",
"keyColor": "magenta"
},

{
"type": "custom",
"format": "└────────────────────────────────────────────────────────┘"
},
{
"type": "colors",
"paddingLeft": 2,
"symbol": "circle"
},
"break"
]
}

6. 删除登录提示

删除最后一次登录ssh的提示信息:

alt text

1
2
sudo sed -i 's/#PrintLastLog yes/PrintLastLog no/g' /etc/ssh/sshd_config
sudo systemctl restart sshd

删除登录提示信息:

alt text

1
2
3
# 编辑/etc/pam.d/sshd文件,注释以下两行
session optional pam_motd.so motd=/run/motd.dynamic
session optional pam_motd.so noupdate

重启sshd服务:

1
sudo systemctl restart sshd

7. 中文语言环境

向导式配置:dpkg-reconfigure locales

手动配置:

1
2
sudo sed -i 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/g' /etc/locale.gen
locale-gen # 生成语言环境

vim 编辑 /etc/default/locale 文件,替换为以下内容:

1
2
3
#  File generated by update-locale
LANG=zh_CN.UTF-8
LANGUAGE="zh_CN:zh"

载入语言环境:

1
2
source /etc/default/locale
date # 显示中文日期则表示成功

vim 显示中文(解决中文乱码问题):

1
2
3
4
5
# 编辑 /etc/vim/vimrc 文件,追加以下内容
set nu
set termencoding=utf-8
set encoding=utf8
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030

8. 上海时区

1
2
3
timedatectl list-timezones | grep Asia  # 查看亚洲所有时区
sudo timedatectl set-timezone Asia/Shanghai # 设置上海时区
date && timedatectl # 查看时间

9. 实用终端工具推荐

10. 参考