sftp服務限制用戶登陸家目錄

       sftp和ftp是兩種協議是不一樣的,sftp是ssh內含的協議,只要sshd服務器啓動了,它就可用,它自己不須要ftp服務器啓動。 shell

1.查看openssh軟件版本,想sftp服務用戶只能訪問特定的文件目錄,版本須要4.8以上 vim

[root@localhost ftp]# rpm -qa | grep openssh
openssh-server-5.3p1-81.el6_3.x86_64
openssh-5.3p1-81.el6_3.x86_64
openssh-clients-5.3p1-81.el6_3.x86_64

2.新增用戶,限制用戶只能經過sftp訪問 服務器

[root@localhost ftp]# useradd -m -d /opt/ftp/dave -s /sbin/nologin dave

3.限制用戶經過sftp登陸進來時只能進入主目錄,修改/etc/ssh/sshd_config文件 session

[root@localhost ftp]# vim /etc/ssh/sshd_config
#Subsystem	sftp	/usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp
Match User dave
         ChrootDirectory /opt/ftp/dave
         X11Forwarding no
         AllowTcpForwarding no
         ForceCommand internal-sftp
重啓ssh

4.測試訪問 ssh

root@10.1.1.200:test# sftp -oPort=22 dave@10.1.6.175
Connecting to 10.1.6.175...
dave@10.1.6.175's password: 
Read from remote host 10.1.6.175: Connection reset by peer
Couldn't read packet: Connection reset by peer

發現鏈接不上,查看日誌 測試

[root@localhost ftp]# tail /var/log/messages
Jan  6 11:41:41 localhost sshd[4907]: fatal: bad ownership or modes for chroot directory "/opt/ftp/dave"
Jan  6 11:41:41 localhost sshd[4905]: pam_unix(sshd:session): session closed for user dave

解決方法: spa

目錄權限設置上要遵循2點: unix

ChrootDirectory設置的目錄權限及其全部的上級文件夾權限,屬主和屬組必須是root; 日誌

ChrootDirectory設置的目錄權限及其全部的上級文件夾權限,只有屬主能擁有寫權限,權限最大設置只能是755。 code

若是不能遵循以上2點,即便是該目錄僅屬於某個用戶,也可能會影響到全部的SFTP用戶。

[root@localhost ftp]# ll
total 4
drwxr-xr-x 3 dave dave 4096 Jan  5 13:06 dave
[root@localhost ftp]# chown root:root dave
[root@localhost ftp]# chmod 755 dave
[root@localhost ftp]# ll
total 4
drwxr-xr-x 3 root root 4096 Jan  5 13:06 dave

而後在測試經過

root@10.1.1.200:test# sftp -oPort=22 dave@10.1.6.175
Connecting to 10.1.6.175...
dave@10.1.6.175's password: 
sftp> ls
test  
sftp> cd ..
sftp> ls
test  
sftp> cd test
sftp> ls
1.txt  
sftp> get 1.txt
Fetching /test/1.txt to 1.txt
/test/1.txt

能夠看到已經限制用戶在家目錄,同時該用戶也不能登陸該機器。

相關文章
相關標籤/搜索