步驟1 配置磁盤
參考 Linux磁盤管理:LVM或Linux文件的 [磁盤管理:Linux新建數(shù)據(jù)盤] 章節(jié)驻襟。
步驟2 創(chuàng)建SFTP用戶組與用戶
groupadd sftpgrp
//創(chuàng)建用戶組
useradd -g sftpgrp -s /usr/sbin/nologin -M sftpuser
//創(chuàng)建用戶抖仅,-g用戶組,-s不能登錄促脉,-M不創(chuàng)建用戶目錄。
chage -M 99999 sftpuser
//修改用戶密碼不過期
passwd sftpuser
//設(shè)置mysftp用戶的密碼
步驟3 配置SFTP目錄
3.1
mkdir -p /backup/sftpuser
//創(chuàng)建目錄
3.2
usermod -d /backup/sftpuser sftpuser
//指定用戶目錄,后面會用chroot指定
chown root:root /backup /backup/sftpuser
chmod 755 /backup /backup/sftpuser
3.3
//創(chuàng)建個不是chroot的、SFTP用戶有權(quán)限寫入的路徑
cd /backup/sftpuser/
mkdir homepage
chown sftpuser:sftpgrp /backup/sftpuser/homepage
chmod 755 /backup/sftpuser/homepage
注意:
1侣夷、chroot要求1)目錄及上級目錄屬主與屬組需為root;2)只用屬主有目錄的寫權(quán)限即最大755.
2仑乌、chroot目錄屬主與權(quán)限設(shè)置錯誤會導(dǎo)致sftp登錄失敗百拓,作者經(jīng)驗:在步驟5使用“ChrootDirectory %h”指定路徑是用戶目錄后琴锭,要確保3.2和3.3已執(zhí)行且正確。
步驟4 配置selinux
vi /etc/selinux/config
SELINUX=disabled
或
SELINUX=permissive
setenforce 0
步驟5 配置/etc/ssh/sshd_config
vi /etc/ssh/sshd_config
//在132行左右注釋下行
Subsystem sftp /usr/libexec/openssh/sftp-server
//增加下面這行
Subsystem sftp internal-sftp
//在文件的最后衙传,因為Match結(jié)尾標志是新的Match或文件結(jié)尾决帖,%h表示只能訪問默認的用戶目錄
Match User sftpuser
或
Match Group sftpgrp
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
ChrootDirectory %h
//修改為以下
MaxSessions 100
//修改為以下
MaxStartups 100
service sshd restart
MaxSessions
Specifies the maximum number of open shell, login or subsystem
(e.g. sftp) sessions permitted per network connection. Multiple
sessions may be established by clients that support connection
multiplexing. Setting MaxSessions to 1 will effectively disable
session multiplexing, whereas setting it to 0 will prevent all
shell, login and subsystem sessions while still permitting for-
warding. The default is 10.
MaxStartups
Specifies the maximum number of concurrent unauthenticated con-
nections to the SSH daemon. Additional connections will be
dropped until authentication succeeds or the LoginGraceTime
expires for a connection. The default is 10:30:100.
Alternatively, random early drop can be enabled by specifying the
three colon separated values start:rate:full (e.g.
"10:30:60"). sshd(8) will refuse connection attempts with a
probability of rate/100 (30%) if there are currently
start'' (10) unauthenticated connections. The probability
increases linearly and all connection attempts are refused if the
number of unauthenticated connections reaches full (60).
步驟6 SFTP客戶端登錄驗證
建議使用命令行 sftp sftpuser@[IPv6地址] 來接入驗證,而不是直接用sftp客戶端蓖捶,如果連接失敗起碼有稍明顯的報錯地回。
FAQ
1、SFTP登錄提示“Connection closed”
使用chage -l sftpuser
查看返回結(jié)果的Password expires字段俊鱼,一般是密碼已經(jīng)過期刻像。