Here's some sample log file output:
Feb 26 23:04:55 pegasus internal-sftp[32524]: session opened for local user joeuser from [123.123.123.123]
Feb 26 23:04:57 pegasus internal-sftp[32524]: opendir "/home/joeuser"
Feb 26 23:04:58 pegasus internal-sftp[32524]: closedir "/home/joeuser"
Feb 26 23:05:01 pegasus internal-sftp[32524]: opendir "/home/joeuser/"
Feb 26 23:05:01 pegasus internal-sftp[32524]: closedir "/home/joeuser/"
Feb 26 23:05:02 pegasus internal-sftp[32524]: opendir "/home/joeuser/upload"
Feb 26 23:05:02 pegasus internal-sftp[32524]: closedir "/home/joeuser/upload"
Feb 26 23:05:07 pegasus internal-sftp[32524]: opendir "/home/joeuser/upload"
Feb 26 23:05:07 pegasus internal-sftp[32524]: closedir "/home/joeuser/upload"
Feb 26 23:05:09 pegasus internal-sftp[32524]: session closed for local user joeuser from [123.123.123.123]
Which user is which?
If you take notice of the output above there is a number between square brackets , internal-sftp[32524]
. The number is 32524. This represents the session ID for user joeuser, so you can use this string together which messages relate to which user's login.
Rotating the logs
You can modify the log rotation schedule for various logs under /etc/logrotate.d/*
. Each log file typically has a corresponding file in this directory. So you could change the syslog
file there, for example or create your own for your sftp.log
logfile.
Also logrotate
has a configuration file, /etc/logrotate.conf
which contains these lines:
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
These are what the files in the /etc/logrotate.d
directory use, if they don't have a setting of their own. So most files are rotated weekly and 4 of them are kept. If you wanted to keep 6 months it would be 4*6 = 24 for the rotate
option to keep 6 months, roughly.
Example
Given you're logging to /var/log/sftp.log
via syslog
you'll need to make your changes in this file, /etc/logrotate.d/syslog
. Your file will look like this after making the required changes:
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
/var/log/sftp.log
{
rotate 24
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
Since you're using syslog
you'll have to rotate all these log files as well, keep 24 weeks worth of these as well. If this is unacceptable then your only other course of action would be to create a separate section in this file, syslog
like so:
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
/var/log/sftp.log
{
rotate 24
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
This has some side-effects, one being that you'll be restarting the syslog
daemon 2 times each week instead of once. But the logroate syntax does not allow for fine granular control of the rotation schedule for certain logfiles while not rotating others, when the log files are being generated by the same service, i.e. syslog
.
References
Best Answer
From the here-string (
<<<
) syntax you used I guess your shell isbash
, so you can also use string with backslash-escaped characters ($''
):The portable alternative is here-document: