Linux上設定使用者通過SFTP訪問目錄的許可權的方法

來源:互聯網
上載者:User

   sftp和ftp是兩種協議是不同的,sftp是ssh內含的協議,只要sshd伺服器啟動了,它就可用,它本身不需要ftp伺服器啟動。

  1.查看openssh軟體版本,想sftp服務使用者只能訪問特定的檔案目錄,版本需要4.8以上

  代碼如下:

  [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檔案

  代碼如下:

  [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.測試訪問

  代碼如下:

  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

  解決方案:

  目錄使用權限設定上要遵循2點:

  ChrootDirectory設定的目錄許可權及其所有的上級檔案夾許可權,屬主和屬組必須是root;

  ChrootDirectory設定的目錄許可權及其所有的上級檔案夾許可權,只有屬主能擁有寫入權限,許可權最大設定只能是755。

  如果不能遵循以上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

  可以看到已經限制使用者在家目錄,同時該使用者也不能登入該機器。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.