Linux 全局安装配置 zsh + oh-my-zsh

Posted by sysin on 2022-06-01
Estimated Reading Time 7 Minutes
Words 1.7k In Total
更新日期:Wed Jun 01 2022 16:09:06 GMT+0800,阅读量:

请访问原文链接:Linux 全局安装配置 zsh + oh-my-zsh,查看最新版。原创作品,转载请保留出处。

作者主页:sysin.org


无耻抄袭者 Yu Tao,请立遁!!!

OMZLogo

Linux 中使用 oh-my-zsh 配置 zsh 默认仅对当前用户有效,新建或者切换用户后,仍然恢复到了默认的 bash,非常不方便。本文描述全局配置的方法,新用户将默认使用 zsh 并加载统一的配置文件。

本文写作环境基于:Ubuntu 20.04 和 CentOS 8,其他版本类似,可以参照。

准备 zsh

  • 查看当前 shell

    1
    echo $SHELL

    返回结果:

    1
    /bin/bash
  • 安装 zsh

    CentOS:

    1
    2
    3
    4
    yum -y install zsh
    # or
    #dnf -y install zsh
    yum -y install util-linux-user

    Ubuntu:

    1
    sudo apt install zsh
  • 为 root 设置默认 shell

    1
    sudo chsh -s /bin/zsh
  • 为特定用户设置默认 shell

    1
    2
    sudo chsh -s /bin/zsh <username>
    # <username> 替换为实际用户名

    返回结果如下,表示切换完成(下载安装 oh-my-zsh 成功后也会提示切换)

    1
    2
    3
    # sudo chsh -s /bin/zsh
    Changing shell for root.
    Shell changed.

    在 CentOS 8 中可能报错 Command not found,执行 sudo dnf install util-linux-user

  • 安装 git

    执行:

    1
    2
    3
    4
    # CentOS
    yum -y install git
    # Ubuntu
    sudo apt install git

全局配置 zsh

注意:以下全局配置相关命令需要 root 权限,请切换到 root 账号,或者使用 sudo。

  • 切换成 root

    1
    2
    3
    su #切换到 root 账号,pwd 不变
    su - #切换到 root 账号,pwd 同时变为 root 主目录
    sudo -i #同上
  • 全局安装 zsh 到 /etc 目录

    1
    git clone --depth=1 https://github.com/ohmyzsh/ohmyzsh.git /etc/oh-my-zsh
  • 从模板文件复制 .zshrc 创建默认配置文件(新用户将使用该配置文件)

    1
    cp /etc/oh-my-zsh/templates/zshrc.zsh-template /etc/skel/.zshrc
  • 修改 on-my-zsh 的安装目录 export ZSH=$HOME/.oh-my-zshexport ZSH=/etc/oh-my-zsh

    1
    sed -i 's|$HOME/.oh-my-zsh|/etc/oh-my-zsh|g' /etc/skel/.zshrc
  • 为每个用户配置独立的 cache 目录

    编辑 /etc/skel/.zshrcexport ZSH=/etc/oh-my-zsh 下添加一句:

    1
    export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"

    注意:必须在 export ZSH=/etc/oh-my-zsh 之后 source $ZSH/oh-my-zsh.sh 之前添加,否则会混乱。

    oh-my-zsh.sh 中定义了 ZSH_CACHE_DIR 路径:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # Set ZSH_CACHE_DIR to the path where cache files should be created
    # or else we will use the default cache/
    if [[ -z "$ZSH_CACHE_DIR" ]]; then
    ZSH_CACHE_DIR="$ZSH/cache"
    fi

    # Make sure $ZSH_CACHE_DIR is writable, otherwise use a directory in $HOME
    if [[ ! -w "$ZSH_CACHE_DIR" ]]; then
    ZSH_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh"
    fi

    初始安装 “$ZSH/cache” 存在为空目录。
    如果不添加上述语句,默认 root 用户对 “$ZSH/cache” 有写权限,会直接将 “$ZSH/cache” 作为 cache 目录。
    如果在 source $ZSH/oh-my-zsh.sh 之后添加,root 在登录后先将 “$ZSH/cache” 作为 cache 目录,然后再更改为自定义路径。
    对于普通用户,默认将使用 “${XDG_CACHE_HOME:-$HOME/.cache}/oh-my-zsh” 作为 cache 目录,经测试自定义其他路径无效。

  • 更改默认主题(推荐 ys)

    编辑 /etc/skel/.zshrc 文件修改:

    1
    sed -i '/^ZSH_THEME=.*/c ZSH_THEME="ys"' /etc/skel/.zshrc
  • 取消每周自动检查更新

    取消注释 /etc/skel/.zshrc 中的 “# DISABLE_AUTO_UPDATE=“true””

    1
    2
    3
    4
    5
    6
    sed -i 's/^#[]\(DISABLE_AUTO_UPDATE="true"\)/\1/' /etc/skel/.zshrc
    # -i 写入文件
    # s 替换指定字符
    # ^ 匹配行开始
    # [ ] 中括号中间有空格表示要替换的内容是一个空格,注意被替换的内容是空格用 " "(双引号中间有空格)表示。
    # \1 的意思就类似于前面的 (DISABLE_AUTO_UPDATE="true"\),\1 就是复制这个位置的内容,

    手动更新:omz update(旧版命令:upgrade_oh_my_zsh)

  • 配置 ll 别名(可选)

    1
    echo 'alias ll="ls -lahF --color --time-style=long-iso"' >> /etc/skel/.zshrc

