SFTP in Linux: Log Management Best Practices
SFTP (SSH File Transfer Protocol) logs are critical for monitoring user activity, detecting unauthorized access, and ensuring regulatory compliance. Since SFTP runs over SSH, its logs are integrated into the system’s SSH logging framework. Below is a structured guide to configuring, viewing, and managing SFTP logs in Linux.
The first step to enabling SFTP logs is adjusting the SSH server (sshd
) configuration. The key parameters control the verbosity and output of logs:
/etc/ssh/sshd_config
: Open the file with root privileges (e.g., sudo nano /etc/ssh/sshd_config
).LogLevel
directive to VERBOSE
. This level records detailed connection attempts, authentication results, and file transfer activities (e.g., Accepted publickey for user from IP port 22 ssh2
).Subsystem
line for SFTP is uncommented and uses the internal-sftp binary (common path: /usr/lib/openssh/sftp-server
). For advanced logging, append -l INFO -f local5
to route SFTP logs to a dedicated facility (e.g., Subsystem sftp internal-sftp -l INFO -f local5
).sudo systemctl restart sshd
(or sudo service ssh restart
on older systems).These configurations ensure SFTP activities are logged with sufficient detail for auditing.
SFTP logs are stored in system log files, with locations varying by Linux distribution:
/var/log/auth.log
(filter with sudo grep 'sftp' /var/log/auth.log
)./var/log/secure
(filter with sudo grep 'sftp-server' /var/log/secure
).tail -f /var/log/auth.log
(or /var/log/secure
) to track live SFTP activity.systemd
, run journalctl -u sshd.service | grep sftp
to view SFTP logs from the SSH service.Filtering commands (e.g., grep 'Failed password'
) help isolate specific events (e.g., failed login attempts).
For granular file-level auditing (e.g., tracking file reads/writes/deletes), use auditd
(Linux Audit Daemon):
sudo apt-get install auditd
(Debian/Ubuntu) or sudo yum install audit
(RHEL/CentOS).auditctl
to monitor SFTP directories. For example, sudo auditctl -w /path/to/sftp/directory -p wa -k sftp_monitor
tracks write/access events in the specified directory (replace /path/to/sftp/directory
with your SFTP root).ausearch -k sftp_monitor
(filters by the sftp_monitor
key) or aureport -k sftp_monitor
(generates a summary report).Audit logs provide actionable insights into file operations, helping detect suspicious activities (e.g., unauthorized file deletions).
To prevent log files from consuming excessive disk space, configure log rotation using logrotate
(default on most Linux systems):
/var/log/auth.log
), edit /etc/logrotate.d/ssh
(or /etc/logrotate.d/secure
) to include settings like:/var/log/auth.log {
daily
rotate 7
compress
missingok
notifempty
create 640 root adm
}
This rotates logs daily, keeps 7 compressed copies, and sets permissions for the new log file./var/log/sftp.log
via local5
), create a new logrotate
config (e.g., /etc/logrotate.d/sftp
):/var/log/sftp.log {
daily
rotate 7
compress
missingok
notifempty
create 640 root root
postrotate
systemctl reload sshd > /dev/null
endscript
}
The postrotate
script reloads sshd
to ensure the new log file is used.For large-scale environments, manual log inspection is impractical. Use tools to automate analysis and alerting:
sudo apt-get install logwatch
(Debian/Ubuntu) or sudo yum install logwatch
(RHEL/CentOS)./etc/fail2ban/jail.local
and a filter in /etc/fail2ban/filter.d/sftp.conf
).These tools reduce the time spent on log analysis and improve incident response.
By following these steps, you can establish a robust SFTP log management system that enhances security, supports compliance, and helps troubleshoot issues effectively.