最近在学习服务器运维,整理了一些入门阶段的基础知识和常用操作,方便日后查阅。主要是 Linux 服务器的日常运维,适合刚接触服务器的朋友参考。
一、服务器基础概念
1.1 什么是服务器
简单说就是一台 24 小时开机的电脑,通过网络提供服务。常见的有:
- 云服务器:阿里云、腾讯云、AWS 等,弹性伸缩,按需付费
- 物理服务器:自己买的硬件,性能独享,成本高
- VPS:虚拟专用服务器,介于两者之间
1.2 常见操作系统
- CentOS:稳定,企业常用(注意:CentOS 8 已停止维护)
- Ubuntu Server:社区活跃,适合新手
- Debian:轻量稳定,资源占用低
- AlmaLinux/Rocky:CentOS 的替代方案
二、SSH 连接与管理
2.1 基本连接
# 基本连接
ssh username@server_ip
# 指定端口
ssh -p 2222 username@server_ip
# 使用密钥连接
ssh -i ~/.ssh/id_rsa username@server_ip
2.2 生成 SSH 密钥
# 生成密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"
# 复制公钥到服务器
ssh-copy-id username@server_ip
2.3 SSH 配置优化
编辑 /etc/ssh/sshd_config:
# 禁用密码登录(推荐)
PasswordAuthentication no
# 修改默认端口
Port 2222
# 禁止 root 登录
PermitRootLogin no
# 重启 SSH 服务
systemctl restart sshd
三、常用 Linux 命令
3.1 系统信息
# 查看系统信息
uname -a
cat /etc/os-release
# 查看 CPU 信息
lscpu
# 查看内存
free -h
# 查看磁盘
df -h
du -sh /*
# 查看运行时间
uptime
3.2 进程管理
# 查看进程
top
htop
ps aux
# 查找进程
ps aux | grep nginx
# 结束进程
kill PID
kill -9 PID # 强制结束
# 查看端口占用
netstat -tulpn
ss -tulpn
lsof -i :80
3.3 文件操作
# 查找文件
find / -name "filename"
locate filename
# 查看文件内容
cat filename
tail -f /var/log/syslog # 实时查看日志
less filename # 分页查看
# 压缩解压
tar -czvf archive.tar.gz folder/
tar -xzvf archive.tar.gz
zip -r archive.zip folder/
unzip archive.zip
3.4 网络相关
# 查看 IP
ip addr
ifconfig
# 测试连通性
ping google.com
traceroute google.com
# 下载文件
wget https://example.com/file.zip
curl -O https://example.com/file.zip
# 端口测试
telnet ip port
nc -zv ip port
四、软件包管理
4.1 APT(Debian/Ubuntu)
# 更新软件源
apt update
# 升级软件
apt upgrade
# 安装软件
apt install package_name
# 删除软件
apt remove package_name
apt purge package_name # 彻底删除
# 搜索软件
apt search keyword
4.2 YUM/DNF(CentOS/RHEL)
# 更新软件源
dnf makecache
# 升级系统
dnf upgrade
# 安装软件
dnf install package_name
# 删除软件
dnf remove package_name
# 搜索软件
dnf search keyword
五、服务管理(Systemd)
# 查看服务状态
systemctl status nginx
# 启动/停止/重启
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
# 设置开机自启
systemctl enable nginx
systemctl disable nginx
# 查看日志
journalctl -u nginx
journalctl -u nginx -f # 实时查看
# 查看所有服务
systemctl list-units --type=service
六、服务器安全基础
6.1 防火墙配置
# UFW(Ubuntu)
ufw status
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
# Firewalld(CentOS)
firewall-cmd --list-all
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
6.2 定期更新
# 设置自动更新(Ubuntu)
apt install unattended-upgrades
# 手动更新习惯
apt update && apt upgrade -y # 每周执行一次
6.3 日志监控
# 查看登录日志
cat /var/log/auth.log # Debian/Ubuntu
cat /var/log/secure # CentOS/RHEL
# 查看失败登录
lastb
# 查看成功登录
last
七、数据备份
7.1 本地备份
# 备份网站文件
tar -czvf /backup/www_$(date +%Y%m%d).tar.gz /var/www/
# 备份数据库
mysqldump -u root -p database_name > backup.sql
# 定时备份(crontab)
0 2 * * * tar -czvf /backup/www_$(date +\%Y\%m\%d).tar.gz /var/www/
7.2 远程备份
# 使用 rsync 同步到另一台服务器
rsync -avz /var/www/ user@backup_server:/backup/www/
# 使用 scp 传输
scp backup.tar.gz user@backup_server:/backup/
八、常用软件部署
8.1 Nginx
# 安装
apt install nginx
# 配置文件位置
/etc/nginx/nginx.conf
/etc/nginx/sites-available/
# 测试配置
nginx -t
# 重载配置
systemctl reload nginx
8.2 Docker
# 安装 Docker
curl -fsSL https://get.docker.com | sh
# 启动 Docker
systemctl start docker
systemctl enable docker
# 常用命令
docker ps
docker images
docker run -d --name container_name image_name
九、运维小技巧
- 使用 screen/tmux:防止 SSH 断开导致命令中断
- 配置别名:在
~/.bashrc中添加常用命令别名 - 监控资源:安装 htop、iotop、nethogs 等工具
- 日志轮转:配置 logrotate 防止日志占满磁盘
- 时间同步:配置 NTP 保证时间准确
# 安装 tmux
apt install tmux
# 常用 tmux 命令
tmux new -s session_name # 新建会话
tmux attach -t session_name # 连接会话
tmux ls # 列出会话
# Ctrl+B 然后 D 退出会话
十、推荐工具
- 终端工具:Xshell、Termius、Tabby
- 文件传输:FileZilla、WinSCP、rsync
- 监控面板:Netdata、Grafana + Prometheus
- 在线工具:SSL 检测、DNS 检测、Ping 测试
写在最后
服务器运维是个实践性很强的技能,多动手、多踩坑才能成长。建议:
- 先在测试服务器上操作,熟悉后再上生产环境
- 重要操作前先备份
- 养成看日志的习惯
- 关注安全更新,及时打补丁
后续会继续更新更深入的运维内容,比如自动化运维、监控告警、性能优化等。 stay tuned!🚀
参考资料:
文章评论