EasyEngine 中使用 SFTP 方式傳輸檔案,非常容易,EE 已經預做好 www-data 使用群組,只要設置 www-data 的密碼後,將權限更改為 /bin/bash 就可以用 FileZilla 來傳輸文件了。
以上的方式,前提是只有你一個人使用 VPS 主機,如果想分享主機給朋友使用,就不能這樣做,不然你分享的使用者用 SFTP 方式登入後,所有的網站資料夾都被看光了,也增加了檔案誤刪的風險。
所以,利用 Chroot 方式來限制使用目錄的方式,來增加 SFTP 使用者。
Chroot SFtp 教學
1. 創建一個用戶:
增加一個用戶 test-user 加入 www-data 使用群組,但不能使用 SSH 權限
useradd -G www-data -ms /bin/false test-user passwd test-user
2. 創建一個 SFtp 家目錄:
限制 test-user 使用者只能在家目錄中傳輸檔案
mkdir -p /home/test-user/example.com/htdocs
3. 設置安裝權限
chown test-user:www-data /home/test-user/example.com chown root:root /home/test-user/ chown root:root /home/
執行上述命令後,權限會跟 /ver/www/example.com 的目錄一樣
~# ls -ld /home/ drwxr-xr-x 5 root root 4096 Oct 24 06:42 /home/ ~# ls -ld /home/test-user/ drwxr-xr-x 3 root root 4096 Oct 24 06:42 /home/test-user ~# ls -ld /home/test-user/example.com drwxr-xr-x 2 test-user www-data 4096 Oct 31 08:49 /home/test-user/example.com
4. 安裝 sftp-server
在 / etc / ssh / sshd_config 文件中修改以下的配置
~# vi /etc/ssh/sshd_config # 找到下面這一行 Subsystem sftp /usr/lib/openssh/sftp-server # 註釋掉上面這一行後,增加下面一行配置 Subsystem sftp internal-sftp
使用 EOF 的方法將下面配置加入 sshd_config 文件最下面處
Match group www-data X11Forwarding no ChrootDirectory %h AllowTcpForwarding no ForceCommand internal-sftp
怕避免出錯,可以先備份 sshd_config 文件
EOF 的寫法:
cat << EOF >> /etc/ssh/sshd_config Match group www-data X11Forwarding no ChrootDirectory %h AllowTcpForwarding no ForceCommand internal-sftp EOF
5. 重新啟動 ssh 服務
~# service ssh restart
6. 設置 webroot 權限
chmod g+s /var/www/example.com/htdocs/ chmod 775 /var/www/example.com/htdocs/
7. 將 webroot 加入 SFtp 目錄中
~# mount --bind /var/www/example.com/htdocs /home/test-user/example.com/htdocs
在 / etc / rc.local 中添加以上配置
~# vim /etc/rc.local
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. mount --bind /var/www/example.com/htdocs /home/test-user/example.com/htdocs exit 0
保存文件並退出
按 ESC 後 shift + : 鍵入 w q
2018/11/23 補充資料:
如果使用 FileZilla 時無法上傳資料,需更改 wp-content 權限。
chmod -R 775 /var/www/example.com/htdocs/wp-content
另外,EasyEngine v4 版已經取消 SFTP 功能,但使用上述的方法,還是可以恢復 SFTP 功能,資料夾的路徑需更改一下。
/var/www/example.com/htdocs/
改為
/opt/easyengine/sites/e4.woowp.tw/app/htdocs/
參考文件:
Featured Photo 攝影師:Pixabay,連結:Pexels