vsftpdとはLinuxでFTPサーバーとして運用できるソフトウェアである。vsftpdの意味としては「Very Secure FTP Daemon」ということで、「非常に安全なFTP Linuxサービス」といったところだろう。FTPとは「File Transfer Protocol(ファイル転送プロトコル)」である。
vsftpdを使うことによって、FFFTPなどからファイルの転送が可能になる。ウェブブラウザからはURIスキームをftpに指定して、「ftp://~」とアクセスできるようになる。
インストールはdebian系Linuxならば「sudo apt-get install vsftpd」コマンドでインストールを行える。Linux Mintであればソフトウェアの管理からインストールすることもできる。
インストールが完了したら、apt-cache show vsftpdコマンドでパッケージを確認することができる。バージョンなどを調べたい場合などにはこのコマンドを実行してみよう。私の環境での実行結果を示す。
$ apt-cache show vsftpd
Package: vsftpd
Architecture: amd64
Version: 3.0.3-9build1
Priority: extra
Section: net
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Keng-Yu Lin <kengyu@lexical.tw>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 326
Provides: ftp-server
Depends: debconf (>= 0.5) | debconf-2.0, libc6 (>= 2.15), libcap2 (>= 1:2.10), libpam0g (>= 0.99.7.1), libssl1.1 (>= 1.1.0), libwrap0 (>= 7.6-4~), adduser, libpam-modules, lsb-base (>= 3.0-6), netbase
Recommends: logrotate, ssl-cert
Conflicts: ftp-server
Replaces: ftp-server
Filename: pool/main/v/vsftpd/vsftpd_3.0.3-9build1_amd64.deb
Size: 115388
MD5sum: 6ff851047c5ab16d7aaf4e9a03290fd9
SHA1: 1979a0355bed3998ab058df5f208a6bc0cdfe5d0
SHA256: a18dcb6b82dd8c5c2391e77be30b003c1efc534baa254b7b2e9bed5707d73ce4
Homepage: http://vsftpd.beasts.org/
Description-en: lightweight, efficient FTP server written for security
This package provides the "Very Secure FTP Daemon", written from
the ground up with security in mind.
.
It supports both anonymous and non-anonymous FTP access, PAM authentication,
bandwidth limiting, and the Linux sendfile() facility.
Description-md5: 81386f72ac91a5ea48f8db0b023f3f9b
Supported: 5y
次に、systemdのユニットファイルも確認しよう。私の環境では/lib/systemd/systemディレクトリのvsftpd.serviceというファイルがvsftpdのユニットファイルである。/etc/systemd/system/multi-user.target.wantsディレクトリのシンボリックリンクファイルからリンクしている。ユニットファイルの内容を示す。
[Unit]
Description=vsftpd FTP server
After=network.target
[Service]
Type=simple
ExecStart=/usr/sbin/vsftpd /etc/vsftpd.conf
ExecReload=/bin/kill -HUP $MAINPID
ExecStartPre=-/bin/mkdir -p /var/run/vsftpd/empty
[Install]
WantedBy=multi-user.target
ここで実体は/usr/sbin/vsftpdであることと、設定ファイルは/etc/vsftpd.confであることが確認できる。
ユニットファイルを参照することで設定ファイルは/etc/vsftpd.confにあることを確認できた。そうでなくとも、/etcディレクトリを探せば見つかるだろう。私の環境で変更した点を解説する。
1.IPプロトコルの変更
必要に応じて、IPv6の設定になってる場合はIPv4にする。(vsftpdのVersion3.0.3-12ではIPv6がデフォルト)
listen=YES
listen_ipv6=NO
2.匿名ユーザの禁止
FTPアクセスするときにanotymousでアクセスすることを禁止する。anotymousにすると誰でもユーザ名とパスワードなしでアクセスできるが、禁止した場合はユーザ名とパスワードが必要になる。
Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
anonymous_enable=YESとなっているところをNOに変更する。
3.書き込みコマンドの有効化
デフォルトではFTPからファイルやディレクトリの変更をすることができない。セキュリティを考えれば安全であるが、アップロードできないので書き込みを有効化する。
Uncomment this to enable any form of FTP write command.
write_enable=YES
#write_enable=YESのコメント(#)を外す。
4.umaskの変更
Nginxなどのウェブサーバーの公開ディレクトリとvsftpdのディレクトリを重ねておけば、ファイルアップロードと同時にウェブサイトを更新することができる。
ただし、nginxでuser設定をnginxにして、vsftpdのユーザ(後述)がcpageなどnginx以外になっていて、nginxユーザとvsftpdユーザが異なっている場合を考える。デフォルトではumaskが077になっていてアップロードしてもその他ユーザ、つまりウェブサーバーではファイルを読み込むことができない。そのような場合にumaskを変更する
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
#local_umask=022のコメントを外す。
もし、vsftpdの設定はumaskが077のままで、ユーザ名cpageに統一したい場合のnginx等の設定例を示す。
/etc/php/7.2/fpm/pool.d/www.conf
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
/etc/php/7.2/fpm/php.ini
sudo chown -R root.cpage /var/lib/php/session
/etc/nginx/nginx.conf
user cpage;
5.ルートディレクトリの変更
ftpのルートディレクトリを明示する設定を行う。デフォルトではファイルシステムのルートディレクトリからすべてのディレクトリに移動できる。ルートディレクトリを明示することでディレクトリ移動を制限する。
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
#chroot_local_user=YESおよび#chroot_list_enable=YESのコメントを外す。
トップディレクトリがどこかについては後述する。
6.追記する設定
その他、必要な追記を示す。
userlist_enable=YES
userlist_deny=NO
user_config_dir=/etc/vsftpd_user_conf
allow_writeable_chroot=YES
userlist_enable=YES はユーザリストを使用することを示す。userlist_deny=NO はユーザリストが拒否リストではない、つまり許可リストであることを示す。user_config_dir=/etc/vsftpd_user_conf はユーザごとの設定ディレクトリを示す。allow_writeable_chroot=YES は「3.ルートディレクトリの変更」で行った設定でログインできるようにする。
/etc/vsftpd.conf設定ファイルの内容にはユーザリストとユーザごとの設定が必要であり、別途ファイルを作成する必要がある。
ユーザリストは/etcディレクトリにvsftpd.user_listファイルを作成する。内容はユーザリストである。cpage1人の場合は単にcpageと書き込めばよい。
ユーザの設定ファイルは/etc/vsftpd_user_confディレクトリにユーザ名のファイルを作成する。私の環境ではcpageというファイルを作成する。ルートディレクトリに設定をしているので例として以下の内容を書き込む。
local_root=/usr/share/nginx
これで完了である。ffftpなどでアクセスできるはずである。また、ウェブブラウザから「ftp://~」としてもアクセスできる。
設定ファイルはいくつもあるので私の環境を例にまとめる。
/etc/vsftpd.chroot_list デフォルトであるが、今回は使用しない。
/etc/vsftpd.conf メインとなる設定ファイル。
/etc/vsftpd.user_list ユーザリスト。今回は許可リストとして使用する。
/etc/vsftpd_user_conf/cpage ユーザの設定ファイル。ここでルートディレクトリを設定した。