俗话说的好,工欲善其事必先利其器,要想活干得漂亮,就得有一个趁手的兵器。对于运维人来说,一个好的 Terminal,绝对可以大大滴提高工作效率。

曾经的我是 Ubuntu 的无脑吹、脑残粉,主要是命令行配合着一众工具使用起来非常丝滑,效率极高。而在 Windows 上除了安装各种执行环境配置环境变量外,还需要借助 xShell 这类工具来完成我的日常工作,虽然当时也折腾过一段时间的 cmder,但却是没有原生 Terminal 顺滑。

好在巨硬还有良心,推出了 Windows TerminalWSL,一度让我从 Ubuntu 脑残粉变成了 Windows 无脑吹~本文则是用来记录一下我自己的终端配置,因为我的工作环境几乎是跑在 WSL 上的,所以 Linux 和 MacOS 均可借鉴。

前置操作

  • 一个 Windows 11 的操作系统

  • 电脑开启虚拟化技术(默认已开启,未开启的则需要进 BIOS 中开启该设置,不同品牌的电脑不一样,请自行 Google)

  • 安装 Windows Terminal(Windows 11 默认已安装,否则请在 Microsoft Store 中进行安装)

Windows Terminal 配置

这里再次强推 Windows 用户使用 Windows Terminal,如同别人推荐在 Mac 中使用 iTerm2, 在 Linux 中使用 Terminator 一样(虽然我用 Ubuntu 的时候也不喜欢用 Terminator 😂)。
当然近年来也有很多好评如潮的开源终端(如 TabbyWindTerm 等),但是秉承着多一事不如少一事的原则,既然 Windows 自带了如此好用的终端,就不费力去折腾别的东西了~

