Linux SFTP日志查看与分析
一 日志位置与快速查看
| 系统或方式 | 日志路径或命令 | 说明 |
|---|---|---|
| Debian/Ubuntu | /var/log/auth.log |
使用 grep sftp /var/log/auth.log 过滤;实时跟踪:`tail -f /var/log/auth.log |
| RHEL/CentOS | /var/log/secure |
使用 grep sftp /var/log/secure;实时跟踪:`tail -f /var/log/secure |
| systemd 统一日志 | journalctl -u sshd.service |
实时跟踪:`journalctl -u sshd.service -f |
| 通用 syslog | /var/log/syslog |
部分系统将SSH日志写入 syslog,可用 grep sftp /var/log/syslog 检索 |
Accepted publickey for <用户> from <IP> port <端口> ssh2pam_unix(sshd:session): session opened for user <用户> by (uid=0)channel_open_session: ... sftp二 增强日志可见性
/etc/ssh/sshd_config,将子系统改为:Subsystem sftp internal-sftp -l INFO(也可用外部 sftp-server -l INFO)。修改后重启SSH:systemctl restart sshd。日志仍写入系统认证日志(如 /var/log/auth.log 或 /var/log/secure)。/etc/rsyslog.conf 或 /etc/rsyslog.d/*.conf 增加:auth,authpriv.* /var/log/sftp.log,然后 systemctl restart rsyslog。注意这会使认证日志不再出现在默认文件,需同步调整监控与轮转策略。sshd_config 中使用 Match User <用户名> 段设置 LogLevel INFO,再重启 sshd。例如:Match User alice
LogLevel INFO
三 常用分析与排查命令
tail -f /var/log/auth.log | grep sftpgrep 'sftp.*<用户名>' /var/log/auth.loggrep 'Failed password\|Login incorrect' /var/log/auth.loggrep 'sftp.*<用户名>' /var/log/auth.log | wc -lgrep 'Accepted.*ssh2' /var/log/auth.log | awk '{print $10}' | sort | uniq -c | sort -nr | headgrep 'session opened for user' /var/log/auth.log 与 grep 'session closed for user' /var/log/auth.log 抽取时间与用户,再在脚本中配对计算差值。awk/sed 做字段提取与时间归一化;复杂场景可结合 Logwatch、ELK(Elasticsearch/Logstash/Kibana)、Graylog、Splunk 做可视化与告警。四 进阶审计与网络排查
auditd,对关键目录设置监控(示例监控用户家目录):sudo auditctl -w /home -p wa -k sftp_home_audit
sudo ausearch -k sftp_home_audit。适用于追踪 SFTP 会话中的具体文件读写与权限变更。sudo tcpdump -i any port 22 -w sftp_traffic.pcap,用 Wireshark 分析握手、算法协商与断开原因。适合配合日志分析进行排障。五 常见问题与排查路径
systemctl status sshd;启动:systemctl start sshdufw allow 22/tcp 或 firewall-cmd --permanent --add-port=22/tcp && firewall-cmd --reloadtail -f /var/log/auth.log 检索 Failed password/Login incorrectPasswordAuthentication yes 等配置是否正确;必要时调整 MaxStartups 缓解并发限制sestatus、apparmor_status,排除策略阻断导致的登录或文件访问异常。