Linux初始化

Sunmy Lv5

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
# Linux 系统初始化配置脚本 - VMware增强版
# 功能:安装常用工具 + 自动挂载VMware共享文件夹

# 定义颜色代码
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CYAN='\033[36m'
RESET='\033[0m'

# 检查root权限
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
}

# 配置FUSE权限
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'
#!/bin/bash
# 自动挂载VMware共享文件夹

# 检查工具是否可用
if ! command -v vmware-hgfsclient &>/dev/null; then
exit 0
fi

# 创建主挂载点
mkdir -p /mnt/hgfs

# 获取共享文件夹列表
folders=$(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

以管理员权限运行

1
sudo ./linux_init.sh

重启系统

1
sudo reboot

Docker安装

更新软件包并安装必要软件

1
2
sudo apt update
sudo 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.sh

# 执行安装
sudo ./docker-install.sh

配置docker

  1. 配置用户组,将当前用户添加到docker组,避免没有权限启动
1
2
3
sun@ldspdvs:~$ sudo usermod -aG docker $USER
# 重启才会生效
sun@ldspdvs:~$ sudo reboot
  1. 配置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
# 直接复制进去并保存 (2025.2.15最新可用源,看到这里的同学评论一下是否还可用,不可用我会更新)
{
"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"
]
}
# 启动/重启docker
sudo systemctl daemon-reload
#如果还没启动
sudo systemctl start docker
#如果已经启动
sudo systemctl restart docker
# docker开机自启
sudo systemctl enable docker

# 验证镜像加速是否修改 查看Registry Mirrors部分
sudo docker info

  1. 验证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. 创建文件夹
1
2
3
4
5
sudo vim /etc/profile

# 新增以下内容
# gitlab基础目录
export GITLAB_HOME=/usr/local/docker/gitlab
  1. 进入创建目录
1
cd /usr/local/docker/gitlab/
  1. 刷新环境变量
1
source /etc/profile
  1. 输出当前环境变量
1
echo $GITLAB_HOME /usr/local/docker/gitlab

创建启动脚本

  1. 脚本内容
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. 脚本赋权及执行
1
chmod 777 startGitlab.sh
  1. 查看容器日志
1
docker logs -f -t --tail -f gitlab

Gitlab登录

  1. 查看密码
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 的密码:
# WARNING: This value is valid only in the following conditions
# 1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
# 2. Password hasn't been changed manually, either via UI or via command line.
#
# If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

Password: OjDxbgBuN208fxdMrPIMK4P0RCjBeThRc4pYbjQigoY=

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
sun@ldspdvs:/usr/local/docker/gitlab/config$
  1. 登录网址

    IP:192.168.111.128:18080

    用户名:root

    密码:OjDxbgBuN208fxdMrPIMK4P0RCjBeThRc4pYbjQigoY=

客户端登陆Gitlab

  1. 生成SSH密钥
1
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. 将公钥添加到Gitlab
1
cat ~/.ssh/id_ed25519.pub
  1. 配置Git全局设置
1
2
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
  1. 克隆仓库
1
git clone git@<GITLAB_HOST>:<GROUP>/<REPO>.git
  1. 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
#+++++++++++++++++++++++++++++++++++++++++++++++++++++

# 重启Gitlab
root@SQA-JCRJ-02:/# docker restart gitlab

按以上配置的效果是可以使用端口访问gitlab的web服务
  • 标题: Linux初始化
  • 作者: Sunmy
  • 创建于 : 2025-05-05 15:52:48
  • 更新于 : 2025-06-29 21:05:24
  • 链接: https://ldspdvsun.github.io/cmchvp8fq000jh0gb4dx28rqs/
  • 版权声明: 版权所有 © Sunmy,禁止转载。
评论