2017年2月18日 星期六

在樹莓派上架設 FTP 伺服器

除了用 WinSCP 或 pscp 在 Windows 與樹莓派之間互傳檔案外, 還可以在樹莓派上架設 FTP 伺服器, 使用跟 WinSCP 類似的 FTP 程式來傳送檔案. 以下根據柯博文寫的 "Raspberry Pi 最佳入門與實戰應用第二版" ˊ6.3 節與 "Raspberry Pi 超炫專案與完全實戰第二版" 4.4 節進行實驗.

1. 更新系統與已安裝軟體清單 (可略過)

$ sudo apt-get update
$ sudo apt-get upgrade

2. 安裝 vsftpd 軟體

$ sudo apt-get install vsftpd

pi@raspberrypi:~ $ sudo apt-get install vsftpd
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  dialog
The following NEW packages will be installed:
  dialog vsftpd
0 upgraded, 2 newly installed, 0 to remove and 24 not upgraded.
Need to get 373 kB of archives.
After this operation, 1,001 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y  (輸入 y 或 Y)
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main dialog armhf 1.2-                                                                             20140911-1 [233 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main vsftpd armhf 3.0.                                                                             2-17+deb8u1 [140 kB]
Fetched 373 kB in 1s (214 kB/s)
Preconfiguring packages ...
Selecting previously unselected package dialog.
(Reading database ... 130140 files and directories currently installed.)
Preparing to unpack .../dialog_1.2-20140911-1_armhf.deb ...
Unpacking dialog (1.2-20140911-1) ...
Selecting previously unselected package vsftpd.
Preparing to unpack .../vsftpd_3.0.2-17+deb8u1_armhf.deb ...
Unpacking vsftpd (3.0.2-17+deb8u1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for systemd (215-17+deb8u6) ...
Setting up dialog (1.2-20140911-1) ...
Setting up vsftpd (3.0.2-17+deb8u1) ...
insserv: warning: script 'K01vncboot' missing LSB tags and overrides
insserv: warning: script 'vncboot' missing LSB tags and overrides
Processing triggers for systemd (215-17+deb8u6) ...
pi@raspberrypi:~ $


3. 修改 vsftpd 設定檔 (/etc/vsftpd.conf)

# sudo nano /etc/vsftpd.conf 

原始檔案部分內容如下 :

# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
listen_ipv6=YES
#
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
local_enable=YES    
#
# Uncomment this to enable any form of FTP write command.
#write_enable=YES    (將前面的 # 刪除)
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
#local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.

將設定檔中的 local_enable 與 write_enable 這兩項前面的 # 去除 (我下載安裝的版本 local_enable 預設已去除), 另外還要確認允許匿名登入的 anonymous_enable 是否設為 NO :

anonymous_enable=NO  (不允許匿名登入)
local_enable=YES  (允許本地端, 即樹莓派上可使用 FTP 伺服器)
write_enable=YES  (允許檔案寫入)

編輯完畢按 Ctrl+O (Enter) 存檔, 再按 Ctrl+X 跳出 Nano 即可.

4. 重新啟動 vsftpd

$ sudo service vsftpd restart

這樣樹莓派上的 FTP 伺服器就啟動了. 接下來要在 Windows 下載安裝 FTP client 軟體來測試檔案互傳, 可至自由軟體 FileZilla 官網下載安裝版軟體 :

https://filezilla-project.org/download.php

不過安裝版會置入廣告, 可下載無廣告的免安裝版 (已用 VirusTotal 掃過 OK) :

FileZilla v3.24.0 免費 FTP 下載工具(免安裝版 +舊版軟體)

解壓縮後開啟 FileZilla 軟體, 輸入 host (樹莓派的 ip), 使用者名稱 (預設 pi), 密碼 (預設 raspberry), 埠號 21 可填可不填, 按 "快速連線" 即可 :


這樣就可以用拖曳方式在 Windows 與樹莓派之間互傳檔案了. 使用預設帳號 pi 登入 FTP 伺服器的話, 預設目錄就是 /home/pi, 可以按 "/" 切換到根目錄下的其他目錄.

如果要在登入 FTP 伺服器時直接進入 /var/www/html 目錄, 可以先建立一個使用者帳號例如 tony :

pi@raspberrypi:~ $ sudo useradd tony
pi@raspberrypi:~ $ sudo passwd tony
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

然後用 usermod 指令更改其預設目錄 :

pi@raspberrypi:~ $ sudo usermod -d /var/www/html/tony tony
You have new mail in /var/mail/pi

這樣用 tony 這個帳號登入 FTP 伺服器時, 就會直接進入 /var/www/html/tony 這個目錄了.

另外也可以在樹莓派上安裝 FileZilla FTP Client 軟體, 這樣也可以在樹莓派的 X Window 中執行 FileZilla 程式了 :

$ sudo apt-get install filezilla

pi@raspberrypi:~ $ sudo apt-get install filezilla
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libasn1-8-heimdal libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhx509-5-heimdal
  libkrb5-26-heimdal libroken18-heimdal libwind0-heimdal libxfce4ui-1-0 xfce-keyboard-shortcuts
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  filezilla-common libtinyxml2.6.2 libwxbase3.0-0 libwxgtk3.0-0
The following NEW packages will be installed:
  filezilla filezilla-common libtinyxml2.6.2 libwxbase3.0-0 libwxgtk3.0-0
0 upgraded, 5 newly installed, 0 to remove and 21 not upgraded.
Need to get 7,817 kB of archives.
After this operation, 31.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libtinyxml2.6.2 armhf 2.6.2-2 [39.1 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwxbase3.0-0 armhf 3.0.2-1 [942 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main libwxgtk3.0-0 armhf 3.0.2-1 [3,818 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main filezilla-common all 3.9.0.5-1 [1,890 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main filezilla armhf 3.9.0.5-1 [1,128 kB]
Fetched 7,817 kB in 7s (1,114 kB/s)
Selecting previously unselected package libtinyxml2.6.2:armhf.
(Reading database ... 129873 files and directories currently installed.)
Preparing to unpack .../libtinyxml2.6.2_2.6.2-2_armhf.deb ...
Unpacking libtinyxml2.6.2:armhf (2.6.2-2) ...
Selecting previously unselected package libwxbase3.0-0:armhf.
Preparing to unpack .../libwxbase3.0-0_3.0.2-1_armhf.deb ...
Unpacking libwxbase3.0-0:armhf (3.0.2-1) ...
Selecting previously unselected package libwxgtk3.0-0:armhf.
Preparing to unpack .../libwxgtk3.0-0_3.0.2-1_armhf.deb ...
Unpacking libwxgtk3.0-0:armhf (3.0.2-1) ...
Selecting previously unselected package filezilla-common.
Preparing to unpack .../filezilla-common_3.9.0.5-1_all.deb ...
Unpacking filezilla-common (3.9.0.5-1) ...
Selecting previously unselected package filezilla.
Preparing to unpack .../filezilla_3.9.0.5-1_armhf.deb ...
Unpacking filezilla (3.9.0.5-1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for hicolor-icon-theme (0.13-1) ...
Processing triggers for gnome-menus (3.13.3-6) ...
Processing triggers for desktop-file-utils (0.22-1) ...
Processing triggers for mime-support (3.58) ...
Setting up libtinyxml2.6.2:armhf (2.6.2-2) ...
Setting up libwxbase3.0-0:armhf (3.0.2-1) ...
Setting up libwxgtk3.0-0:armhf (3.0.2-1) ...
Setting up filezilla-common (3.9.0.5-1) ...
Setting up filezilla (3.9.0.5-1) ...
Processing triggers for libc-bin (2.19-18+deb8u7) ...
You have new mail in /var/mail/pi

然後在樹莓派的 X Window 視窗中按 "Menu/Run", 輸入 "filezilla" 按 OK 即啟動樹莓派上的 FileZilla Client, 可以連線到其他 FTP 伺服器以互傳檔案.




沒有留言 :