一、查看SFTP服务基础状态
要监控CentOS上SFTP服务的运行状态,首先需确认SSH服务(SFTP依赖SSH协议)是否正常运行。使用systemctl
命令查看sshd
服务状态,若服务未运行需及时启动:
sudo systemctl status sshd # 查看SSH服务状态(SFTP依赖此服务)
sudo systemctl start sshd # 启动SSH服务(若未运行)
sudo systemctl enable sshd # 设置SSH服务开机自启
二、检查SFTP配置文件正确性
SFTP的核心配置位于/etc/ssh/sshd_config
文件中,需确保以下关键配置项正确(常见配置示例):
sudo cat /etc/ssh/sshd_config | grep -E 'Subsystem|PasswordAuthentication'
sftp-server
):Subsystem sftp /usr/libexec/openssh/sftp-server
PasswordAuthentication yes
)。sudo systemctl restart sshd
三、查看系统日志监控SFTP活动
CentOS默认将SFTP活动记录在/var/log/secure
(RHEL/CentOS 7/8)或/var/log/auth.log
(部分发行版)中。可通过以下命令过滤SFTP相关日志:
# 实时查看SFTP日志(含连接、上传、下载等操作)
sudo tail -f /var/log/secure | grep 'sftp-server'
# 过滤特定用户的SFTP活动(如用户"sftpuser")
sudo grep 'sftpuser' /var/log/secure | grep 'sftp-server'
# 统计用户SFTP连接次数(按用户分组计数)
sudo awk '/sshd.*sftp-server/ {print $1}' /var/log/secure | sort | uniq -c
四、使用auditd进行深度文件操作审计
auditd
是Linux系统级审计工具,可监控SFTP用户的文件访问、修改、删除等操作,适合安全合规场景。
sudo yum install audit -y # 安装auditd
sudo systemctl start auditd # 启动服务
sudo systemctl enable auditd # 开机自启
/etc/audit/rules.d/sftp.rules
文件,添加以下规则(监控SFTP相关系统调用):-a exit,always -F arch=b32 -S open,openat,write,read -F path=/home/sftpuser -k sftp_activity # 监控/home/sftpuser目录下的文件操作(b32架构)
-a exit,always -F arch=b64 -S open,openat,write,read -F path=/home/sftpuser -k sftp_activity # 监控64位架构下的文件操作
保存后重启auditd使规则生效:sudo systemctl restart auditd
ausearch
命令查询标记为sftp_activity
的日志:sudo ausearch -k sftp_activity | aureport -f -i # 查看文件操作详情(-f表示文件操作)
五、使用第三方工具实现高级监控 若需更全面的监控(如实时告警、可视化),可使用以下工具:
node_exporter
收集系统指标(如SFTP进程CPU/内存使用率):sudo yum install prometheus-node-exporter -y
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
node_exporter
指标(编辑/etc/prometheus/prometheus.yml
):scrape_configs:
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
/etc/logstash/conf.d/sftp.conf
):input { file { path => "/var/log/secure" start_position => "beginning" } }
filter {
grok { match => { "message" => "%{SYSLOGTIMESTAMP:timestamp} %{HOSTNAME:hostname} sshd\[%{NUMBER:pid}\]: %{GREEDYDATA:log}" } }
if [log] =~ /sftp-server/ { mutate { add_tag => ["sftp"] } }
}
output { elasticsearch { hosts => ["localhost:9200"] index => "sftp-logs-%{+YYYY.MM.dd}" } }
sftp-logs-*
),通过可视化面板分析SFTP活动趋势。六、测试SFTP连接有效性 定期测试SFTP连接,确保配置正确且服务可用:
sftp sftpuser@localhost # 替换为实际用户名和IP
输入密码(或使用密钥)后,若成功进入SFTP命令行(如sftp>
),则说明配置正常。
七、检查防火墙与SELinux设置
sudo firewall-cmd --permanent --add-service=ssh # 允许SSH服务(包含SFTP)
sudo firewall-cmd --reload # 重新加载防火墙规则
getenforce
返回Enforcing
),需确保其策略允许SFTP访问:sudo setenforce 0 # 临时设置为permissive模式(测试用)
sudo getenforce # 确认状态
若需永久修改,编辑/etc/selinux/config
文件,将SELINUX=enforcing
改为SELINUX=permissive
,然后重启系统。