CentOS安装常用命令+yum配置国内镜像源+Docker安装并配置国内镜像源完整教程
适用于:
- CentOS 7
- VMware ESXi
- VirtualBox
- 裸机服务器
- 阿里云 / 腾讯云 / 华为云
一、一键安装 Linux 常用工具
安装常见运维工具
(如果yum源需要调整为国内源请看第二步)
yum install -y \
net-tools \
traceroute \
telnet \
bind-utils \
wget \
lsof \
tcpdump \
tree \
vim \
nano \
zip \
unzip \
rsync \
wget
工具说明
| 工具 | 作用 |
|---|---|
| net-tools | ifconfig / netstat |
| traceroute | 路由追踪 |
| telnet | 端口测试 |
| bind-utils | dig/nslookup |
| wget | 下载文件 |
| lsof | 查看端口占用 |
| tcpdump | 抓包 |
| tree | 树形目录 |
| vim/nano | 编辑器 |
| zip/unzip | 压缩解压 |
| rsync | 文件同步 |
二、配置 YUM 镜像源
1、先备份YUM源文件 (如果YUM源无法使用,可以看这步配置为腾讯国内源。可跳过)
备份/etc/yum.repos.d下的文件,养成习惯,这里以腾讯的镜像源为例
如下:
CentOS-Base.repo
CentOS-Epel.repo
cwp.repo
epel.repo
epel-testing.repo
cd /etc/yum.repos.d
# 备份
mkdir -p /backup/yum-repo-backup
cp -a *.repo /backup/yum-repo-backup/
# 替换腾讯源
sed -i 's|http://mirror.centos.org|http://mirrors.tencent.com|g' *.repo
sed -i 's|https://mirror.centos.org|http://mirrors.tencent.com|g' *.repo
sed -i 's|http://download.fedoraproject.org/pub|http://mirrors.tencent.com|g' *.repo
sed -i 's|https://download.fedoraproject.org/pub|http://mirrors.tencent.com|g' *.repo
sed -i 's|http://dl.fedoraproject.org/pub|http://mirrors.tencent.com|g' *.repo
sed -i 's|https://dl.fedoraproject.org/pub|http://mirrors.tencent.com|g' *.repo
2、刷新 YUM 缓存
# 刷新
yum clean all
yum makecache
# 查看源是否生效
yum repolist
三、安装 EPEL 仓库 (额外软件包:例如增强版top命令)
EPEL 提供大量额外软件包
安装:
yum install -y epel-release
刷新缓存:
yum makecache
四、安装 htop(增强版 top)

yum install -y htop
启动:
htop
五、CentOS 7 安装 Docker
1、创建安装脚本
创建文件:
vim install_docker_centos7.sh
2、写入以下脚本内容
#!/bin/bash
echo "========== 1. 安装依赖 =========="
yum install -y yum-utils device-mapper-persistent-data lvm2
echo "========== 2. 配置阿里云 Docker 仓库 =========="
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
echo "========== 3. 手动导入 GPG 密钥(避免网络问题) =========="
rpm --import https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
echo "========== 4. 安装 Docker =========="
yum install -y docker-ce docker-ce-cli containerd.io
echo "========== 5. 配置镜像加速 =========="
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << 'JSON'
{
"registry-mirrors": [
"https://docker.1panel.live",
"https://mirror.ccs.tencent.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}
JSON
echo "========== 6. 启动 Docker =========="
systemctl daemon-reload
systemctl start docker
systemctl enable docker
echo "========== 7. 验证安装 =========="
docker --version
systemctl status docker --no-pager
echo ""
echo "========== 安装完成 =========="
echo "Docker 版本: $(docker --version)"
echo "镜像加速:"
docker info 2>/dev/null | grep -A 5 "Registry Mirrors"
3、赋予执行权限
chmod +x install_docker_centos7.sh
4、执行安装脚本
./install_docker_centos7.sh
六、验证 Docker 是否安装成功
查看版本:
docker --version
查看运行状态:
systemctl status docker
查看镜像加速:
docker info
七、Docker 常用命令
查看 Docker 信息
docker info
查看镜像
docker images
查看容器
docker ps
查看全部容器:
docker ps -a
启动 Docker
systemctl start docker
停止 Docker
systemctl stop docker
重启 Docker
systemctl restart docker
开机自启
systemctl enable docker
八、解决 Docker 命令 TAB 无法补全
1、安装 bash-completion
yum install -y bash-completion
2、重新加载环境变量
source /etc/profile
3、重新登录终端
退出 SSH:
exit
重新登录即可。
九、推荐安装的额外工具
安装 Git
yum install -y git
安装 jq(JSON 格式化)
yum install -y jq
安装 screen
yum install -y screen
安装 nc 网络工具
yum install -y nc
十、推荐目录规划
| 目录 | 用途 |
|---|---|
| /data | 数据目录 |
| /data/docker | Docker 数据 |
| /data/logs | 日志目录 |
| /opt | 软件目录 |
| /backup | 备份目录 |
十一、推荐关闭防火墙(测试环境)
生产环境请谨慎操作
停止防火墙:
systemctl stop firewalld
关闭开机启动:
systemctl disable firewalld
查看状态:
systemctl status firewalld
十二、推荐关闭 SELinux(测试环境)
查看状态:
getenforce
临时关闭:
setenforce 0
永久关闭:
编辑:
vim /etc/selinux/config
修改:
SELINUX=disabled
重启服务器:
reboot
十三、完整初始化命令(可直接复制)
# 刷新缓存
yum clean all
yum makecache
# 安装常用工具
yum install -y \
net-tools \
traceroute \
telnet \
bind-utils \
wget \
lsof \
tcpdump \
tree \
vim \
nano \
zip \
unzip \
rsync \
bash-completion
# 安装 EPEL
yum install -y epel-release
yum makecache
# 安装 htop
yum install -y htop
# Docker TAB 补全
source /etc/profile
十四、最终效果
初始化完成后:
- YUM 源正常
- 常见 Linux 工具齐全
- htop 可用
- Docker 已安装
- Docker 镜像加速完成
- Docker 开机自启
- TAB 自动补全正常
适合:
- Java 项目部署
- Redis
- MySQL
- Docker Compose
- Kubernetes
- Nginx
- Spring Boot
- 微服务环境

浙公网安备 33010602011771号