歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> sftp服務限制用戶登錄家目錄

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

日期:2017/3/1 17:02:33   编辑:關於Linux
sftp服務限制用戶登錄家目錄 sftp和ftp是兩種協議是不同的,sftp是ssh內含的協議,只要sshd服務器 啟動了,它就可用,它本身不需要ftp服務器啟動。 www.2cto.com 1.查看openssh軟件版本,想sftp服務用戶只能訪問特定的文件目錄,版本需要4.8以上 1 [root@localhost ftp]# rpm -qa | grep openssh 2 openssh-server-5.3p1-81.el6_3.x86_64 3 openssh-5.3p1-81.el6_3.x86_64 4 openssh-clients-5.3p1-81.el6_3.x86_64 2.新增用戶,限制用戶只能通過sftp訪問 1 [root@localhost ftp]# useradd -m -d /opt/ftp/dave -s /sbin/nologin dave 3.限制用戶通過sftp登錄進來時只能進入主目錄,修改/etc/ssh/sshd_config文件 www.2cto.com 1 [root@localhost ftp]# vim /etc/ssh/sshd_config 1 #Subsystem sftp /usr/libexec/openssh/sftp-server 2 Subsystem sftp internal-sftp 3 Match User dave 4 ChrootDirectory /opt/ftp/dave 5 X11Forwarding no 6 AllowTcpForwarding no 7 ForceCommand internal-sftp 重啟ssh 4.測試訪問 1 [email protected]:test# sftp -oPort=22 [email protected] 2 Connecting to 10.1.6.175... 3 [email protected]'s password: 4 Read from remote host 10.1.6.175: Connection reset by peer 5 Couldn't read packet: Connection reset by peer 發現連接不上,查看日志 1 [root@localhost ftp]# tail /var/log/messages 2 Jan 6 11:41:41 localhost sshd[4907]: fatal: bad ownership or modes for chroot directory "/opt/ftp/dave" 3 Jan 6 11:41:41 localhost sshd[4905]: pam_unix(sshd:session): session closed for user dave 解決方法: 目錄權限設置上要遵循2點: ChrootDirectory設置的目錄權限及其所有的上級文件夾權限,屬主和屬組必須是root; ChrootDirectory設置的目錄權限及其所有的上級文件夾權限,只有屬主能擁有寫權限,權限最大設置只能是755。 如果不能遵循以上2點,即使是該目錄僅屬於某個用戶,也可能會影響到所有的SFTP用戶。 1 [root@localhost ftp]# ll 2 total 4 3 drwxr-xr-x 3 dave dave 4096 Jan 5 13:06 dave 4 [root@localhost ftp]# chown root:root dave 5 [root@localhost ftp]# chmod 755 dave 6 [root@localhost ftp]# ll 7 total 4 8 drwxr-xr-x 3 root root 4096 Jan 5 13:06 dave 然後在測試通過 01 [email protected]:test# sftp -oPort=22 [email protected] 02 Connecting to 10.1.6.175... 03 [email protected]'s password: 04 sftp> ls 05 test 06 sftp> cd .. 07 sftp> ls 08 test 09 sftp> cd test 10 sftp> ls 11 1.txt 12 sftp> get 1.txt 13 Fetching /test/1.txt to 1.txt 14 /test/1.txt 可以看到已經限制用戶在家目錄,同時該用戶也不能登錄該機器。
Copyright © Linux教程網 All Rights Reserved