CentOS 7.6 常用配置
CentOS 7.6 仍然常见于一些存量环境里,但它默认提供的软件版本偏旧,不过很多现代工具可通过第三方源或 Snap 来补齐。 本文记录在这类环境下的常用配置,方便后续快速搭建可用的开发环境。
配置静态 IP
修改配置文件 /etc/sysconfig/network-scripts/ifcfg-<dev>:
# 其默认值为 dhcp
BOOTPROTO=static
# 系统启动时自动启用该网卡
ONBOOT=yes
# 静态 IP
IPADDR=<ip>
# 掩码长度
PREFIX=<prefix>
# 网关 IP
GATEWAY=<ip>
# DNS
DNS1=114.114.114.114
重启服务:
sudo systemctl restart network
为 Yum 配置镜像
CentOS Linux 7 已于 2024-06-30 结束生命周期(EOL),官方不再维护,因此改用国内镜像。
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sudo yum clean all
sudo yum makecache fast
EPEL(Extra Packages for Enterprise Linux)是 Fedora 社区维护的扩展仓库,用于补充 CentOS 官方源缺少的软件:
sudo yum install epel-release
sudo curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
sudo yum clean all
sudo yum makecache fast
SCL(Software Collections)是 Red Hat/CentOS 提供的一套并行安装机制,可在不替换系统默认版本的前提下安装较新的开发工具链(如新版 GCC、Python 等)。 在 CentOS 7 这类老系统里,它常用于补齐默认源中过旧的软件版本。
sudo yum install centos-release-scl
sudo sed -E -e "s|^mirrorlist=|# mirrorlist=|g" \
-e "s|^# ?baseurl=http://mirror.centos.org/centos/7|baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009|g" \
-i.bak \
/etc/yum.repos.d/CentOS-SCLo-scl*.repo
sudo yum clean all
sudo yum makecache fast
安装 Docker
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-24.0.9-1.el7 docker-ce-cli-24.0.9-1.el7 containerd.io
# 启动 Docker 服务,并设置为开机自启。
sudo systemctl enable --now docker
# 将当前用户加入 docker 组,之后不需要 sudo 即可直接使用 docker 命令。
sudo usermod -aG docker "${USER}"
# 重新登录当前用户,使新的组成员身份立即生效。
su - "${USER}"
docker run --name hello-world hello-world
docker ps -a
docker rm hello-world
docker rmi hello-world
# 创建 Docker 客户端配置目录,并为交互式容器统一配置脱离快捷键。
mkdir -p "${HOME}/.docker"
cat << 'EOF' > "${HOME}/.docker/config.json"
{
"detachKeys": "ctrl-\\,ctrl-q"
}
EOF
安装 Snap
sudo yum install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap
Rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
安装 Helix
sudo snap install helix
hx --health
安装 GitUI
cargo install --locked gitui
gitui --version
mkdir -p "${HOME}/.config/gitui"
curl -L -o "${HOME}/.config/gitui/key_bindings.ron" https://raw.githubusercontent.com/gitui-org/gitui/refs/heads/master/vim_style_key_config.ron
安装 clangd 与 clang-format
mkdir "${HOME}/dev-tools/Clang" && cd "${HOME}/dev-tools/Clang"
curl -LO https://github.com/llvm/llvm-project/releases/download/llvmorg-8.0.1/clang+llvm-8.0.1-x86_64-linux-gnu-ubuntu-14.04.tar.xz
tar -Jxf *.tar.xz
echo "# PATH for Clang" >> "${HOME}/.bashrc"
echo 'export PATH="${HOME}/dev-tools/Clang/clang+llvm-8.0.1-x86_64-linux-gnu-ubuntu-14.04/bin:${PATH}"' >> "${HOME}/.bashrc"
. "${HOME}/.bashrc"
clangd --version
注意,不能装太新的版本,因为它依赖的 glibc 版本可能会比较高。 查询系统中 glibc 版本:
ldd --version
安装 Bear
https://github.com/rizsotto/Bear/tree/2.4.4
sudo yum install cmake
mkdir "${HOME}/dev-tools/Bear" && cd "${HOME}/dev-tools/Bear"
install_dir="${HOME}/dev-tools/Bear/install"
mkdir -p "${install_dir}"
curl -LO https://github.com/rizsotto/Bear/archive/refs/tags/2.4.4.tar.gz
tar -zxf *.tar.gz
cd Bear-2.4.4
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release "-DCMAKE_INSTALL_PREFIX=${install_dir}"
make -j"$(nproc)"
make install
echo "# PATH for Bear" >> "${HOME}/.bashrc"
echo 'export PATH="${HOME}/dev-tools/Bear/install/bin:${PATH}"' >> "${HOME}/.bashrc"
. "${HOME}/.bashrc"
bear --version
安装 Nushell
如果希望 SSH 登录后默认进入 Nushell,最好通过 Cargo 安装;如果改用 Snap 安装会不太好处理。
cargo install nu
nu --version
配置 $nu.config-path:
$env.config.buffer_editor = 'hx'
$env.config.history = {
file_format: sqlite
isolation: true
sync_on_enter: true
}
配置 $nu.env-path:
use std/util 'path add'
path add ('/usr/sbin')
path add ('/sbin')
path add ('/snap/bin')
path add ($env.HOME | path join '.cargo' 'bin')
$env.EDITOR = 'hx'
$env.VISUAL = 'hx'
$env.HOSTNAME = (hostname)
def create_left_prompt [] {
let hostname = $env.HOSTNAME
let dir = ($env.PWD)
$"(ansi green_bold)($hostname)(ansi reset) (ansi blue)($dir)(ansi reset)"
}
$env.PROMPT_COMMAND = {|| create_left_prompt}
接下来配置登录 Shell,让 SSH 登录后默认进入 Nushell:
echo "${HOME}/.cargo/bin/nu" | sudo tee -a /etc/shells
chsh -s "${HOME}/.cargo/bin/nu"
如果切换后 SSH 登录异常,可以先查看最近的 SELinux 拒绝记录:
sudo ausearch -m avc -ts recent | grep nu
尝试临时将 SELinux 从强制模式切换到宽容模式:
sudo setenforce 0
此时如果 SSH 登录正常则说明与 SELinux 有关。
安装 Wezterm
sudo yum install devtoolset-7-gcc openssl11-devel
scl enable devtoolset-7 -- gcc --version
mkdir -p "${HOME}/dev-tools/Wezterm" && cd "${HOME}/dev-tools/Wezterm"
git clone https://github.com/wezterm/wezterm.git
cd wezterm
export OPENSSL_INCLUDE_DIR=/usr/include/openssl11
export OPENSSL_LIB_DIR=/usr/lib64/openssl11
export OPENSSL_NO_VENDOR=1
scl enable devtoolset-7 -- cargo build --release -p wezterm -p wezterm-mux-server
./target/release/wezterm --version
mkdir -p "${HOME}/dev-tools/Wezterm/install"
install -m 755 target/release/wezterm-mux-server "${HOME}/dev-tools/Wezterm/install/wezterm-mux-server"
install -m 755 target/release/wezterm "${HOME}/dev-tools/Wezterm/install/wezterm"
echo "# PATH for Wezterm" >> "${HOME}/.bashrc"
echo 'export PATH="${HOME}/dev-tools/Wezterm/install:${PATH}"' >> "${HOME}/.bashrc"
. "${HOME}/.bashrc"
wezterm --version
# 卸载 Developer Toolset 7,避免影响 clangd
sudo yum remove devtoolset-7-gcc
sudo yum autoremove
安装 Copilot CLI
Snap 装的有问题,只能自己写 Dockerfile 了。
FROM node:24-bookworm
RUN npm config set registry https://registry.npmmirror.com
RUN npm install -g @github/copilot
ENTRYPOINT ["copilot"]
docker build -t copilot --network host .
cat << 'EOF' >> "${HOME}/.bashrc"
alias copilot='docker run --rm -it \
--user "$(id -u):$(id -g)" \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/group:/etc/group:ro \
-v "${HOME}:${HOME}" \
-v "$(pwd):$(pwd)" \
-w "$(pwd)" \
--network host \
copilot'
EOF
. "${HOME}/.bashrc"
copilot --version
--user "$(id -u):$(id -g)":让容器内进程以当前用户身份运行,避免在挂载目录里生成root属主的文件。-v /etc/passwd:/etc/passwd:ro与-v /etc/group:/etc/group:ro:把宿主机的用户和组信息只读挂进容器,使容器正确解析当前uid与gid对应的用户名和组名。-v "${HOME}:${HOME}":挂载宿主机HOME,使容器可以读写当前用户的配置与缓存。-v "$(pwd):$(pwd)"与-w "$(pwd)":把当前工作目录挂进容器,并将容器启动后的工作路径配置为同一路径。--network host:直接复用宿主机网络。