对于终端来说,需要配置的内容无非就是 配色、字体、快捷键、外观,这里记录一下我当前使用的一些配置:

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": 
    [
        {
            "command": "paste"
        },
        {
            "command": "unbound",
            "keys": "ctrl+v"
        },
        {
            "command": 
            {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": 
            {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{2d17c16e-af5c-4ccf-bcca-796cc19e890b}",
    "newTabMenu": 
    [
        {
            "type": "remainingProfiles"
        }
    ],
    "profiles": 
    {
        "defaults": 
        {
            "colorScheme": "Tango Dark",
            "font": 
            {
                "face": "Maple Mono NF"
            },
            "opacity": 80,
            "useAcrylic": true
        },
        "list": 
        [
            {
                "font": 
                {
                    "face": "Maple Mono NF CN"
                },
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell",
                "opacity": 90,
                "useAcrylic": true
            },
            {
                "font": 
                {
                    "face": "Maple Mono NF CN"
                },
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "\u547d\u4ee4\u63d0\u793a\u7b26",
                "opacity": 90,
                "useAcrylic": true
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "adjustIndistinguishableColors": "indexed",
                "antialiasingMode": "grayscale",
                "colorScheme": "Tango Dark",
                "commandline": "C:\\Windows\\system32\\wsl.exe -d Ubuntu-20.04",
                "experimental.retroTerminalEffect": false,
                "font": 
                {
                    "face": "Maple Mono NF",
                    "size": 12.0,
                    "weight": "semi-light"
                },
                "guid": "{2d17c16e-af5c-4ccf-bcca-796cc19e890b}",
                "hidden": false,
                "historySize": 10240,
                "icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
                "name": "Ubuntu-20.04",
                "opacity": 80,
                "startingDirectory": "~",
                "useAcrylic": true
            },
            {
                "font": 
                {
                    "face": "Maple Mono NF"
                },
                "guid": "{4dd1e689-b517-5f39-947d-78e8a8bdf958}",
                "hidden": false,
                "name": "Ubuntu 20.04.6 LTS",
                "source": "CanonicalGroupLimited.Ubuntu20.04LTS_79rhkp1fndgsc"
            },
            {
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "PowerShell",
                "source": "Windows.Terminal.PowershellCore"
            }
        ]
    },
    "schemes": 
    [
        {
            "background": "#0C0C0C",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#012456",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell Powershell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#282C34",
            "black": "#282C34",
            "blue": "#61AFEF",
            "brightBlack": "#5A6374",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B6C2",
            "brightGreen": "#98C379",
            "brightPurple": "#C678DD",
            "brightRed": "#E06C75",
            "brightWhite": "#DCDFE4",
            "brightYellow": "#E5C07B",
            "cursorColor": "#FFFFFF",
            "cyan": "#56B6C2",
            "foreground": "#DCDFE4",
            "green": "#98C379",
            "name": "One Half Dark",
            "purple": "#C678DD",
            "red": "#E06C75",
            "selectionBackground": "#FFFFFF",
            "white": "#DCDFE4",
            "yellow": "#E5C07B"
        },
        {
            "background": "#FAFAFA",
            "black": "#383A42",
            "blue": "#0184BC",
            "brightBlack": "#4F525D",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B5C1",
            "brightGreen": "#98C379",
            "brightPurple": "#C577DD",
            "brightRed": "#DF6C75",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#E4C07A",
            "cursorColor": "#4F525D",
            "cyan": "#0997B3",
            "foreground": "#383A42",
            "green": "#50A14F",
            "name": "One Half Light",
            "purple": "#A626A4",
            "red": "#E45649",
            "selectionBackground": "#FFFFFF",
            "white": "#FAFAFA",
            "yellow": "#C18301"
        },
        {
            "background": "#002B36",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#FFFFFF",
            "cyan": "#2AA198",
            "foreground": "#839496",
            "green": "#859900",
            "name": "Solarized Dark",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#FDF6E3",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#002B36",
            "cyan": "#2AA198",
            "foreground": "#657B83",
            "green": "#859900",
            "name": "Solarized Light",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#FFFFFF",
            "cyan": "#06989A",
            "foreground": "#D3D7CF",
            "green": "#4E9A06",
            "name": "Tango Dark",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#FFFFFF",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#000000",
            "cyan": "#06989A",
            "foreground": "#555753",
            "green": "#4E9A06",
            "name": "Tango Light",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#300A24",
            "black": "#171421",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#08458F",
            "brightCyan": "#2C9FB3",
            "brightGreen": "#26A269",
            "brightPurple": "#A347BA",
            "brightRed": "#C01C28",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#A2734C",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#FFFFFF",
            "green": "#26A269",
            "name": "Ubuntu-20.04-ColorScheme",
            "purple": "#881798",
            "red": "#C21A23",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#A2734C"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#000080",
            "brightBlack": "#808080",
            "brightBlue": "#0000FF",
            "brightCyan": "#00FFFF",
            "brightGreen": "#00FF00",
            "brightPurple": "#FF00FF",
            "brightRed": "#FF0000",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFFF00",
            "cursorColor": "#FFFFFF",
            "cyan": "#008080",
            "foreground": "#C0C0C0",
            "green": "#008000",
            "name": "Vintage",
            "purple": "#800080",
            "red": "#800000",
            "selectionBackground": "#FFFFFF",
            "white": "#C0C0C0",
            "yellow": "#808000"
        }
    ],
    "theme": "dark",
    "themes": [],
    "useAcrylicInTabRow": true
}

总结一下:

  • 默认配置文件:选择对应 WSL 的那个,如 Ubuntu-20.04

  • 字体:Maple Mono NF

  • 快捷键:采用默认快捷键,但是注意 Ctrl+v 这种会和 vim 冲突的键位需要改掉

  • 外观

    • 光标:|
    • 透明度:80%
    • 亚克力:开启
    • 配色方案:Tango Dark

这里再给上两篇文章以供参考:

安装 WSL 环境

操作参考:

打开 PowerShell 输入以下命令进行安装

## 使用管理员运行
wsl --install

## 安装完成后可以使用以下命令查看可供安装的 Linux 发行版
wsl -l -o

# 以下是可安装的有效分发的列表。
# 使用 'wsl.exe --install <Distro>' 安装。

# NAME                            FRIENDLY NAME
# Ubuntu                          Ubuntu
# Debian                          Debian GNU/Linux
# kali-linux                      Kali Linux Rolling
# Ubuntu-18.04                    Ubuntu 18.04 LTS
# Ubuntu-20.04                    Ubuntu 20.04 LTS
# Ubuntu-22.04                    Ubuntu 22.04 LTS
# Ubuntu-24.04                    Ubuntu 24.04 LTS
# OracleLinux_7_9                 Oracle Linux 7.9
# OracleLinux_8_7                 Oracle Linux 8.7
# OracleLinux_9_1                 Oracle Linux 9.1
# openSUSE-Leap-15.6              openSUSE Leap 15.6
# SUSE-Linux-Enterprise-15-SP5    SUSE Linux Enterprise 15 SP5
# SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6
# openSUSE-Tumbleweed             openSUSE Tumbleweed

## 使用 wsl --install -d <Distribution Name> 进行安装,这里我们安装 Ubuntu
wsl --install -d Ubuntu

WSL 的好处在于 Windows 和 Linux 之间的文件系统可以互相访问,网络也可以通过 NAT 或者桥接打通,比传统的虚拟机要方便很多(虽然 WSL 也是个虚拟机),下面放上一些文章以供大家学习:

配置 WSL 环境

作为日常工作的 80% 甚至是 90% 的系统环境,配置一个方便好用的 Linux 能够给工作体验和效率带来极大的提升,正所谓磨刀不误砍柴工,乱拳打死老师傅。这里以 Ubuntu 为例,记录一下 Linux 环境的配置内容:

# 1. 更换软件源(源是什么? -->  https://flyflypeng.tech/%E5%88%A9%E5%99%A8/2016/10/24/Ubuntu%E8%BD%AF%E4%BB%B6%E6%BA%90%E8%AF%A6%E8%A7%A3.html)
## 以往都是用 aliyun 源的,最近比较喜欢清华源 ~
sudo sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

# 2. 安装常用软件
sudo apt install git net-tools vim tmux zsh dstat sysstat bat curl htop -y

# 3. tmux 配置
## 对于 tmux 的配置,我这里仅推荐 GitHub 上的开源项目 .tmux --> https://github.com/gpakosz/.tmux,其余的大家可以自行去网络上搜索
cd ~
git clone https://github.com/gpakosz/.tmux.git
ln -s -f .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .

## 除此之外,我一般还会额外打开 tmux 的一些配置
set -g history-limit 10000    ## tmux 默认的历史纪录太少了,多搞点
set -g set-clipboard on    ## 打开选中复制,当然你可以使用 Windows terminal 的选中复制功能
set -g mouse on    ## 打开鼠标支持,这样就不用 Ctrl+B 切换 panel 了

## 关于 tmux 使用的一些推荐阅读:
## - https://www.ruanyifeng.com/blog/2019/10/tmux.html
## - https://pengfeixc.com/blogs/developer-handbook/tmux.html
## - https://www.escapelife.site/posts/1a9a72ec.html

# 4. Docker
## 容器技术已经成为当前最廉价的技术了,作为一名运维工程师,docker 是一个必知必会的技能,不管你日常用的多还是少,先把 docker 在你自己的系统装上
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null

apt-get update
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

这里单独说一下 zsh 的配置,首当其冲的肯定是把 oh-my-zsh 给装上

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

接着肯定是要换一个美美的皮肤(bushi),主题的配置可以参考官方文档 Themes|oh-my-zsh wiki,我的配置如下:

ZSH_THEME="powerlevel10k/powerlevel10k"

zsh 对于我来说,提效最大的还是他的一堆插件了,这里先贴一下当前已用的插件:

plugins=(
    zsh-ssh
    ssh-agent
    aliases
    zsh-autosuggestions
    safe-paste
    F-Sy-H
    z
    cp
    copyfile
    copybuffer
    copypath
    git
    fzf
    sudo
    kube-ps1
)

像常见的 zsh-autosuggestionscpgit 以及 sudo 等插件这里就不多提及了,基本上是网上提到过比较的好用的插件了,大家可以自行去搜索。下面来说一些我个人觉得很好用的插件:

  1. zsh-ssh
    zsh-ssh 是我目前强推的一个 zsh 插件,该插件会去读取 ~/.ssh/config 文件中的配置并通过 fzf 将其可交互显示,也就是说我们可以通过命令界面动态展示 ssh host 列表并选择连接。由于该插件还依赖 fzf,所以先安装 fzf 再安装 zsh-ssh

    ## 安装 fzf
    git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
    ~/.fzf/install
    
    cat << EOF > ~/.zshrc
    # Set up fzf key bindings and fuzzy completion
    source <(fzf --zsh)
    EOF
    source .zshrc
    
    ## 安装 zsh-ssh
    git clone https://github.com/sunlei/zsh-ssh ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-ssh

    上面说到了,zsh-ssh 这个插件是读取 ~/.ssh/config 文件中的配置来显示 ssh host 的,因此我们还需要将 ssh host 信息写入配置文件,我自己是写了一个脚本来生成 ssh host,具体的配置格式参考如下:

    Host Bastion-Host
    Hostname 1.1.1.1
    User sunlei
     
    Host Development-Host
    Hostname 2.2.2.2
    IdentityFile ~/.ssh/development-host
    #_Desc For Development
  2. F-sy-H
    相较于网上文档里说的比较多的 zsh-users/zsh-syntax-highlightingF-sy-H 插件的高亮配色会更加丰富一些,虽然没有什么大用,但是好看就完事了

    git clone https://github.com/z-shell/F-Sy-H.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/F-Sy-H
  3. kube-ps1
    kube-ps1 插件是用来显示当前所应用的 K8s 集群信息的,由于已经被 zsh 集成为默认插件了,所以不需要的单独安装,直接在 ~/.zshrc 中配置即可

    ## 除了配置 plugins 外,还需要添加 PROMPT 变量
    PROMPT='$(kube_ps1)'$PROMPT # or RPROMPT='$(kube_ps1)'
  4. z
    z 插件会基于历史访问过的目录进行快速跳转,无需输入全路径即可跳转,按下 tab 键即可显示历史目录列表,选择后回车即可跳转。z 已经被集成进 zsh 默认插件列表,无需单独安装。

  5. ssh-agent
    既然我们都这么依赖 ssh 来工作了,那么 ssh-agent 插件就必不可少,该插件会自动帮我们设置一些 ssh-agent 相关的配置,加载 credentials。由于该插件是 zsh 默认的插件,所以无需单独安装,直接在 ~/.zshrc 中配置即可

    ## 配置 agent-forwading
    zstyle :omz:plugins:ssh-agent agent-forwarding yes
    
    ## 配置 identities
    zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github    ## 如果 identities 不在 ~/.ssh 目录下,则要写绝对路径