Linux 防火墙技巧:提升数据库效率

欢迎来到冉冉博客的技术教程。今天分享Linux 防火墙技巧的实战经验,这些都是我在网站编程过程中总结的最佳实践。

一、Linux 进程管理与系统监控

进程管理是 Linux 运维的核心技能。了解如何监控系统资源、排查进程问题,是每个运维工程师的必备能力。冉冉博客的生产服务器使用这套监控方案,保持 99.9% 的正常运行时间。

1.1 进程查看与控制

# 查看所有进程
ps aux | head -20
ps -ef --forest

# 实时监控
top
htop
btop

# 进程控制
kill -15 1234  # 优雅终止
kill -9 1234   # 强制杀死
killall nginx

# 后台进程
nohup command &
Ctrl+Z
bg
fg

1.2 系统资源监控

# CPU 使用率
mpstat 1 5
sar -u 1 5
lscpu

# 内存使用
free -h
vmstat 1 5

# 磁盘使用
df -h
du -sh /var/log/*

# 网络连接
ss -tulnp
netstat -anp | grep ESTABLISHED

二、Linux 网络配置与故障排查

网络是 Linux 服务器的生命线。掌握网络配置和故障排查技能,能快速定位和解决网络问题。

2.1 网络配置与管理

# 查看网络接口
ip addr show
ip link set eth0 up/down

# 网络测试
ping -c 4 8.8.8.8
curl -I https://www.0rr.cn
traceroute www.google.com

# 端口扫描
nmap -sT localhost
nmap -sP 192.168.1.0/24

2.2 防火墙配置

# UFW(Ubuntu)
ufw status
ufw allow 22/tcp
ufw allow 80,443/tcp
ufw deny 3306
ufw enable

# iptables
iptables -L -n -v
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j DROP

三、Linux 日志分析与安全审计

日志是排查问题的第一手资料。Linux 系统有完善的日志机制,掌握日志分析方法能让故障排查事半功倍。

3.1 常用日志位置

# 系统日志
/var/log/syslog
/var/log/messages
/var/log/secure
/var/log/nginx/
/var/log/apache2/

# 查看日志
tail -f /var/log/syslog
journalctl -xe
journalctl -u nginx --since "1 hour ago"

3.2 安全审计与入侵检测

# 查看登录记录
last
lastlog
who
lastb

# 检查可疑活动
find /etc -mtime -1
find / -perm -4000 -type f 2>/dev/null
ps aux --sort=-%cpu | head -10

# fail2ban 防暴力破解
cat /etc/fail2ban/jail.local
fail2ban-client status

四、Linux 服务管理与自动化运维

systemd 是现代 Linux 发行版的标配服务管理器。掌握 systemd 的使用,能高效管理系统服务。

4.1 systemd 服务管理

# 服务管理
systemctl start nginx
systemctl status nginx
systemctl enable nginx
systemctl disable nginx

# 查看服务日志
journalctl -u nginx
journalctl -u nginx -f

# 创建服务
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
ExecStart=/opt/myapp/run.sh
Restart=always
[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl enable myapp

4.2 定时任务 cron

# crontab 格式: 分 时 日 月 周 命令
# 0 3 * * *  每天凌晨3点
# */5 * * * * 每5分钟
# 0 */2 * * * 每2小时

crontab -e
crontab -l

# 示例任务
0 3 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1
*/5 * * * * /usr/bin/python3 /opt/monitor.py

五、Linux 性能优化实战案例

结合前面的知识,分享几个冉冉博客实际使用的 Linux 优化案例。

5.1 Nginx 高并发配置

# /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 10240;
    use epoll;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    gzip on;
    gzip_types text/plain text/css text/javascript application/javascript;

    location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
    
    keepalive_timeout 65;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 100;
}

5.2 Linux 系统参数调优

# /etc/sysctl.conf
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300

fs.file-max = 6553600
* soft nofile 6553600
* hard nofile 6553600

sysctl -p  # 生效配置

希望这篇关于Linux 防火墙技巧的教程对你有帮助。冉冉博客会持续更新更多高质量技术文章,敬请期待!

© 版权声明
THE END
喜欢就支持一下吧
点赞7
评论 抢沙发

请登录后发表评论

    暂无评论内容