Linux服务器查看网络日志的常用命令详解
在Linux系统中,网络日志是系统监控和故障排查的重要依据。通过分析网络日志,管理员可以追踪网络连接状态、识别异常流量、检测安全攻击等。本文将详细介绍查看网络日志的常用命令及其使用技巧。
一、系统日志查看
journalctl 作为systemd系统的核心日志工具,journalctl支持实时查看和过滤日志: journalctl -u networking.service journalctl -b --since "10 minutes ago" --until "now" journalctl -x --unit sshd.service
dmesg 查看内核环缓冲区中的网络相关消息: dmesg | grep -i eth dmesg | grep -i network dmesg | grep -i "device eth"
二、网络连接日志
netstat 显示网络连接状态(需安装net-tools包): netstat -antp | grep ESTABLISHED netstat -tunlp | grep :80 netstat -i | grep RX
ss 现代替代工具,性能更优: ss -antp ss -tunlp ss -i | grep "eth0"
三、防火墙日志
iptables 查看iptables日志(需配置日志选项): iptables -L -n --line-numbers tail -f /var/log/messages | grep -i "iptables"
firewalld 查看firewalld日志: journalctl -u firewalld.service firewall-cmd --list-all
四、应用层日志
SSH日志 /var/log/secure(CentOS/RHEL)或/var/log/auth.log(Debian/Ubuntu): grep "sshd" /var/log/secure grep "Failed password" /var/log/secure

Apache/Nginx日志 /var/log/httpd/access_log /var/log/nginx/access.log tail -f /var/log/nginx/error.log

五、网络接口状态
ifconfig(需安装net-tools) ifconfig eth0 ifconfig | grep "RX bytes"
ip命令 ip -s link show eth0 ip addr show
六、实时监控技巧
tail -f tail -f /var/log/messages tail -f /var/log/syslog
journalctl实时查看 journalctl -f journalctl -u network.service -f
七、日志分析工具
grep grep "192.168.1.100" /var/log/messages
awk awk '{print $1,$7}' /var/log/secure
less less /var/log/syslog
八、日志配置与管理
日志轮转配置 /etc/logrotate.conf中设置: /var/log/messages { daily rotate 7 compress missingok notifempty }
systemd日志配置 修改/etc/systemd/journald.conf: Storage=persistent Compress=yes Sync=yes
九、高级网络日志分析
tcpdump tcpdump -i eth0 -nn -c 100 tcpdump -i eth0 port 80 -w http.pcap
ngrep ngrep -i 'GET' tcp port 80 ngrep -i 'HTTP/1.1' 192.168.1.100
curl/wget调试 curl -v http://example.com wget --spider http://example.com -O - 2>&1 | grep "HTTP/1.1"
十、安全审计日志
auditd日志 ausearch -k network audit2why
/var/log/audit/audit.log ausearch --start -10m --type=NET
通过以上命令组合,可以全面掌握Linux服务器的网络日志分析能力。建议结合grep、awk等文本处理工具进行精准过滤,同时注意日志文件的大小管理和定期备份。对于生产环境,建议配置日志监控告警机制,及时发现异常网络活动。