Ricky 叩叮俱樂部

在開始之前,先來杯長島冰茶吧~

0%

Ubuntu 18.04 LTS+vsftpd

前言

自從開始實習,公司開了一組GCP帳號讓我測試用之後
前前後後碰了不少東西,也接觸了很多有關Server的知識
而這次要玩的則是不依賴GCP方便的傳輸介面
直接在Server上架設FTP,這個從小聽到大的檔案傳輸協定
那就一起來看看是如何實現的吧~

Ubuntu

這邊使用的是GCP平台,因此在建立Ubuntu時可先賦予root密碼

1
$ sudo passwd root

可切換至root帳號

1
$ su -

這樣在操作下列步驟時,執行指令前就不用另外加sudo

環境

安裝vsftpd

1
2
$ sudo apt-get update
$ sudo apt-get install vsftpd -y

啟動&開機自動啟動vsftpd

1
2
$ sudo systemctl start vsftpd
$ sudo systemctl enable vsftpd

設定檔

備份原始設定檔以方便回溯

1
$ sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.org

修改設定檔

1
$ sudo vim /etc/vsftpd.conf

設定存取權限

1
2
3
4
5
6
7
8
# 禁止匿名使用者
anonymous_enable=NO

# 只允許本地端使用者存取
local_enable=YES

# 允許使用者寫入
write_enable=YES

限制存取範圍,讓使用者只能在/home/<USER>/ftp底下

1
2
3
chroot_local_user=YES
user_sub_token=$USER
local_root=/home/$USER/ftp

指定Port範圍在10000 ~ 110000 之間,可以依照個人需求修改

1
2
pasv_min_port=10000
pasv_max_port=11000

使用者登入清單

1
2
3
userlist_enable=YES
userlist_file=/etc/vsftpd.user_list
userlist_deny=NO

PAM模組名稱

1
pam_service_name=vsftpd

SSL加密,這裡可以事先一起設定

1
2
3
rsa_cert_file=/etc/ssl/private/vsftpd.pem
rsa_private_key_file=/etc/ssl/private/vsftpd.pem
ssl_enable=YES

都存檔之後就可以重啟vsftpd

1
$ sudo systemctl restart vsftpd

完整vsftpd.conf如下,保留原始的配置以便日後修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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.
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# If enabled, vsftpd will display directory listings with the time
# in your local time zone. The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
#xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may restrict local users to their home directories. See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
chroot_local_user=YES
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
#chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd.chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# This option should be the name of a directory which is empty. Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
pam_service_name=vsftpd
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/private/vsftpd.pem
rsa_private_key_file=/etc/ssl/private/vsftpd.pem
ssl_enable=YES

#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
#utf8_filesystem=YES

user_sub_token=$USER
local_root=/home/$USER/ftp

pasv_min_port=10000
pasv_max_port=11000

userlist_enable=YES
userlist_file=/etc/vsftpd.user_list
userlist_deny=NO

SSL / TLS

預設的FTP是使用明碼進行傳輸,這其實對資料非常沒有保障
為了安全性上的考量,我們採用OpenSSL建立加密憑證,這裡會產生一組十年期限的2048bit金鑰
而金鑰和憑證都會依照設定檔配置放在/etc/ssl/private/vsftpd.pem

1
$ sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/ssl/private/vsftpd.pem -out /etc/ssl/private/vsftpd.pem

系統會先問你一連串的基本資料加入至憑證中,按照指示回答即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 國家代號
Country Name (2 letter code) [AU]:TW
# 州或省
State or Province Name (full name) [Some-State]:Taipei
# 城市
Locality Name (eg, city) []:xinyi
# 組織或公司名稱
Organization Name (eg, company) [Internet Widgits Pty Ltd]:kiitzu
# 單位名稱
Organizational Unit Name (eg, section) []:RD
# Server或是負責人姓名
Common Name (e.g. server FQDN or YOUR name) []:ricky_lin
# 聯絡Email
Email Address []:ricky_lin@kiitzu.com

這裡就依照你的個人資料輸入

重啟vsftpd就設定好囉

1
$ sudo systemctl restart vsftpd

使用者

接著我們就可以創立一組給予vsftpd使用的帳號,這裡用vsftp當作示例
按照步驟輸入密碼,後面的問題可直接Enter略過

1
$ sudo adduser vsftp

adduseruseradd雖然都可以建立使用者,但在細節上並不相同,可別搞混囉

將使用者加入至登入清單中

1
$ sudo vim /etc/vsftpd.user_list
1
vsftp

FTP目錄

在剛建立好的使用者家目錄下建立FTP目錄,並設定權限

1
2
3
4
$ sudo cd /home/vsftp
$ sudo mkdir ftp
$ sudo chown nobody:nogroup ftp
$ sudo chmod a-w ftp

a-w: 該檔案(目錄)不能寫入

接下來在ftp目錄下建立一個可以上傳下載的目錄,並設定使用者權限

1
2
3
$ sudo cd ftp
$ sudo mkdir upload
$ sudo chown vsftp:vsftp upload

chmod: 更改檔案(目錄)權限 / chown: 更改檔案(目錄)擁有者

GCP

別忘了我們的Ubuntu是綁在GCP底下,因此我們還要到GCP設置防火牆
不然連線至FTP時,會硬是活生生的被Google給擋了下來

這邊要記得Port要根據你在設定檔配置的範圍
gcp_firewall_ftp.png

FileZilla

所有的步驟都做好後,我們就可以在本機端連線至FTP了
我是使用FileZilla這套老牌的FTP軟體

點擊左上角新增站台
filezilla01.png

配置好主機加密使用者後,點選連線
filezilla02.png

這就是我們之前建立好的憑證,點選確認
openssl_ftp.png

我們的目錄就會出現在畫面右方
filezilla03.png

接著你就可以在目錄下存取檔案囉~

WordPress

如果你想在FTP上直接存取WordPress的話,可以這麼做

先將WordPress目錄移至ftp

1
$ sudo mv /var/www/html/wordpress /home/vsftp/ftp

設定軟連結

1
$ sudo ln -s /home/vsftp/ftp/wordpress /var/www/html/wordpress

這時候可以在FTP看到WordPress,但只能夠對檔案進行下載,完全不能上傳或是異動檔案
這是因為WordPress的擁有者必須是為www-data,因此vsftp這組帳號對WordPress並沒有寫入的權限
除非你WordPress的權限改為777,但是如此一來又有安全性上的疑慮
如果你希望能夠寫入,這裡提供一個簡單的方式,就是直接將vsftp加入至www-data群組當中
再把WordPress目錄及底下所有檔案擁有者改為www-data:www-data
這樣的好處是你可以不必更動WordPress的所有權,同時又可以讓vsftp擁有權限
聽起來非常的複雜,但其實只要三行指令就可以做到這件事了

1
2
3
4
5
6
7
8
# 將使用者加入至www-data群組
$ sudo usermod -aG www-data vsftp

# 將WordPress目錄及檔案擁有者改為www-data
$ sudo chown -R www-data:www-data /home/vsftp/ftp/wordpress

# 設定目錄及檔案權限
$ sudo chmod -R 775 /home/vsftp/ftp/wordpress

接下來你就可以在FTP上對WordPress進行操作囉~

你可以在過程中使用ls -l觀察這其中的變化,這也代表linux在安全性上做得很好
我也是在這之中學到很多基礎的知識,linux真是個有趣的世界呢

注意上傳至WordPress的檔案也記得要手動將檔案擁有者改為www-data!