CentOS7服务器运维踩坑记录

用CentOS7做服务器运维一段时间了,整理一下常用操作和踩过的坑。

磁盘分区与挂载

查看磁盘信息

首先使用fdisk -l命令查看系统中的磁盘信息:

1
fdisk -l

输出示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000cc4ad
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2050047 1024000 83 Linux
/dev/xvda2 2050048 22530047 10240000 83 Linux
/dev/xvda3 22530048 24578047 1024000 83 Linux
/dev/xvda4 24578048 83886079 29654016 5 Extended
/dev/xvda5 24580096 26628095 1024000 82 Linux swap / Solaris
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

关键字段说明:

字段 说明
Device 设备名称,如/dev/xvda
Boot 是否启动分区
Start 起始扇区
End 结束扇区
Blocks 块大小(1KB)
Id 分区类型ID
System 文件系统类型

创建新分区

以新挂载的数据盘/dev/xvdb为例:

步骤1:进入fdisk模式

1
fdisk /dev/xvdb

步骤2:创建新分区

输入n表示新增分区。

步骤3:选择分区类型

输入p选择主分区(最多4个)。

步骤4:选择分区编号

输入1选择编号1。

步骤5:设置起始扇区

直接回车使用默认值2048。

步骤6:设置结束扇区

直接回车使用全部空间。

步骤7:写入分区表

输入w保存。

同步分区表

1
partprobe

格式化分区

1
mkfs -t ext4 /dev/xvdb1

格式化过程输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621184 blocks
131059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

挂载分区

创建挂载目录:

1
mkdir /mnt/sdc

挂载:

1
mount /dev/xvdb1 /mnt/sdc

查看挂载结果:

1
df -TH

输出示例:

1
2
3
4
5
6
7
8
9
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/xvda2 xfs 11G 7.4G 3.2G 71% /
devtmpfs devtmpfs 4.1G 0 4.1G 0% /dev
tmpfs tmpfs 4.1G 82k 4.1G 1% /dev/shm
tmpfs tmpfs 4.1G 9.2M 4.1G 1% /run
tmpfs tmpfs 4.1G 0 4.1G 0% /sys/fs/cgroup
/dev/xvda3 xfs 1.1G 39M 1.1G 4% /home
/dev/xvda1 xfs 1.1G 131M 915M 13% /boot
/dev/xvdb1 ext4 11G 38M 9.9G 1% /mnt/sdc

设置开机自动挂载

注意: 不能直接在/etc/fstab中指定/dev/xvdb1,因为设备顺序可能变。推荐用UUID配置。

查询UUID:

1
blkid /dev/xvdb1

输出示例:

1
/dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"

编辑fstab文件:

1
vi /etc/fstab

添加内容:

1
UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc ext4 defaults 0 2

fstab字段说明:

字段 说明
UUID 分区唯一标识符
/mnt/sdc 挂载点
ext4 文件系统类型
defaults 挂载选项
0 dump备份设置
2 fsck检查顺序

DNS配置

查看网络连接

1
nmcli connection show

修改DNS服务器

1
nmcli con mod eth0 ipv4.dns "114.114.114.114 8.8.8.8"

注意:eth0需要替换为实际的连接名称。

使配置生效

1
nmcli con up eth0

验证配置

1
cat /etc/resolv.conf

时区配置

查看时区状态

1
timedatectl

列出所有时区

1
timedatectl list-timezones

设置系统时区

1
timedatectl set-timezone Asia/Shanghai

硬件时钟设置

1
2
3
4
5
# 设置为本地时间
timedatectl set-local-rtc 1

# 设置为UTC时间
timedatectl set-local-rtc 0

时间同步

1
2
3
4
5
# 安装ntp
yum -y install ntp

# 通过阿里云时间服务器校准时间
ntpdate ntp1.aliyun.com

常用NTP服务器:

服务器地址 说明
ntp1.aliyun.com 阿里云NTP服务器
ntp2.aliyun.com 阿里云NTP服务器
time.windows.com Windows时间服务器
cn.pool.ntp.org 中国NTP池

MySQL安装与配置

卸载现有MySQL/MariaDB

查询已安装的包:

1
2
rpm -qa | grep mysql
rpm -qa | grep mariadb

卸载MariaDB:

1
2
3
4
5
# 逐个卸载
rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

# 批量卸载(推荐)
rpm -qa | grep mariadb | xargs rpm -e --nodeps

安装MySQL YUM源

下载YUM源:

1
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

安装YUM源:

1
rpm -ivh mysql80-community-release-el7-3.noarch.rpm

安装MySQL服务器

1
yum -y install mysql-server

设置外部访问

MySQL安装后默认只允许localhost访问,如需外部访问需创建新用户:

1
2
3
4
5
-- 创建允许外部访问的用户
GRANT ALL PRIVILEGES ON *.* TO 'outUser'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

-- 刷新权限
FLUSH PRIVILEGES;

防火墙配置

1
2
3
4
5
# 开放MySQL端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent

# 重载防火墙
firewall-cmd --reload

Nginx部署与配置

安装Nginx

1
yum -y install nginx

403 Forbidden错误解决

查看错误日志:

1
cat /var/log/nginx/error.log

常见错误:

1
open() "/data/xxxx.xxx" failed (13: Permission denied), client: 192.168.1.2, server: www.xxxx.com

解决方案1:修改Nginx用户

查看Nginx启动用户:

1
ps aux | grep "nginx: worker process" | awk '{print $1}'

修改nginx.conf

1
user root;  # 改为root用户或其他有权限的用户

解决方案2:修改文件权限

1
chmod -R 777 /data/www/

解决方案3:关闭SELinux

查看SELinux状态:

1
/usr/sbin/sestatus

修改SELinux配置:

1
vi /etc/selinux/config

修改内容:

1
SELINUX=disabled

重启生效:

1
reboot

网络工具

获取外网IP

使用curl命令:

1
2
3
4
5
6
7
8
9
10
# 多种方式获取外网IP
curl ifconfig.me
curl ifconfig.me/ip
curl icanhazip.com
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i
curl ip.sb

输出示例:

1
123.45.67.89

网络诊断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 测试网络连通性
ping www.baidu.com

# 查看路由表
ip route

# 查看网络接口
ip addr

# 查看端口监听
netstat -tlnp

# 查看网络连接
ss -tlnp

常用数值单位

单位 英文 换算关系
B Byte 1 B
KB kilo 1 KB = 1024 B
MB mega 1 MB = 1024 KB
GB giga 1 GB = 1024 MB
TB tera 1 TB = 1024 GB
PB peta 1 PB = 1024 TB
EB exa 1 EB = 1024 PB

总结一下

CentOS7运维的核心技能就这些:

主题 关键命令/配置 注意事项
磁盘管理 fdisk, mkfs, mount 用UUID配置自动挂载
DNS配置 nmcli 确保使用正确的连接名
时区配置 timedatectl 用ntp同步时间
MySQL安装 yum, rpm 先卸载MariaDB
Nginx部署 yum 注意权限和SELinux
网络工具 curl, ping, netstat 多种方式获取外网IP

掌握这些后,日常的Linux服务器维护和故障排查基本能搞定了。

参考:

  1. CentOS官方文档:https://www.centos.org/
  2. MySQL官方文档:https://dev.mysql.com/doc/
  3. Nginx官方文档:https://nginx.org/en/docs/
  4. Linux man pages:https://man7.org/linux/man-pages/