服务器

云服务器操作手册

目录

  1. 系统初始化配置
  2. 用户和权限管理
  3. 网络和防火墙配置
  4. 服务部署和管理
  5. 安全配置
  6. 监控和维护
  7. 备份和恢复
  8. 常用命令速查

系统初始化配置

1. 系统更新

1
2
3
4
5
6
# CentOS/RHEL
yum update -y
yum install -y epel-release

# Ubuntu/Debian
apt update && apt upgrade -y

2. 安装常用工具

1
2
3
4
5
# CentOS/RHEL
yum install -y vim wget curl git net-tools htop iftop iotop lsof telnet nc

# Ubuntu/Debian
apt install -y vim wget curl git net-tools htop iftop iotop lsof telnet netcat

3. 时区配置

1
2
3
4
5
6
7
8
9
10
# 设置时区为上海
timedatectl set-timezone Asia/Shanghai

# 查看时区
timedatectl status

# 安装NTP服务
yum install -y ntp || apt install -y ntpdate
systemctl enable ntpd
systemctl start ntpd

用户和权限管理

1. 创建新用户

1
2
3
4
5
6
7
# 创建用户
useradd -m -s /bin/bash username
passwd username

# 添加到sudo组
usermod -aG wheel username # CentOS
usermod -aG sudo username # Ubuntu

2. SSH安全配置

1
2
3
4
5
6
7
8
9
10
11
12
13
vim /etc/ssh/sshd_config

# 重要配置项:
Port 2222 # 修改默认端口
PermitRootLogin no # 禁止root登录
PasswordAuthentication no # 禁止密码登录(仅密钥)
PubkeyAuthentication yes # 启用密钥认证
AllowUsers username # 只允许特定用户
MaxAuthTries 3 # 最大尝试次数
ClientAliveInterval 300 # 连接超时设置

# 重启SSH服务
systemctl restart sshd

3. 密钥对配置

1
2
3
4
5
# 本地生成密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 上传公钥到服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub username@server_ip -p 2222

网络和防火墙配置

1. 防火墙管理 (firewalld)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看状态
systemctl status firewalld
firewall-cmd --state

# 基本操作
systemctl start firewalld
systemctl enable firewalld
systemctl stop firewalld

# 端口管理
firewall-cmd --list-all # 查看所有规则
firewall-cmd --zone=public --add-port=80/tcp --permanent # 添加端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent # 删除端口
firewall-cmd --reload # 重载配置

# 服务管理(推荐方式)
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent
firewall-cmd --reload

2. 防火墙管理 (iptables)

1
2
3
4
5
6
7
8
9
10
11
12
# 查看规则
iptables -L -n

# 保存规则(CentOS)
service iptables save

# 常用规则示例
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -P INPUT DROP

3. 网络诊断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 端口监听检查
netstat -tunlp
ss -tunlp

# 连接测试
telnet host port
nc -zv host port

# 路由跟踪
traceroute domain.com
tracepath domain.com

# 带宽测试
speedtest-cli

服务部署和管理

1. Web服务环境

Nginx安装配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# CentOS
yum install -y nginx
systemctl enable nginx
systemctl start nginx

# Ubuntu
apt install -y nginx
systemctl enable nginx
systemctl start nginx

# 配置文件位置
/etc/nginx/nginx.conf
/etc/nginx/conf.d/
/var/www/html/

虚拟主机配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

# 静态文件
location /static/ {
alias /var/www/static/;
expires 30d;
}
}

2. 数据库服务

MySQL安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# CentOS
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum localinstall mysql80-community-release-el7-3.noarch.rpm
yum install -y mysql-community-server

# Ubuntu
apt install -y mysql-server

# 安全配置
mysql_secure_installation

# 服务管理
systemctl enable mysqld
systemctl start mysqld

Redis安装

1
2
3
4
5
6
7
8
9
# CentOS
yum install -y redis
systemctl enable redis
systemctl start redis

# Ubuntu
apt install -y redis-server
systemctl enable redis-server
systemctl start redis-server

3. 应用部署

Node.js应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装Node.js
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs

# 部署应用
cd /var/www/app
npm install
npm start

# 使用PM2管理
npm install -g pm2
pm2 start app.js
pm2 startup
pm2 save

Docker环境

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装Docker
curl -fsSL https://get.docker.com | bash
systemctl enable docker
systemctl start docker

# Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# 常用命令
docker ps
docker images
docker logs container_name

安全配置

1. 系统安全加固

1
2
3
4
5
6
7
8
9
10
11
# 禁用不必要的服务
systemctl disable bluetooth
systemctl disable cups

# 配置SSH加固
vim /etc/ssh/sshd_config

# 安装fail2ban防暴力破解
yum install -y fail2ban || apt install -y fail2ban
systemctl enable fail2ban
systemctl start fail2ban

