linux 初始化

Rocky linux(RedHat 系)
a.更新 yum 源
cp /etc/yum.repos.d/rocky.repo{,.bak}
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/rocky*.repo
yum install -y epel-release
dnf makecache
yum -y update
# 安装基础应用
yum install -y git vim iproute net-tools
b.网卡配置
# cat /etc/NetworkManager/system-connections/ens18.nmconnection
[ipv4]
address1=192.168.123.66/24,192.168.123.1
dns=223.5.5.5
may-fail=false
method=manual
systemctl restart NetworkManager
# 调用nmcli重启设备和连接配置
nmcli d r ens18
nmcli c r ens18
c.优化终端
# 自定义终端网站
https://ezprompt.net/
vim /etc/profile
#------------
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF --color=auto'
alias l.='ls -d .* --color=auto'
alias la='ls -A --color=auto'
alias ll='ls -alFh --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'
export CLICOLOR=1
export LSCOLORS=gxfxaxdxcxegedabagacad
export PS1="\[\e[01;32m\]\u\[\e[m\]\[\e[01;32m\]@\[\e[m\]\[\e[01;32m\]\h\[\e[m\]\[\e[01;33m\]:\[\e[m\]\[\e[01;34m\]\w\[\e[m\] \[\e[01;35m\]\A\[\e[m\]\$ "
#-------------
source /etc/profile
# 安装提示bash提示
yum -y install bash-completion
使用 zsh+oh my zsh 方案
# 查看安装的shell
cat /etc/shells
# 查看默认shell
echo $SHELL
# 安装zsh
sudo apt install zsh -y #安装zsh
cat /etc/shells #查看shells
zsh --version #查看版本
# 设置默认shell
chsh -s $(which zsh)#设置终端默认shell为zsh
sudo reboot#重启系统
# 更新软件源
sudo apt update && sudo apt upgrade -y
# 安装 zsh git curl
sudo apt install zsh git curl -y
# 安装oh my zsh
Method Command
curl sh -c "$(curl -fsSL https://install.ohmyz.sh/)"
wget sh -c "$(wget -O- https://install.ohmyz.sh/)"
fetch sh -c "$(fetch -o - https://install.ohmyz.sh/)"
国内curl sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"
国内wget sh -c "$(wget -O- https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"
# 安装oh my zsh 主题 powerlevel10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# 中国用户可以使用 gitee.com 上的官方镜像加速下载
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
# 安装插件
# 命令提示
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 中国用户可以使用下面任意一个加速下载
# 加速1
git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 加速2
git clone https://gh.xmly.dev/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 加速3
git clone https://gh.api.99988866.xyz/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 命令检测
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 中国用户可以使用下面任意一个加速下载
# 加速1
git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 加速2
git clone https://gh.xmly.dev/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 加速3
git clone https://gh.api.99988866.xyz/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 内置插件 z
oh-my-zsh 内置了 z 插件。z 是一个文件夹快捷跳转插件,对于曾经跳转过的目录,只需要输入最终目标文件夹名称,就可以快速跳转,避免再输入长串路径,提高切换文件夹的效率。
# 内置插件 extract x
oh-my-zsh 内置了 extract 插件。extract 用于解压任何压缩文件,不必根据压缩文件的后缀名来记忆压缩软件。
# 内置插件 web-search
oh-my-zsh 内置了 web-search 插件。web-search 能让我们在命令行中使用搜索引擎进行搜索。使用搜索引擎关键字+搜索内容 即可自动打开浏览器进行搜索。
# 启用插件,主题 如果设置ZSH_THEME="random"随机主题
sudo sed -i "s/^ZSH_THEME=.*/ZSH_THEME='powerlevel10k/powerlevel10k'/" ~/.zshrc
sudo sed -i "s/^plugins=.*/'plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search)'" ~/.zshrc
# mac执行
sudo sed -i "" "s/^ZSH_THEME=.*/ZSH_THEME='powerlevel10k/powerlevel10k'/" ~/.zshrc
sudo sed -i "" "s/^plugins=.*/'plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search)'" ~/.zshrc
# 让其生效
source ~/.zshrc
# 卸载 oh my zsh
uninstall_oh_my_zsh
Are you sure you want to remove Oh My Zsh? [y/N] Y
# 手动更新 Oh My Zsh
upgrade_oh_my_zsh
# 安装字体 powerline
git clone https://github.com/powerline/fonts.git --depth=1
#安装字体
cd fonts
./install.sh
# 图形化安装终端 tabby kitty wezterm
wget https://github.com/Eugeny/tabby/releases/download/v1.0.223/tabby-1.0.223-linux-x64.deb
sudo dpkg -i *.deb
sudo apt-get install gconf2 gconf-service -y
sudo apt --fix-broken install # 解决安装依赖的问题
# 将tabby设置为默认窗口,通过快捷键打开 ctrl + alt + t
gsettings set org.gnome.desktop.default-applications.terminal exec /usr/bin/tabby
gsettings set org.gnome.desktop.default-applications.terminal exec-arg "-x"
# 还原为原来的设置:
gsettings reset org.gnome.desktop.default-applications.terminal exec
gsettings reset org.gnome.desktop.default-applications.terminal exec-arg
轻量化方案zsh+zinit
# 查看安装的shell
cat /etc/shells
# 查看默认shell
echo $SHELL
# 安装zsh
sudo apt install zsh -y #安装zsh
cat /etc/shells #查看shells
zsh --version #查看版本
# 设置默认shell
chsh -s $(which zsh)#设置终端默认shell为zsh
sudo reboot#重启系统
# 配置~/.zshrc
# ==============================================
# 高性能 Zsh 配置 (zinit + Powerlevel10k)
# 适用于新设备,无 OMZ 依赖
# 最后更新: 2026-05-31
# ==============================================
# ---- 1. 自动安装 zinit (如果缺失) ----
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
print -P "%F{33} %F{220}正在安装 %F{33}Zinit%F{220} 插件管理器…%f"
command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
command git clone --depth 1 https://github.com/zdharma-continuum/zinit "$HOME/.local/share/zinit/zinit.git" && \
print -P "%F{33} %F{34}Zinit 安装成功.%f%b" || \
print -P "%F{160} Zinit 克隆失败,请检查网络.%f%b"
fi
# ===== 1.5 历史记录(History) =====
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=10000
export SAVEHIST=10000
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_IGNORE_SPACE
setopt APPEND_HISTORY
setopt INC_APPEND_HISTORY
setopt EXTENDED_HISTORY
setopt SHARE_HISTORY
# 加载 zinit
source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
# 智能提示
zinit light zsh-users/zsh-completions
# 现在初始化补全系统(compdef 将在这之后可用)
autoload -Uz compinit
compinit -u # -u 跳过不安全目录的提示,避免产生额外输出
# ---- 2. Powerlevel10k 瞬时提示 (必须紧跟 zinit 之后) ----
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# ---- 3. 插件加载 (全部轻量独立仓库 / OMZ 片段) ----
# Git 别名与函数 (等价于 OMZ git 插件)
zinit snippet OMZP::git
# 自动补全建议 (灰色提示)
zinit light zsh-users/zsh-autosuggestions
# 语法高亮 (延迟加载, 提升启动速度)
zinit ice wait lucid
zinit light zsh-users/zsh-syntax-highlighting
# 智能目录跳转 (z 命令)
zinit light rupa/z
# 万能解压命令 (extract)
zinit snippet OMZP::extract
# 历史子字符串搜索 (上下箭头匹配)
zinit light zsh-users/zsh-history-substring-search
# 从命令行快速搜索 (google/stackoverflow 等)
zinit snippet OMZP::web-search
# ---- 4. 主题: Powerlevel10k ----
zinit ice depth=1
zinit light romkatv/powerlevel10k
# 加载 p10k 配置文件 (首次运行时可用 p10k configure 生成)
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
# ---- 5. 基础别名 (安全、颜色、便捷) ----
# 安全操作 (覆盖原命令的 -i 提示)
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
# 跨平台 ls 颜色
if [[ "$OSTYPE" == (darwin|freebsd)* ]]; then
alias ls='ls -G' # macOS/BSD 风格
else
alias ls='ls --color=auto' # GNU/Linux
fi
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alFh'
alias l.='ls -d .*'
# grep 颜色 (需 GNU grep 或兼容版本; macOS 原生 grep 请先 brew install grep)
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
# ---- 6. 历史子字符串搜索按键绑定 ----
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
# ---- 7. 用户自定义区域 (放在这里避免触发 p10k 警告) ----
# 在这里添加你自己的别名、环境变量、初始化脚本等
# 例如:
# export PATH="$HOME/bin:$PATH"
# alias tp="cd ~/projects"
# ---- 8. 如果存在本地环境变量文件则加载 (可选) ----
[[ -f "$HOME/.local/bin/env" ]] && source "$HOME/.local/bin/env"
# reload
source ~/.zshrc
d.防火墙修改 firewalld 为 iptables
systemctl stop firewalld
systemctl disable firewalld
yum -y install iptables-services
systemctl start iptables
iptables -F
systemctl enable iptables
service iptables save
e.禁用 selinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
grubby --update-kernel ALL --args selinux=0
# 查看是否禁用
# grubby --info DEFAULT
# 回滚内核层禁用操作
# grubby --update-kernel ALL --remove-args selinux
f.设置时区
timedatectl set-timezone Asia/Shanghai
g.安装 docker 环境
# 加载bridge
yum install -y epel-release
yum install -y bridge-utils
modprobe br_netfilter &&
echo "br_netfilter" >> /etc/modules-load.d/bridge.conf &&
echo "net.bridge.bridge-nf-call-iptables=1" >> /etc/sysctl.conf &&
echo "net.bridge.bridge-nf-call-iptables=1" >> /etc/sysctl.conf &&
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf &&
sysctl -p
# 添加docker-ce yum源
# 中科大
sudo dnf config-manager --add-repo https://mirrors.ustc.edu.cn/dockerce/linux/centos/docker-ce.repo
# 阿里
sudo dnf config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cd /etc/yum.repos.d
# 切换中科大源
sed -o 's|download.docker.com|mirrors.ustc.edu.cn/docker-ce|g' docker-ce.repo
sudo yum makecache
# 安装docker-ce
yum -y install docker-ce
# 配置daemon
vim /etc/docker/daemon.json
cat <<EOF>>/etc/docker/daemon.json
{
"default-ipc-mode": "shareable",
"data-root": "/data/docker",
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "100"
},
"insecure-registries": ["harbor.xinxainghf.com"],
"registry-mirrors": [
"https://zemxtma9.mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn"
]
}
EOF
# 重启服务
mkdir -p /etc/systemd/system/docker.service.d &&
systemctl daemon-reload && systemctl restart docker && systemctl enable docker
# 配置zsh completion
mkdir -p ~/.oh-my-zsh/completions
docker completion zsh > ~/.oh-my-zsh/completions/_docker
h.tmux 安装
Tmux 是一个终端复用器(terminal multiplexer),属于常用的开发工具,学会了之后可以大大的提高工作效率。
1.1 基本概念 在使用 tmux 之前我们先了解关于 tmux 的几个名词:
session,会话(任务) windows,窗口 pane,窗格 关于 session,很多人把 session 成为会话,但我觉得叫任务更适合一些。
在普通的终端中,窗口和其中由于 session(任务)而启动的进程是连在一起的,关闭窗口,session 就结束了,session 内部的进程也会终止,不管是否运行完。但是在具体使用中,我们希望当前的 session 隐藏起来,在终端中做其他事情,但是又不希望 session 及其进程被关闭。这样就需要用到 tmux,对 session 进行解绑。之后再想继续出来这个 session 的时候,再次绑定就可以回到之前的工作状态。
对于 window 可以理解为一个工作区,一个窗口。
对于一个 session,可以创建好几个 window,对于每一个窗口,都可以将其分解为几个 pane 小窗格。
所以,关于 session、window、pane 的关系是: [pane∈window]∈session
1.2 安装
# Ubuntu or Debian
sudo apt-get install tmux
# CentOS or Fedora
sudo yum install tmux
# Mac
brew install tmux
2.1 session 启动
新建 session,可以在 terminal 上输入tmux命令,会自动生成一个 id 为 0 的 session
tmux
也可以在建立时显式地说明 session 的名字,这个名字可以用于解绑后快速的重新进入该 session
tmux new -s your-session-name
2.2 session 分离
在 tmux 窗口中,按下ctrl+b d或者输入以下命令,就会将当前 session 与窗口分离,session 转到后台执行
tmux detach
2.3 session 退出
如果你想退出该 session,可以杀死 session
tmux kill-session -t your-session-name
当然,也可以使用ctrl+d关闭该 session 的所有窗口来退出该 session
2.4 绑定、解绑、切换 session
假设现在正处于 session1,使用分离操作就是将 session1 进行解绑:
tmux detach
而如果你想再次绑定 session1,可以使用命令:
tmux attach -t your-session-name
切换到指定 session:
tmux switch -t your-session-name
2.5 重命名 session
tmux rename-session -t old-session new-session
3.1 新建窗口
一个 session 可以有好几个 window 窗口。
# 新建一个指定名称的窗口
tmux new-window -n your-window-name
3.2 切换窗口
ctrl+b c: 创建一个新窗口(状态栏会显示多个窗口的信息)
ctrl+b p: 切换到上一个窗口(按照状态栏的顺序)
ctrl+b n: 切换到下一个窗口
ctrl+b w: 从列表中选择窗口(这个最好用)
3.3 重命名窗口
tmux rename-window -t old_name new_name
4.1 pane 操作划分窗格
tmux 可以将一个窗口分为几个窗格(pane),每个窗格运行不同的命令。
# 划分为上下两个窗格
tmux split-window
# 划分左右两个窗格
tmux split-window -h
# 使用快捷键
左右划分:ctrl+b %
上下划分:ctrl+b "
4.2 光标位置
# ctrl+b arrow-key(方向键):光标切换到其他窗格
4.3 交换窗格位置
# 列出所有快捷键,及其对应的 Tmux 命令
$ tmux list-keys
# 列出所有 Tmux 命令及其参数
$ tmux list-commands
# 列出当前所有 Tmux 会话的信息
$ tmux info
# 重新加载当前的 Tmux 配置
$ tmux source-file ~/.tmux.conf
4.4 关闭窗格
# ctrl+d,记住如果只有一个窗格就是关闭window哦
5.1 其他操作
# 列出所有快捷键,及其对应的 Tmux 命令
$ tmux list-keys
# 列出所有 Tmux 命令及其参数
$ tmux list-commands
# 列出当前所有 Tmux 会话的信息
$ tmux info
# 重新加载当前的 Tmux 配置
$ tmux source-file ~/.tmux.conf
5.2 tmux 上下翻屏
# 使用快捷键ctrl+b [ ,就可以通过方向键上下移动使用PageUp和PageDown可以实现上下翻页
i.vim优化
yum install -y vim
cat >>~/.vimrc<<EOF
colorscheme desert
syntax on
filetype on
au BufRead,BufNewFile *.sv set filetype=systemverilog
au BufRead,BufNewFile *.v set filetype=verilog
set number
set autoindent
set ts=2
set expandtab
set shiftwidth=4
autocmd FileType make set noexpandtab
set hlsearch
set cursorline
set cursorcolumn
set guifont=Monospace\ 16
inoremap ( ()<Esc>i
inoremap { {}<Esc>i
inoremap [ []<Esc>i
inoremap " ""<Esc>i
EOF