有時候我們在程式開發過程當中,有需要檔案上傳、下載…等等,除了有版控來完成這一塊之外,有時候為了測試方便,我們也可以透過 FTP 軟體來達到快速更新程式的功能,所以接下來我們來記錄一下如何安裝 FTP 軟體。
 
 
安裝 FTP Server
 
 
1、更新套件並安裝 vsftpd package
$ sudo apt update
$ sudo apt install vsftpd
 
2、複製原始檔案備份
 
$ sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.bk
 
3、編輯 FTP 基本組態檔 ( basic FTP configuration )
 
編輯 /etc/vsftpd.conf 檔案,並取消下面列的註解
 
write_enable=YES
local_umask=022
chroot_local_user=YES
 
 
 
新增下面文字到最底端,並儲存離開。
 
allow_writeable_chroot=YES
pasv_min_port=40000
pasv_max_port=40100
 
 
4. 重啟設定
 
$ sudo systemctl restart vsftpd
$ sudo systemctl status vsftpd
 
狀態為 active ( running ),則代表已經啟用成功。
 
 
5. 測試 FTP server  ( Use /usr/sbin/nologin shell to limit access. )
 
$ ftp 127.0.0.1
 
 
 
6. 建置 FTPS
 
建立 SSL 認證,在這邊採用 RSA 認證 和 設定一年的有效期限
Create new SSL certificate using below command. Here we are going to create 2048 bit RSA certificate with 365 days validity.
 
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/ftps_vsftpd.pem -out /etc/ssl/private/ftps_vsftpd.pem
 
 
編輯並開始 ssl 設定
 
編輯 vsftpd.conf 文件,開啟 SSL 設定。 (  /etc/vsftpd.conf  )
 
Now make changes in /etc/vsftpd.conf file to enable and configure SSL. Find and modify below lines as per our certificate location and enable SSL.
 
rsa_cert_file=/etc/ssl/private/ftps_vsftpd.pem
rsa_private_key_file=/etc/ssl/private/ftps_vsftpd.pem
ssl_enable=YES
 
 
8. 重啟並測試
 
$ sudo systemctl restart vsftpd
 
 
9. SFTP 設定  ( SFTP configuration )
 
SFTP 使用不同的埠協定,它會比 FTP 更加安全
SFTP uses different protocol, It is more secure than FTP since it uses ssh port for data transfer.
$ sudo apt-get install openssh-server
建立一個新群組給 SFTP 的使用者 ( Create a new group for SFTP users. )
$ sudo addgroup ftpaccess
編輯 /etc/ssh/sshd_config 檔案,並將以下行進行註解 ( Edit /etc/ssh/sshd_config file and comment the below line. )
#Subsystem sftp /usr/lib/openssh/sftp-server
並加入下面資訊至檔檔最底部 ( and add these lines at the end of the file. )
 
Subsystem sftp internal-sftp
Match group ftpaccess
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
 
重新啟動 ssh 服務 ( Now restart ssh service. ) 
( **Please perform this step in the console since you may loose SSH connection while doing the service restart.** )
 
$ sudo systemctl restart ssh
 
將使用者 ( jqnets ) 加入 ftpacess 群組 ( Create an user with nologin shell and with group ftpacess. )
並設定密碼
 
$ sudo useradd -m jqnets -s /usr/sbin/nologin -G ftpaccess
$ sudo passwd jqnets
 
將目錄設定為 root 權限 ( Perform below steps to chroot users to their home directories. )
 
$ sudo chown root:root /home/jqnets
 
建立〝www〞目錄供上傳檔案使用。
Create a directory “upload” inside home directory and modify ownership. This directory can be used to upload files to the server.
 
$ sudo mkdir /home/jqnets/www
$ sudo chown jqnets:ftpaccess /home/jqnets/www
 
接下來,可以進行最後測試 ( Now you can connect server using sftp clients. )
 
$ sftp jqnets@127.0.0.1
 
 
 
10. SFTP 設定  ( SFTP configuration )
 
由於 var/www/html 只有 root 擁有使用權限,所以將已經設定好的 jqnets/www 指向 var/www/html/ ,方便 FTP 上傳使用。
 
ln :是對資料夾建立超連結的指令
 
$ cd /var/www/html/
$ sudo ln -s /home/jqnets/www
 
 
 
arrow
arrow
    文章標籤
    Ubuntu FTP LAMP
    全站熱搜

    Gimmy 發表在 痞客邦 留言(0) 人氣()