Linux 初始化配置
此脚本主要用于Linux第一次安装后的系统环境配置,包括但不限于常用工具的安装以及共享文件夹的初始化配置
创建脚本
首先,创建一个脚本文件,例如 linux_init.sh:
1 sudo vim ./linux_init.sh
添加以下内容到脚本中
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 #!/bin/bash RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' BLUE='\033[34m' CYAN='\033[36m' RESET='\033[0m' check_root () { if [ "$EUID " -ne 0 ]; then echo -e "${RED} ✗ 错误:请使用sudo或root用户运行此脚本${RESET} " exit 1 fi } declare -A PKGS=( ["VMware工具" ]="open-vm-tools open-vm-tools-desktop" ["网络工具" ]="ssh ifplugd net-tools iputils-ping" ["开发工具" ]="build-essential gcc gdb git apt-transport-https" ["系统工具" ]="tree htop curl wget" ["调试工具" ]="strace ltrace valgrind" ["其他工具" ]="vim tmux" ) install_packages () { echo -e "\n${CYAN} ▶ 开始软件包安装流程 ◀${RESET} " echo -ne "${BLUE} ⟳ 更新软件源缓存..." if apt-get update -qq >/dev/null 2>&1; then echo -e "\r${GREEN} ✓ 软件源更新完成${RESET} " else echo -e "\r${RED} ✗ 软件源更新失败${RESET} " exit 1 fi for category in "${!PKGS[@]} " ; do echo -e "\n${BLUE} ● 安装 ${category} 组件:${RESET} " for pkg in ${PKGS[$category]} ; do echo -ne " ${YELLOW} ▶${RESET} ${pkg} " if dpkg -l | grep -q "^ii ${pkg} " ; then echo -e "[${GREEN} 已存在${RESET} ]" continue fi if apt-get install -y -qq $pkg >/dev/null 2>&1; then echo -e "\r ${GREEN} ✓${RESET} ${pkg} [${GREEN} 安装成功${RESET} ]" else echo -e "\r ${RED} ✗${RESET} ${pkg} [${RED} 安装失败${RESET} ]" echo -e "${YELLOW} 建议手动执行:sudo apt-get install -y ${pkg} ${RESET} " exit 1 fi done done } setup_fuse () { echo -e "\n${CYAN} ▶ 配置文件系统权限 ◀${RESET} " if grep -q "^user_allow_other" /etc/fuse.conf; then echo -e "${GREEN} ✓ FUSE 已允许用户挂载${RESET} " else echo -ne "${BLUE} ⟳ 正在配置FUSE权限..." echo "user_allow_other" >> /etc/fuse.conf echo -e "\r${GREEN} ✓ FUSE 权限配置完成${RESET} " fi } create_mount_script () { echo -e "\n${CYAN} ▶ 配置VMware共享文件夹挂载 ◀${RESET} " cat > /usr/local/bin/mount_hgfs.sh <<'EOF' if ! command -v vmware-hgfsclient &>/dev/null; then exit 0 fi mkdir -p /mnt/hgfsfolders=$(vmware-hgfsclient) for folder in $folders ; do mount_point="/mnt/hgfs/${folder} " if mount | grep -q " ${mount_point} " ; then continue fi mkdir -p "$mount_point " if vmhgfs-fuse -o allow_other,uid=$(id -u),gid=$(id -g) \ ".host:/${folder} " "$mount_point " ; then echo "已挂载: ${mount_point} " else echo "挂载失败: ${mount_point} " >&2 fi done EOF chmod +x /usr/local/bin/mount_hgfs.sh echo -e "${GREEN} ✓ 挂载脚本已部署到 /usr/local/bin/mount_hgfs.sh${RESET} " } setup_systemd_service () { echo -e "\n${CYAN} ▶ 配置系统服务 ◀${RESET} " cat > /etc/systemd/system/mount_hgfs.service <<EOF [Unit] Description=Mount VMware Shared Folders After=network.target [Service] Type=oneshot ExecStart=/usr/local/bin/mount_hgfs.sh RemainAfterExit=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable mount_hgfs.service >/dev/null if systemctl start mount_hgfs.service; then echo -e "${GREEN} ✓ 服务已成功启用并启动${RESET} " else echo -e "${RED} ✗ 服务启动失败,请检查以下日志:${RESET} " journalctl -u mount_hgfs.service -n 10 --no-pager exit 1 fi } final_check () { echo -e "\n${CYAN} ▶ 最终验证 ◀${RESET} " echo -ne "${BLUE} ⟳ 加载VMware内核模块..." if modprobe vmw_vmci vmwgfx; then echo -e "\r${GREEN} ✓ 内核模块加载成功${RESET} " else echo -e "\r${YELLOW} ⚠ 部分内核模块加载失败(不影响基础功能)${RESET} " fi echo -e "\n${BLUE} 检测到的共享文件夹列表:${RESET} " if command -v vmware-hgfsclient &>/dev/null; then vmware-hgfsclient | sed 's/^/ ➔ /' echo -e "\n${BLUE} 实际挂载点状态:${RESET} " mount | grep vmhgfs-fuse || echo -e "${YELLOW} 尚未挂载任何共享文件夹${RESET} " else echo -e "${RED} ✗ vmware-hgfsclient 命令不可用${RESET} " fi } main () { clear echo -e "${CYAN} ═══════════════════════════════════════════════${RESET} " echo -e "${CYAN} Linux 系统初始化配置脚本 ${RESET} " echo -e "${CYAN} ═══════════════════════════════════════════════${RESET} " check_root install_packages setup_fuse create_mount_script setup_systemd_service final_check echo -e "\n${GREEN} ✅ 所有配置已完成!建议重启系统使配置生效${RESET} " } main
赋予脚本执行权限
1 sudo chmod +x ./linux_init.sh
以管理员权限运行
重启系统
Docker安装
更新软件包并安装必要软件
1 2 sudo apt updatesudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
导入 Docker 官方 GPG 密钥
1 2 sun@ldspdvs:~$ sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - sun@ldspdvs:~$
添加阿里云的docker仓库
1 sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
更新软件包列表
1 2 3 4 5 6 7 8 9 10 sun@ldspdvs:~$ sudo apt update 命中:1 http://mirrors.huaweicloud.com/repository/ubuntu noble InRelease 命中:2 http://mirrors.huaweicloud.com/repository/ubuntu noble-updates InRelease 命中:3 http://mirrors.huaweicloud.com/repository/ubuntu noble-backports InRelease 命中:4 http://security.ubuntu.com/ubuntu noble-security InRelease 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 有 1 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。 sun@ldspdvs:~$
安装Docker
1 2 3 4 5 6 7 8 9 10 11 12 sun@ldspdvs:~$ sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin curl -sSL https://gitee.com/SuperManito/LinuxMirrors/raw/main/DockerInstallation.sh -o docker-install.sh chmod +x docker-install.shsudo ./docker-install.sh
配置docker
配置用户组,将当前用户添加到docker组,避免没有权限启动
1 2 3 sun@ldspdvs:~$ sudo usermod -aG docker $USER sun@ldspdvs:~$ sudo reboot
配置docker加速
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 sun@ldspdvs:~$ sudo vi /etc/docker/daemon.json { "registry-mirrors" : [ "https://docker.hpcloud.cloud" , "https://docker.m.daocloud.io" , "https://docker.unsee.tech" , "https://docker.1panel.live" , "http://mirrors.ustc.edu.cn" , "https://docker.chenby.cn" , "http://mirror.azure.cn" , "https://dockerpull.org" , "https://dockerhub.icu" , "https://hub.rat.dev" , "https://proxy.1panel.live" , "https://docker.1panel.top" , "https://docker.m.daocloud.io" , "https://docker.1ms.run" , "https://docker.ketches.cn" ] } sudo systemctl daemon-reload sudo systemctl start dockersudo systemctl restart dockersudo systemctl enable dockersudo docker info
验证docoker
1 sudo docker run hello-world
Gitlab部署
镜像下载
下载镜像
1 docker pull registry.gitlab.cn/omnibus/gitlab-jh:15.11.13
创建文件夹
1 sudo mkdir -p /usr/local/docker/gitlab
配置环境变量
创建文件夹
1 2 3 4 5 sudo vim /etc/profileexport GITLAB_HOME=/usr/local/docker/gitlab
进入创建目录
1 cd /usr/local/docker/gitlab/
刷新环境变量
输出当前环境变量
1 echo $GITLAB_HOME /usr/local/docker/gitlab
创建启动脚本
脚本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 tee startGitlab.sh << "end" docker run -d \ --privileged=true \ --hostname 192.168.111.128 \ -p 18443:443 -p 18080:80 -p 18022:22 \ --name gitlab \ --restart always \ -v $GITLAB_HOME /config:/etc/gitlab \ -v $GITLAB_HOME /logs:/var/log/gitlab \ -v $GITLAB_HOME /data:/var/opt/gitlab \ --shm-size 512m \ registry.gitlab.cn/omnibus/gitlab-jh:15.11.13 end
脚本赋权及执行
1 chmod 777 startGitlab.sh
查看容器日志
1 docker logs -f -t --tail -f gitlab
Gitlab登录
查看密码
1 2 3 4 5 6 7 8 9 10 11 12 13 sun@ldspdvs: cd /usr/local/docker/gitlab/config sun@ldspdvs:/usr/local/docker/gitlab/config$ sudo cat initial_root_password [sudo ] sun 的密码: Password: OjDxbgBuN208fxdMrPIMK4P0RCjBeThRc4pYbjQigoY= sun@ldspdvs:/usr/local/docker/gitlab/config$
登录网址
IP:192.168.111.128:18080
用户名:root
密码:OjDxbgBuN208fxdMrPIMK4P0RCjBeThRc4pYbjQigoY=
客户端登陆Gitlab
生成SSH密钥
1 ssh-keygen -t ed25519 -C "your_email@example.com"
将公钥添加到Gitlab
1 cat ~/.ssh/id_ed25519.pub
配置Git全局设置
1 2 git config --global user.name "Your Name" git config --global user.email "your_email@example.com"
克隆仓库
1 git clone git@<GITLAB_HOST>:<GROUP>/<REPO>.git
Gitlab http 克隆地址更改为本地IP
1 2 3 4 5 6 7 8 9 10 11 12 13 root@SQA-JCRJ-02:/# find /newFS/docker/gitlab/ -name gitlab.rb root@SQA-JCRJ-02:/# vim /newFS/docker/gitlab/data/etc/gitlab.rb external_url 'http://192.168.2.136:18080' nginx['listen_port' ] = 80 root@SQA-JCRJ-02:/# docker restart gitlab 按以上配置的效果是可以使用端口访问gitlab的web服务