2. 安全扫描

1
2
3
4
5
6
7
8
# 安装lynis安全扫描工具
yum install -y lynis || apt install -y lynis

# 执行安全扫描
lynis audit system

# 检查开放端口
nmap localhost

3. 日志监控

1
2
3
4
5
6
7
8
9
# 重要日志文件
/var/log/secure # SSH登录日志
/var/log/messages # 系统消息
/var/log/nginx/ # Nginx日志
/var/log/mysql/ # MySQL日志

# 实时监控日志
tail -f /var/log/secure
journalctl -f

监控和维护

1. 系统监控

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装htop进行进程监控
yum install -y htop || apt install -y htop

# 磁盘空间检查
df -h
du -sh /path/to/directory

# 内存使用情况
free -h

# 系统负载
uptime
cat /proc/loadavg

2. 性能分析

1
2
3
4
5
6
7
8
9
10
# CPU使用率 top
top

# IO监控
iostat -x 1
iotop

# 网络监控
iftop
nethogs

3. 定时任务

1
2
3
4
5
6
7
8
9
10
11
# 编辑定时任务
crontab -e

# 示例:每天备份
0 2 * * * /root/scripts/backup.sh

# 示例:每周清理日志
0 3 * * 0 /root/scripts/clean_logs.sh

# 查看定时任务日志
tail -f /var/log/cron

备份和恢复

1. 文件备份脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# backup.sh

BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d_%H%M%S)
TAR_FILE="backup_$DATE.tar.gz"

# 备份重要目录
tar -czf $BACKUP_DIR/$TAR_FILE \
/etc/ \
/var/www/ \
/home/ \
/root/scripts/

# 保留最近7天的备份
find $BACKUP_DIR -name "backup_*.tar.gz" -mtime +7 -delete

echo "Backup completed: $TAR_FILE"

2. 数据库备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# mysql_backup.sh

BACKUP_DIR="/backup/mysql"
DATE=$(date +%Y%m%d_%H%M%S)
USER="root"
PASSWORD="your_password"

# 备份所有数据库
mysqldump -u$USER -p$PASSWORD --all-databases > $BACKUP_DIR/full_backup_$DATE.sql

# 压缩备份文件
gzip $BACKUP_DIR/full_backup_$DATE.sql

# 保留最近7天备份
find $BACKUP_DIR -name "full_backup_*.sql.gz" -mtime +7 -delete

3. 自动化备份配置

1
2
3
4
5
6
7
8
# 添加到crontab
crontab -e

# 每天凌晨2点执行文件备份
0 2 * * * /root/scripts/backup.sh

# 每天凌晨3点执行数据库备份
0 3 * * * /root/scripts/mysql_backup.sh

常用命令速查

系统信息

1
2
3
4
5
uname -a              # 系统信息
cat /etc/redhat-release # 系统版本
hostname # 主机名
whoami # 当前用户
uptime # 运行时间

进程管理

1
2
3
4
ps aux | grep nginx   # 查找进程
kill -9 PID # 强制杀死进程
pkill process_name # 按名称杀进程
nice -n 10 command # 调整优先级

文件操作

1
2
3
4
5
find / -name "filename"      # 查找文件
grep "pattern" /path/file # 文件内容搜索
tar -czf archive.tar.gz /dir # 压缩
tar -xzf archive.tar.gz # 解压
rsync -av source/ dest/ # 同步目录

网络工具

1
2
3
4
ping host            # 网络连通性
curl -I URL # HTTP头部信息
wget URL # 下载文件
scp file user@host:/path # 安全复制

权限管理

1
2
3
4
chmod 755 file       # 修改文件权限
chown user:group file # 修改属主属组
chmod +x script.sh # 添加执行权限
umask 022 # 设置默认权限

系统服务

1
2
3
4
5
6
systemctl status service_name    # 服务状态
systemctl start service_name # 启动服务
systemctl stop service_name # 停止服务
systemctl restart service_name # 重启服务
systemctl enable service_name # 开机自启
systemctl disable service_name # 禁用自启

紧急情况处理

1. 无法连接服务器

  • 检查云服务商控制台
  • 查看VNC控制台
  • 检查安全组规则
  • 验证网络连通性

2. 磁盘空间不足

1
2
3
4
5
6
# 查找大文件
find / -type f -size +100M
# 清理日志文件
find /var/log -name "*.log" -mtime +7 -delete
# 清理缓存
yum clean all || apt clean

3. 性能问题诊断

1
2
# 快速诊断脚本
wget -qO- https://raw.githubusercontent.com/soyking/linux-check/master/linux-check.sh | bash

这份手册涵盖了云服务器管理的各个方面,建议根据实际需求选择相应的章节进行参考。记得定期更新和维护服务器,确保系统安全稳定运行。