「vsftpd」をインストールする。
■「vsftpd」のインストール準備。
[root@centos ~]# rpm -qa | grep vsftpd ←存在の確認。
[root@centos ~]# yum list installed | grep vsftpd ←存在の確認。
https://security.appspot.com/vsftpd.html ←公式サイト確認。
[root@centos SOURCES]# wget https://security.appspot.com/downloads/vsftpd-3.0.2.tar.gz /root/rpmbuild/SOURCES/ ←ダウンロード。
[root@centos SOURCES]# tar zxvf vsftpd-3.0.2.tar.gz ←展開。
[root@centos SOURCES]# ll vsftpd-3.0.2 ←「SPECファイルの確認:なし」
[root@centos SOURCES]# chown -R root:root vsftpd-3.0.2 ←所有者の変更。
[root@centos SOURCES]# cd vsftpd-3.0.2
[root@centos vsftpd-3.0.2]# less INSTALL ←確認。
---省略---
edit "builddefs.h" to handle compile-time settings (tcp_wrappers build,
etc).
---省略---
[root@centos vsftpd-3.0.2]# vi builddefs.h ←編集。
#ifndef VSF_BUILDDEFS_H ←変更無し。
#define VSF_BUILDDEFS_H ←変更無し。
#undef VSF_BUILD_TCPWRAPPERS ←原文。現状無効。
#define VSF_BUILD_TCPWRAPPERS ←「define」に変更。
#define VSF_BUILD_PAM ←現状有効。
#undef VSF_BUILD_SSL ←原文。現状無効。
#define VSF_BUILD_SSL ←「define」に変更。
#endif /* VSF_BUILDDEFS_H */ ←変更無し。
[root@centos vsftpd-3.0.2]# make
[root@centos vsftpd-3.0.2]# mkdir -p /usr/local/man/{man5,man8}
|
|
「Windows」などから「CentOS」にファイルをアップロードしたりダウンロードをした際に「改行コード」や「文字コード」の違いによって文字化けが起きたりする問題に「CGI」を動かす辺りで非常に苦しみました。
それが「FTP」を利用してアップ/ダウンロードする場合にはそれらの違いを自動に行ってくれるとのことなので(無論設定は初めにするのでしょうが)、「FTPサーバ」は立てて置いた方が良いかも知れないということで、挑戦です。
まずは既に古いバージョンがインストールされていないか確認しました。無いようです。
公式サイトで最新バージョンをダウンロードします。(The latest vsftpd release is v3.0.2)
ダウンロードファイルを置くディレクトリにいつも迷ってしまいます。今回は「/root/rebuild/SOURCES」に。(その後サイトを見たら「cd /usr/local/src」が多かったです。)
ファイルを展開し、作成されたディレクトリの内部を「ll」コマンドで確認します。
「SPECファイル」が存在すれば、「rpmbuild」コマンドを使用して「RPM」ファイルを作成することができると思ったのですが、無いようなので「コンパイル」をしないといけないようです。
作成されたディレクトリの内部にある「INSTALL」ファイルを「less」コマンドで開いてインストール時の指示を読んでみます。
「builddefs.h」というファイルを編集するようですね。
「vi」コマンドで開いてみると、意外と少ない行数でした。調べてみましょう。
「#ifndef」と「#endif」はセットで使うようでコンパイルの前に実行されるコマンドのことだそうです。初めと終わりという感じなのでこれらに挟まれた「VSF_BUILD_TCPWRAPPERS」「VSF_BUILD_PAM」「VSF_BUILD_SSL」をいじれば良いのかと思います。
「#define」と「#undef」が「有効」と「無効」に当たると思います。(コメントアウトを外すか否かと思いました。)つまり現状では「VSF_BUILD_PAM」だけが有効と言うことですね。これを自分の環境に合ったものに編集します。
「PAM=認証」「SSL=暗号化」「TCPWRAPPER=フィルタリング」と言ったところでしょうか。つまり、セキュリティについての設定ですね。
とりあえず全て「define」としてみます。
「make」を実行するわけですが、ミスを犯しました。当方は作業をしながらメモ代わりにブログの記事を書いているのですが、「define」としたつもりが記事には書いたものの、実際の方は無変更のまま「make」をしてしまいました。
「make」のやり直しには「make clean」それでだめなら「make mrproper 」とのことで前者でやり直せました。メモ。
「man」ディレクトリを作成します。マニュアル関係のようです。
■「vsftpd」のインストール、ファイルコピー。
[root@centos vsftpd-3.0.2]# make install ←インストール。
[root@centos vsftpd-3.0.2]# mkdir /etc/vsftpd ←ディレクトリを作成し
[root@centos vsftpd-3.0.2]# cd RedHat/
[root@centos RedHat]# cp vsftpd.log /etc/logrotate.d/vsftpd ←ファイルコピー。
[root@centos RedHat]# cp vsftpd.pam /etc/pam.d/vsftpd ←ファイルコピー。
[root@centos RedHat]# cd ~/
[root@centos ~]# mkdir /var/ftp ←匿名FTPのルート。
[root@centos ~]# mkdir -p /home/ftp/pub ←ユーザー「FTP」のホーム。
[root@centos ~]# chown -R root:ftp /home/ftp ←所有者変更。
[root@centos ~]# chmod 755 /home/ftp/ ←パーミッション変更。
|
|
「make install」を実施すると「/usr/local/sbin」に実行ファイル「vsftpd」が作成されます。
「PAM」用にファイルを指定場所にコピーしていきます。そのためのディレクトリ「/etc/vsftpd」作成です。にしても、全て手動なんですねぇ。所有者もパーミッションも変更します。
■「vsftpd.conf」の編集。
[root@centos ~]# cp /root/rpmbuild/SOURCES/vsftpd-3.0.2/vsftpd.conf /etc/vsftpd/ ←設定ファイルをコピー。
[root@centos vsftpd]# chown root:ftp 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.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES ←アノニマス接続許可。
#
anon_root=/var/ftp/pub ←一文追記。アノニマスのディレクトリ。
#
# 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
#
# 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 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 ←コメントアウト外す。
allow_writeable_chroot=YES ←一文追記。
#
# 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 ←コメントアウト外す。
#
#
pam_service_name=vsftpd ←一文追記。
userlist_enable=YES ←一文追記。
userlist_file=/etc/vsftpd/user_list ←一文追記。
use_localtime=YES ←一文追記。
text_userdb_names=YES ←一文追記。
pasv_min_port=50000 ←一文追記。
pasv_max_port=50030 ←一文追記。
#
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
#
# This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6
# sockets, you must run two copies of vsftpd with two configuration files.
# Make sure, that one of the listen options is commented !!
#listen_ipv6=YES
tcp_wrappers=YES ←コメントアウト外す。hosts.allow と hosts.deny を参照。
|
|
設定ファイルも解凍してできたディレクトリから指定場所にコピーして、それを編集します。
オプション編集項目の意味は書き込むと長くなるので省略します。検索すればいくらでも情報が手に入ります。
注意点は、作成したディレクトリ名を間違えないことですかね。
■各種ファイルの作成。
[root@centos ~]# touch /etc/vsftpd/chroot_list ←空ファイル
[root@centos ~]# chown root:root /etc/vsftpd/chroot_list ←変更。
[root@centos ~]# chmod 600 /etc/vsftpd/chroot_list ←変更。
[root@centos ~]# vi /etc/ftpusers ←ファイル作成。
# Users that are not allowed to login via ftp ←全文追記。
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@centos ~]# chown root:root /etc/ftpusers ←所有者変更。
[root@centos ~]# chmod 600 /etc/ftpusers ←パーミッション変更。
[root@centos ~]# vi /etc/vsftpd/user_list ←ファイル作成。
# vsftpd userlist ←全文追記。
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file,
# and do not even prompt for a password.
# Note that the default vsftpd pam config also checks
/etc/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody
[root@centos ~]# chown root:root /etc/vsftpd/user_list ←所有者変更。
[root@centos ~]# chmod 600 /etc/vsftpd/user_list ←パーミッション変更。
[root@centos ~]# vi /etc/hosts.allow ←編集。
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
ALL: 127.0.0.1 ←一文追記。
vsftpd: 192.168.0. ←一文追記。
[root@centos ~]# vi /etc/xinetd.d/vsftpd ←編集。
# default: on
# description:
# The vsftpd FTP server serves FTP connections. It uses
# normal, unencrypted usernames and passwords for authentication.
# vsftpd is designed to be secure.
service ftp
{
socket_type = stream
wait = no
user = root
server = /usr/local/sbin/vsftpd
# server_args =
# log_on_success += DURATION USERID
# log_on_failure += USERID
nice = 10
disable = no ←yesに変更。
}
[root@centos ~]# service xinetd restart
|
|
「chroot_list_file」は「/etc/vsftpd.conf」において「chroot_list_enable=YES」で有効の時に指定ファイルに記載されたユーザ を chroot_local_user の設定に基づいて制御する。とのこと。
まぁ、記載されたユーザーを制御すると言うことでしょう。「YES」の場合は対象ユーザーがいなくてもファイルは必要とのこと。ゆえに空ファイル。
「/etc/ftpusers」はFTPのアクセス制限をかけることができます。この中にユーザ名が記述されている場合、そのユーザはアクセスを拒否されます。
一行に一ユーザづつ記入していきます。とのこと。
「/etc/vsftpd/user_list」はvsftpd.confの設定により、user_listの意味が異なるとのことで注意が必要そうです。
「user_listファイルに追加したユーザーのアクセスを禁止する場合」(ブラックリストかな)
userlist_enable=YES で
userlist_deny=YES のとき。
「user_listファイルに追加したユーザーのアクセスを許可する場合」(ホワイトリストかな)
userlist_enable=YES で
userlist_deny=NO のとき。
「/etc/hosts.allow」(denyもある)は「vsftpd」だけのファイルというわけではないようです。
このファイルに記載されているものは許可されるということで、
書式は「デーモン名:ホスト名またはIPアドレス 」という形で示されます。
今回は、全てのデーモンは「ローカルホスト」からは接続可能。「FTPサーバ」は「192.168.0.」からは接続可能。ということでしょうかね。
「xinetd」はスーパーサーバと呼ばれとあるポートに対するアクセスがあった場合に、各ポートに対応するデーモンを起動する。とのこと。
つまり普段は常時起動してなくても誰かからポートにアクセスがあれば自動に起動してくれるということでしょうか。
今回はこれを停止しておきます。
■「rcスクリプト」の作成。
[root@centos ~]# /etc/rc.d/init.d/vsftpd ←ファイル作成。
#!/bin/bash ←全文追記。
#
# vsftpd This shell script takes care of starting and stopping
# standalone vsftpd.
#
# chkconfig: 235 60 50
# description: Vsftpd is a ftp daemon, which is the program \
# that answers incoming ftp service requests.
# processname: vsftpd
# config: /etc/vsftpd/vsftpd.conf
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0
[ -x /usr/local/sbin/vsftpd ] || exit 0
RETVAL=0
prog="vsftpd"
start() {
# Start daemons.
if [ -d /etc/vsftpd ] ; then
for i in `ls /etc/vsftpd/*.conf`; do
site=`basename $i .conf`
echo -n $"Starting $prog for $site: "
/usr/local/sbin/vsftpd $i &
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog $site"
}
echo
done
else
RETVAL=1
fi
return $RETVAL
}
stop() {
# Stop daemons.
echo -n $"Shutting down $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
RETVAL=$?
fi
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac
exit $RETVAL
|
|
自分でソースコードをコンパイルしてインストールした場合は、rcスクリプトも自分で用意する必要があります。とのことです。
当然ですが、当方には絶対無理なので、下記サイト様にあるものをそのまま使用させていただきました。というか今回はほとんどこちらの情報を使用させていただいております。
誠にありがとうございました。
http://ja.528p.com/linux/centos6/ZH002-vsftpd.html
■「vsftpd」の動作確認。
[root@centos ~]# vi /etc/sysconfig/iptables ←編集。
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT ←一文追記。
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@centos init.d]# /etc/init.d/iptables restart ←再起動。
iptables: チェインをポリシー ACCEPT へ設定中filter [ OK ]
iptables: ファイアウォールルールを消去中: [ OK ]
iptables: モジュールを取り外し中: [ OK ]
iptables: ファイアウォールルールを適用中: [ OK ]
[root@centos ~]# chkconfig --add vsftpd ←起動スクリプトを管理対象に追加。
[root@centos init.d]# service vsftpd start ←起動。
vsftpd 用の vsftpd を起動中: [ OK ]
[root@centos init.d]# service vsftpd status ←確認。
vsftpd (pid 2476) を実行中...
|
|
よーやく起動までこぎ着けました。が、実は一度失敗しました。
逸ってしまった結果でしたが、「iptables」を編集してポートを開けてやるのを失念しておりました。
ポートを空けて、再起動してやり再度「vsftpd」を起動してやります。
そして起動を確認。動いてくれているようですね。
■「FTPクライアント」で接続。
[root@centos ~]# vi /home/ftp/pub/test.txt ←テキストファイル作成。
C:\Users\bibo-roku>ftp ←「Windows」コマンドプロンプト。
ftp> open
宛先 192.168.0.66
192.168.0.66 に接続しました。
220 (vsFTPd 3.0.2)
ユーザー (192.168.0.66:(none)): ftp
331 Please specify the password.
パスワード:
230 Login successful.
ftp> get test.txt
200 PORT command successful. Consider using PASV.
150 Opening ASCII mode data connection for test.txt (20 bytes).
226 Transfer complete.
ftp: 21 バイトが受信されました 0.00秒 21.00KB/秒。
|
|
起動確認は「service vsftpd status」で確認できましたが、実際にファイルのやりとりができるかも確認してみます。
現在ユーザーなどの設定もしておりませんので、「ftp」か「anonymous」を使うことにします。
まずは、「CentOS」の「anonymous」用に作ったディレクトリ「/home/ftp/pub/」に「test.txt」ファイルを「vi」コマンドで作成します。
次に、「Windows」で「コマンドプロンプト」で「FTPクライアントモード」を起動し、ファイルを確認してダウンロードします。
できましたー!
あとは、専用のユーザーを作ってアップロードなども試しましょう。