刚买的阿里云服务器,CentOS 7.9系统,初始配置该怎么做?

恭喜你购买了阿里云服务器!CentOS 7.9 是一个稳定且广泛使用的 Linux 发行版,适合搭建 Web 服务、数据库、应用等。以下是新购阿里云服务器(ECS)后的初始安全与基础配置建议,帮助你快速、安全地使用。


✅ 一、连接服务器

1. 获取公网 IP 和登录凭证

  • 登录 阿里云控制台
  • 找到你的实例,查看 公网 IP 地址
  • 获取登录密码(如果是密钥对,则使用 .pem 文件)

2. 使用 SSH 登录

ssh root@你的公网IP

如果使用密钥:

ssh -i /path/to/your-key.pem root@你的公网IP

提示:首次登录建议使用 root 用户,后续创建普通用户更安全。


✅ 二、系统基础设置

1. 修改 root 密码(确保安全)

passwd

2. 创建普通用户(推荐)

useradd yourusername
passwd yourusername

赋予 sudo 权限:

usermod -aG wheel yourusername

后续操作建议用普通用户 + sudo,避免误操作。


✅ 三、更新系统

yum update -y

更新所有软件包,包括内核和安全补丁。


✅ 四、配置防火墙(firewalld)

CentOS 7 默认使用 firewalld

1. 启动并启用 firewalld

systemctl start firewalld
systemctl enable firewalld

2. 开放常用端口

# 开放 SSH(22)
firewall-cmd --permanent --add-port=22/tcp

# 开放 HTTP(80)
firewall-cmd --permanent --add-port=80/tcp

# 开放 HTTPS(443)
firewall-cmd --permanent --add-port=443/tcp

# 重启防火墙生效
firewall-cmd --reload

⚠️ 建议不要开放不必要的端口(如 3389、445 等)。


✅ 五、禁用 root 远程登录(提高安全性)

1. 编辑 SSH 配置文件

vi /etc/ssh/sshd_config

修改以下项:

PermitRootLogin no
PasswordAuthentication no         # 如果你用密钥登录,建议关闭密码登录
PubkeyAuthentication yes

2. 重启 SSH 服务

systemctl restart sshd

⚠️ 操作前请确保你已配置好普通用户的密钥登录,否则可能无法再次登录!


✅ 六、配置时区和时间同步

1. 设置时区(例如上海)

timedatectl set-timezone Asia/Shanghai

2. 启用 NTP 时间同步

timedatectl set-ntp yes

验证:

timedatectl status

✅ 七、安装常用工具

yum install -y vim wget curl net-tools htop git bash-completion

✅ 八、配置 Swap(可选,尤其内存小的实例)

如果你的 ECS 内存较小(如 1GB),建议添加 swap:

# 创建 2GB 的 swap 文件
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# 永久挂载
echo '/swapfile none swap sw 0 0' >> /etc/fstab

✅ 九、安全加固建议

1. 安装 Fail2ban(防暴力破解)

yum install -y epel-release
yum install -y fail2ban

systemctl start fail2ban
systemctl enable fail2ban

默认保护 SSH,配置文件在 /etc/fail2ban/jail.local

2. 检查监听端口

netstat -tuln

确保只有必要的服务在监听。


✅ 十、备份与监控(进阶)

  • 定期快照(通过阿里云控制台)
  • 安装 rsyncrclone 做远程备份
  • 使用 cron 定时任务做日志清理或备份
  • 考虑部署监控工具如 Prometheus + Node Exporter 或使用阿里云云监控

✅ 十一、其他建议

项目 建议
DNS 解析 在阿里云域名控制台绑定公网 IP
域名备案 如需国内访问网站,记得备案
SELinux 可保持开启(enforcing),除非应用冲突
日志管理 查看 /var/log/secure/var/log/messages

✅ 总结:初始配置 checklist

✅ SSH 登录成功
✅ 修改 root 密码
✅ 创建普通用户 + sudo 权限
✅ 系统更新 yum update -y
✅ 配置防火墙(开放 80/443/22)
✅ 禁用 root 远程登录 + 关闭密码登录(用密钥)
✅ 设置时区和时间同步
✅ 安装常用工具(vim, wget, git 等)
✅ 配置 swap(可选)
✅ 安装 fail2ban
✅ 快照备份一次


完成以上步骤后,你的 CentOS 7.9 服务器就具备了基本的安全性和可用性,可以开始部署 Web 服务(如 Nginx、Apache)、数据库(MySQL/PostgreSQL)或运行自己的应用了。

需要我帮你写一个自动化初始化脚本吗?或者下一步部署 LNMP/LAMP?欢迎继续提问!

未经允许不得转载:云知道CLOUD » 刚买的阿里云服务器,CentOS 7.9系统,初始配置该怎么做?