LoginSignup
15
9

More than 3 years have passed since last update.

CentOSにvsftpdを入れる

Posted at

なにするん

CentOSにFTPサーバーを作ります。
セキュリティ考慮してパッシブにすることが多いと思うので
今回もパッシブモードで構築。

前提

扱うOS

$ cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

ネットワーク

今回はAWS EC2で構築するのでiptablesなどの設定はしません。

構築

インストール

$ sudo yum install vsftpd

バージョン確認します

$ vsftpd -v
vsftpd: version 3.0.2

どこにいるの

daemonはここにいます。

$ ll /usr/sbin/vsftpd 
-rwxr-xr-x. 1 root root 175480 10月 30  2018 /usr/sbin/vsftpd

設定ファイルはこれだけあります。(上3つ)

$ ll /etc/vsftpd/
合計 20
-rw-------. 1 root root  125 10月 30  2018 ftpusers
-rw-------. 1 root root  361 10月 30  2018 user_list
-rw-------. 1 root root 5116 10月 30  2018 vsftpd.conf
-rwxr--r--. 1 root root  338 10月 30  2018 vsftpd_conf_migrate.sh

ドキュメントはこちらでした。

$ ll /usr/share/doc/vsftpd-3.0.2/
合計 160
-rw-r--r--. 1 root root  1392  2月  2  2008 AUDIT
-rw-r--r--. 1 root root  2908  2月  2  2008 BENCHMARKS
-rw-r--r--. 1 root root   822  3月 28  2012 BUGS
-rw-r--r--. 1 root root 18288  2月  2  2008 COPYING
-rw-r--r--. 1 root root 67038  9月 18  2012 Changelog
(略)

サービス起動

インストール後はもちろん起動していないので

$ sudo systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

起動します。

$ sudo systemctl start vsftpd

$ sudo systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 水 2019-09-04 06:28:35 UTC; 4s ago
  Process: 8259 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
 Main PID: 8260 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─8260 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

 9月 04 06:28:35 ip-192-168-2-251.ap-northeast-1.compute.internal systemd[1]: Starting Vsftpd ftp daemon...
 9月 04 06:28:35 ip-192-168-2-251.ap-northeast-1.compute.internal systemd[1]: Started Vsftpd ftp daemon.

FTPサーバーとして自動起動設定をしておきます。

$ sudo systemctl is-enabled vsftpd
disabled

有効化

$ sudo systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

念の為確認。

$ sudo systemctl is-enabled vsftpd
enabled

そのまま使う

デフォルトの動作

ネットワークがオープンならば、そのまま匿名でのアクセスが可能です。

/etc/vsftpd/vsftpd.conf(抜き出し)
# 匿名FTPサーバーの設定
anonymous_enable=YES

例えば外部からアクセスする際は、'anonymous'ユーザでアクセス可能。

$ ftp {FTP server IP}
Connected to {FTP server IP}.
220 (vsFTPd 3.0.2)
Name (hostusername): anonymous
331 Please specify the password.
Password: (何でも)
230 Login successful.
ftp> 

この時、ディレクトリは'/var/ftp/'です。
では、ファイルを置いたりできるのか?

ftp> put test
200 PORT command successful. Consider using PASV.
550 Permission denied.

まあ、そりゃそうだ(安心)

ユーザを作成してanonymousを使用しない想定なので、拒否設定。

コピっといてから

$ sudo cp -p /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.org
/etc/vsftpd/vsftpd.conf(抜き出し)
# 匿名FTPサーバーの設定
anonymous_enable=NO

パッシブ設定とユーザ作成

クライアント -> サーバでコネクションを確立する パッシブモード に設定し、
ユーザを明示的に作成します。

今回はLinuxユーザ作成、FTPユーザ作成をしようと思います。
まずは /etc/vsftpd/user_list にユーザを追加。ユーザ名はshinon。

sudo echo 'shinon' | sudo tee -a /etc/vsftpd/user_list

nobodyまではデフォルトで存在します。

/etc/vsftpd/user_list
…
operator
games
nobody
shinon

このファイルに描かれるユーザが許可リストになるように、あとで
userlist_deny=NO に変更しています(後述)

また、ユーザが上位の階層の使用できないよう制限します。
他、必要な項目は以下。

/etc/vsftpd/vsftpd.conf

#コメント外す
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
ls_recurse_enable=YES

#YESに変更
listen=YES

#コメントアウトする(IPv4とダブるとエラー)
#listen_ipv6=YES

#変更
userlist_deny=NO #許可リストにする
userlist_file=/etc/vsftpd/user_list

#今回セキュリティグループで制限しているためNOにする
tcp_wrappers=NO

#以下パッシブモードの追記する
pasv_promiscuous=YES
pasv_enable=YES
pasv_address={グローバルIP}
pasv_min_port=55000
pasv_max_port=55020

ポートは、21をパッシブで受け付けますが、接続時の使用範囲を上記のように指定。

また、/etc/vsftpd/chroot_list に制御から除外するユーザを記載。
この設定ファイルは無いので、作ります。

sudo echo 'shinon' | sudo tee -a /etc/vsftpd/chroot_list

追加しました。

/etc/vsftpd/chroot_list
shinon

ログインして、ローカルに置いてあるtestファイルを送ってみます。

$ ftp {FTPサーバーIP}
Connected to {FTPサーバーIP}.
220 (vsFTPd 3.0.2)
Name (hostusername): shinon
331 Please specify the password.
Password: 
230 Login successful.
ftp> put test
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.

まとめ

vsftpdはすぐ使える便利なFTPサーバーです。
こんな感じでCentOSをベースにLinuxのサーバー構築記録を残していきます。

ありがとうございました。

補足

vsftpdとは

18.2.2. vsftpd サーバー Red Hat Enterprise Linux 6 | Red Hat Customer Portal から引用

Red Hat Enterprise Linux に標準装備されている、唯一の独立型 FTP サーバーです。

詳しくはこちらを参照。
vsftpd - Secure, fast FTP server for UNIX-like systems

Very Secure Ftp Daemonは、
・高速である
・セキュリティ的に安全である
ことを目指し作成されたFTPサーバーです。

Chris Evans氏が開発しました。

15
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
9