Centos7 搭建多用户SFTP服务,并开启日志记录---亲测,成功---OK
Centos7 搭建多用户SFTP服务,并开启日志记录---亲测,成功---OK!!!
https://blog.csdn.net/xjjj064/article/details/116708500
===========================================================
先写后期维护:只需要新建用户和赋权,以admin001为例:
useradd -g sftpuser -M -s /sbin/nologin admin001
echo "123456" | passwd --stdin admin001
mkdir -p /data/sftp/admin001/home
usermod -d /data/sftp/admin001 admin001
chown root:sftpuser /data/sftp/admin001/
chown admin001:sftpuser /data/sftp/admin001/home/
systemctl restart sshd
===========================================================
entos7 搭建多用户SFTP服务,并开启日志记录
一、环境描述
系统版本:CentOS Linux release 7.8.2003
背景:有一台Centos服务器作为SFTP服务器,需要有SFTP用户,且需要记录操作日志。
=======================================
二、基于Centos7搭建sftp
①、修改SSH文件 /etc/ssh/sshd.conf
先注销掉这行
#Subsystem sftp /usr/libexec/openssh/sftp-server
1
在最底下添加如下几行
Subsystem sftp internal-sftp -l INFO -f local5
Match Group sftpuser
ChrootDirectory /data/sftp/%u
ForceCommand internal-sftp -l INFO -f local5
AllowTcpForwarding no
X11Forwarding no
================================================
ChrootDirectory 锁定SFTP目录到该路径, %u 每用户不同家目录
================================================
====组名为sftpuser===目录名为sftp===都可自定义=====================
创建SFTP根目录
mkdir /data/sftp
====先创建sftp组===组名随意=========================================
②、创建SFTP用户组sftpuser
[root@server3 ~]# groupadd sftpuser
1
③、重启sshd服务
[root@server3 ~]# systemctl restart sshd
==========================================================
以上的不变了,只需要新建用户和赋权
==建sftp用户==============================================
④、建sftp用户admin
创建用户admin,用户所属组为sftp
useradd -g sftpuser -s /bin/false admin
或者
# -d 指定家目录 -s 禁止登陆; -M 不创建家目录
-------------------------------
useradd -g sftpuser -M -s /sbin/nologin admin (推荐)
-------------------------------
或者,直接指定家目录,指定组,自动创建目录
useradd -d /data/sftp/admin -g sftpuser -m -s /sbin/nologin admin
---上面这命令,虽全面,但是会在新建目录下,产生.bashrc等等文件,还是用上面第一个吧。各有优势!
===========
设置用户密码
echo “123456” |passwd --stdin admin
===========
家目录/data/sftp/admin
注:用户无法在家目录直接操作,需要在家目录下创建一个文件夹用于存放文件
mkdir -p /data/sftp/admin/home
===========
修改用户家目录
usermod -d /data/sftp/admin admin
===========
修改文件夹属主和属组
chown admin:sftpuser /data/sftp/admin/home/
----------------------------
家目录属主、属组、及用户权限,不能有错,错了,就登录报错。
/data root:root 755
/data/sftp root:root 755
/data/sftp/admin root:sftpuser 755
/data/sftp/danny root:sftpuser 755
-------------------------
如下:
[root@ftp-cd01 sftp]# ll -d /data/
drwxr-xr-x 3 root root 18 Mar 2 15:53 /data/
[root@ftp-cd01 sftp]#
[root@ftp-cd01 sftp]# cd /data/
[root@ftp-cd01 data]# ll
total 0
drwxr-xr-x 4 root root 32 Mar 2 16:12 sftp
[root@ftp-cd01 data]#
[root@ftp-cd01 data]# cd sftp/
[root@ftp-cd01 sftp]# ll
total 0
drwxr-xr-x 4 root sftpuser 90 Mar 2 16:13 admin
drwxr-xr-x 3 root sftpuser 18 Mar 2 16:00 danny
[root@ftp-cd01 sftp]# cd admin/
[root@ftp-cd01 admin]# ll
total 0
drwxr-xr-x 2 admin sftpuser 6 Mar 2 16:39 home
[root@ftp-cd01 admin]# cd home/
[root@ftp-cd01 home]# pwd
/data/sftp/admin/home
================================================
测试:
[root@ftp-cd01 sftp]# sftp admin@10.28.10.6
admin@10.28.10.6's password:
Connected to 10.28.10.6.
sftp>
三、开启SFTP日志记录(最前面已经做了,可跳过)
①、设置sshd.conf文件
修改Subsystem 和ForceCommand 在后面增加 -l INFO -f local5
Subsystem sftp internal-sftp -l INFO -f local5
Match Group sftpuser
ChrootDirectory /data/sftp/%u
ForceCommand internal-sftp -l INFO -f local5
AllowTcpForwarding no
X11Forwarding no
重要 [ -l INFO -f local5 ]
日志等级:INFO # 定义代码:local5
记录消息代码:DAEMON,USER,AUTH,LOCAL0,LOCAL1,LOCAL2,LOCAL3,LOCAL4,LOCAL5,LOCAL6,LOCAL7。默认值为AUTH。
②、修改/etc/rsyslog.conf
[root@localhost ~]# vim /etc/rsyslog.conf
在最后面添加以下
auth,authpriv.,local5. /var/log/sftp.log
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList # run asynchronously
#$ActionResumeRetryCount -1 # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###
auth,authpriv.*,local5.* /var/log/sftp.log
③、重启sshd、rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# systemctl restart sshd
1
2
④、验证sshd日志是否记录
May 12 16:33:56 localhost sshd[1580]: Accepted password for admin from 192.168.7.119 port 54375 ssh2
May 12 16:33:56 localhost systemd-logind: New session 3 of user admin.
May 12 16:33:56 localhost sshd[1580]: pam_unix(sshd:session): session opened for user admin by (uid=0)
May 12 16:33:56 localhost sshd[1580]: session opened for local user admin from [192.168.7.119] [postauth]
May 12 16:33:56 localhost sshd[1580]: opendir "/home/" [postauth]
May 12 16:33:56 localhost sshd[1580]: closedir "/home/" [postauth]
May 12 16:33:56 localhost sshd[1580]: opendir "/home/" [postauth]
May 12 16:33:56 localhost sshd[1580]: closedir "/home/" [postauth]
May 12 16:33:58 localhost sshd[1580]: sent status No such file [postauth]
May 12 16:33:58 localhost sshd[1580]: open "/home/123.jpg" flags WRITE,CREATE,TRUNCATE mode 0666 [postauth]
May 12 16:33:58 localhost sshd[1580]: close "/home/123.jpg" bytes read 0 written 10566 [postauth]
May 12 16:33:58 localhost sshd[1580]: set "/home/123.jpg" modtime 20210318-06:12:39 [postauth]
May 12 16:33:58 localhost sshd[1580]: opendir "/home/" [postauth]
May 12 16:33:58 localhost sshd[1580]: closedir "/home/" [postauth]
或者
tail -f /var/log/messages
这个默认文件也有日志