全局配置插件

全局安装插件(安装到 /etc/oh-my-zsh/custom/plugins/)

zsh-syntax-highlighting:语法高亮插件

作用:命令错误会显示红色,直到你输入正确才会变绿色,另外路径正确会显示下划线。

安装:

1
2
3
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git /etc/oh-my-zsh/custom/plugins/zsh-syntax-highlighting
# 注意:以下命令包含变量需要当前用户 .zshrc 配置后才有效
#git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-syntax-highlighting

配置启用插件:

编辑 /etc/skel/.zshrc,以下部分添加插件的名字

1
plugins=([plugins...] zsh-syntax-highlighting)

快速修改:

1
sed -i '/^plugins=.*/c plugins=(git zsh-syntax-highlighting)' /etc/skel/.zshrc

zsh-completions

额外的自动补全功能,用于补充 zsh 中尚不支持的命令补全,该项目将在完善时合并到 zsh。

  • Clone the repository inside your oh-my-zsh repo:

    1
    2
    3
    git clone --depth=1 https://github.com/zsh-users/zsh-completions /etc/oh-my-zsh/custom/plugins/zsh-completions
    # 注意:以下命令包含变量需要当前用户 .zshrc 配置后才有效
    #git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions
  • Add it to FPATH in your .zshrc by adding the following line before source "$ZSH/oh-my-zsh.sh":

    1
    fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src

Note: adding it as a regular Oh My ZSH! plugin will not work properly (see #603).

zsh-autosuggestions

作用是根据历史输入命令的记录即时的提示(建议补全),然后按 → 键即可补全。*

1
2
3
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git /etc/oh-my-zsh/custom/plugins/zsh-autosuggestions
# 注意:以下命令包含变量需要当前用户 .zshrc 配置后才有效
#git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-autosuggestions

编辑 /etc/skel/.zshrc,找到 plugins=(git) 这一行,修改为:

1
2
3
4
5
plugins=(
git
# other plugins...
zsh-autosuggestions
)

Incremental completion on zsh

增强的实时自动命令补全插件:Incremental completion on zsh

该插件对性能似乎有一点点影响,请根据需要启用。

作用如图:

incr

1
2
3
4
mkdir /etc/oh-my-zsh/custom/plugins/incr
curl -fsSL https://mimosa-pudica.net/src/incr-0.2.zsh -o /etc/oh-my-zsh/custom/plugins/incr/incr.zsh
#启用该插件
echo "source /etc/oh-my-zsh/custom/plugins/incr/incr.zsh" >> /etc/skel/.zshrc

使用用户配置文件

  • 改变新用户的默认 shell

    vi /etc/default/useradd

    将 SHELL= * (比如 SHELL=/bin/sh) 改成 SHELL=/bin/zsh

    1
    sed -i '/^SHELL=.*/c SHELL=/bin/zsh' /etc/default/useradd

    修改后,使用 useradd 命令无需 -s /bin/zsh,用户默认使用 zsh,当然也可以不修改此项,useradd 命令继续追加 -s /bin/zsh 参数。

    新用户登录后,将自动复制 .zshrc 和上述 cache 目录到用户主目录下,并自动加载 zsh 配置。

  • 针对现有用户

    直接复制 /etc/skel/.zshrc~/

    1
    2
    cp /etc/skel/.zshrc ~/.zshrc
    source ~/.zshrc

参考:创建管理员账号的正确姿势(默认配置)。

在 visudo (/etc/sudoers)默认配置下,创建管理员账号的命令如下:

CentOS:

1
2
useradd -m -s /bin/zsh -G wheel sysin  #sysin 为用户名
passwd sysin #为 sysin 设置密码

Ubuntu:

1
2
useradd -m -s /bin/zsh -G sudo sysin  #sysin 为用户名
passwd sysin #为 sysin 设置密码

备注:

-m 创建同名 home 目录

-s 指定 shell

-G 修改附加属组


捐助本站 ❤️ Donate

点击访问官方网站


文章用于推荐和分享优秀的软件产品及其相关技术,所有软件默认提供官方原版(免费版或试用版),免费分享。对于部分产品笔者加入了自己的理解和分析,方便学习和测试使用。任何内容若侵犯了您的版权,请联系作者删除。如果您喜欢这篇文章或者觉得它对您有所帮助,或者发现有不当之处,欢迎您发表评论,也欢迎您分享这个网站,或者赞赏一下作者,谢谢!

支付宝赞赏 微信赞赏

赞赏一下


☑️ 评论恢复,欢迎留言❗️
敬请注册!点击 “登录” - “用户注册”(已知不支持 21.cn/189.cn 邮箱)。请勿使用联合登录(已关闭)