SSブログ

---追記用---「vsftpd」の設定 [vsftpd]

---追記用---「vsftpd」の設定

■「/etc/vsftpd/」のデフォルトの状態確認。

       
[root@centos vsftpd]# ll
合計 20
-rw-------. 1 root root  125  ftpusers
-rw-------. 1 root root  361  user_list
-rw-------. 1 root root 4600  vsftpd.conf
-rwxr--r--. 1 root root  338  vsftpd_conf_migrate.sh
       
      
 必要に迫られたときに調べた内容を追記していくページにします。

■「/etc/vsftpd/ftpusers」のデフォルトの状態確認。

       
# Users that are not allowed to login via ftp
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

       
      

■「/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/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

       
      

■「/etc/vsftpd/vsftpd.conf」のデフォルトの状態確認。

       
# Example config file /etc/vsftpd/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
#
# 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 ←「#」外すとパーミッション「755」に。
#
# 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 ←「chroot_list_file」のファイルを使用する宣言。
# (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
#
# 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

       
      

■ローカルユーザーの上位ディレクトリ参照の制限。

       
[root@centos vsftpd]# vi /etc/vsftpd/vsftpd.conf
---省略---
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list

ftpクライアント→ログイン
500 OOPS: unrecognised variable in config file: allow_writable_root ←エラー。

[root@centos vsftpd]# vi /etc/vsftpd/vsftpd.conf
---省略---
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
allow_writeable_chroot=YES ←追記。
       
      
 初めてローカルユーザーにて接続したところ、「/」まで参照できたので驚きました。
 まぁ、ターミナルで移動できる場所を考えれば、それと同様の移動ができるのは自然なことなのでしょう。
 ただローカルユーザーを他人に使わせるために作成した場合、上位ディレクトリを参照されるのはセキュリティ上好ましくないような気がします。
 なのでこれを制限する方法を記述しておきます。

 上記の設定ですと、「chroot_list」に記載されたユーザーがホームディレクトリの上位ディレクトリに移動できるという設定のはずです。
 しかし実際には、未記載「bibo-roku」ユーザーは「local_enable=YES」であるにも拘わらずログインさえできませんでした。
 試しに「chroot_list」「bibo-roku」を書き込むとログインはできるのですが、当然上位にも移動できてしまいました。
 調べてみると、上記の条件の場合「ホームディレクトリ(bibo-roku)」のパーミッションが「書き込み可能」になっているとログイン「できない」ということらしいです。
 試してみたところ、確かにログインできて上位に移動できなくなりました。
 しかしこれでは書き込み(アップロード)できないし……。

 で!
「allow_writeable_chroot=YES」これを追記することで「ローカルユーザーをホームディレクトリ以上のディレクトリに移動させない」が実現できました。

■「/etc/vsftpd/chroot_list」の作成。

       
[root@centos vsftpd]# vi /etc/vsftpd/vsftpd.conf
---省略---
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list ←使用するためにはファイルを作成する必要あり。
allow_writeable_chroot=YES

[root@centos vsftpd]# vi chroot_list
root
bibo-roku

[root@centos vsftpd]# ll
合計 24
-rw-------. 1 root root chroot_list

       
      
 上記の設定だと「chroot_list」に記載されているローカルユーザーはホームディレクトリの上位ディレクトリへ移動できる。を実現します。
 そのリストはデフォルトではないので、作ります。パーミッションは「600」で良いようです。

■「SElinux」を有効にして動作させる。

       
[bibo-roku@centos ~]$ sudo /usr/sbin/getsebool -a | grep ftp ←ルール確認コマンドツール。
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off  ←ホームディレクトリへのアクセス。
ftpd_connect_db --> off
ftpd_use_fusefs --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_use_cifs --> off
tftp_use_nfs --> off

[bibo-roku@centos ~]$ sudo setsebool -P ftp_home_dir 1 ←「1」で「on」に。

[bibo-roku@centos ~]$ sudo /usr/sbin/getsebool -a | grep ftp ←確認。
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> on ←変更された。
ftpd_connect_db --> off
ftpd_use_fusefs --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_use_cifs --> off
tftp_use_nfs --> off
       
      
「SElinux」を有効にしてセキュリティを有効にしてみる。
 anonymousユーザーではanonymousユーザーのデフォルト設定で(アップロードはできないとか)接続ができました。
 しかし、一般ユーザー(今回はbibo-roku)は接続が不可能でした。homeディレクトリへのアクセスが禁止されている。とのこと。
「getsebool -a | grep ftp」はSELinuxの組み込みルールの有効/無効状態(boolean)を確認するコマンドツールで、「ftp」だけを取り出しました。
 ふーむ。つまりこれだけの項目が「SElinux」により制御されているのでしょうかね。
 今回は「ftp_home_dir」を「on」に変更します。
「setsebool [-P] <boolean><value>」にて設定します。[-P]を付けることによりシステム設定に自動反映され、次回起動時にも反映されます。 <value>は「0=off」「1=on」です。
「SElinux」や「vsftpd」の再起動無くhomeディレクトリへのアクセスが可能になりました。



「vsftpd」のソースからRPMファイルを作成する。 [vsftpd]

「vsftpd」のソースからRPMファイルを作成する。

■「vsftpd」のダウンロード、インストール。

       
[bibo-roku@centos Downloads]$ wget http://vsftpd.devnet.ru/files/3.0.2/packages/rhel6/vsftpd-3.0.2-1.el6.src.rpm ←ソースをダウンロード。

[bibo-roku@centos Downloads]$ su 
パスワード:
[root@centos Downloads]# mv vsftpd-3.0.2-1.el6.src.rpm /home/mockbuild/rpmbuild/SOURCES/ ←移動。

[root@centos SOURCES]# chown mockbuild:mockbuild vsftpd-3.0.2-1.el6.src.rpm ←所有者変更。

[root@centos SOURCES]# rpm2cpio vsftpd-3.0.2-1.el6.src.rpm | cpio --list ←ソースの一覧表示。
vsFTPd-3.0.2-ext1.tgz
vsftpd-ext-ssl.patch
vsftpd-ext-tcp_wrappers.patch
vsftpd.ftpusers
vsftpd.init
vsftpd.pam
vsftpd.spec ←「SPEC」ファイル。
vsftpd.user_list
vsftpd.xinetd
vsftpd_conf_migrate.sh
463 blocks

[root@centos SOURCES]# su mockbuild ←「rebuild」コマンド用ユーザー。

[mockbuild@centos SOURCES]$ rpmbuild --rebuild --clean vsftpd-3.0.2-1.el6.src.rpm ←ソースから作成。
vsftpd-3.0.2-1.el6.src.rpm をインストール中です。
警告: ユーザ phantom は存在しません - root を使用します ←「mockbuil」ではなかった。
警告: グループ shared は存在しません - root を使用します
---省略---
エラー: ビルド依存性の失敗:
        libcap-devel は vsftpd-3.0.2-1.el6.i386 に必要とされています ←不足分。

[root@centos SOURCES]# yum install libcap-devel ←不足分インストール。
---省略---
インストール:
  libcap-devel.i686 0:2.16-5.5.el6                                                              
完了しました!

[root@centos SOURCES]# rpmbuild --rebuild --clean vsftpd-3.0.2-1.el6.src.rpm ←「root」で再度。 
---省略---
書き込み完了: /root/rpmbuild/RPMS/i386/vsftpd-3.0.2-1.el6.i386.rpm ←作成完了。
---省略---

[root@centos ~]# ll /root/rpmbuild/RPMS/i386/vsftpd-3.0.2-1.el6.i386.rpm
-rw-r--r--. 1 root root 318491  /root/rpmbuild/RPMS/i386/vsftpd-3.0.2-1.el6.i386.rpm

[root@centos i386]# rpm -ivh vsftpd-3.0.2-1.el6.i386.rpm
準備中...                ########################################### [100%]
   1:vsftpd                 ########################################### [100%]
       
      
「vsftpd」に関しては先日はコンパイルしてインストールできたのですが、「Webmin」での設定が上手く反映されたりしなかったので、「RPM」ファイルでインストールした物と比較するために、ソースファイルから「rpmbuild」コマンドで「RPM」ファイルを作成してインストールしてみたいと思います。

 実は公式HPには「rpm」「src.rpm」のファイルがありませんでした。
「yum」コマンドではバージョンが低いので困っていたのですが、下記アドレスにファイルが存在しました。

http://vsftpd.devnet.ru/files/3.0.2/packages/rhel6/vsftpd-3.0.2-1.el6.src.rpm
http://www.rpmseek.com/rpm/vsftpd-3.0.2-6.fc20.i686.html?hl=com&cx=0:-:0:12063675:0:0:0:

 ファイルをダウンロードしたら「rpmbuild」コマンドを使用するために以前作成した「mockbuild」ユーザーの作業ディレクトリに移動させます。その際、「su」ユーザーでないと移動できません。

 一応、所有者を「mockbuild」にして「rpm2cpio」コマンドで「SPEC」ファイルの有無を確認してみます。
「SPEC」ファイルはじめ、先日確認したファイル群がありますね。

 いよいよ「rpmbuild --rebuild」コマンドでソースファイルから「RPM」ファイル作成に入ります。
 警告。ぶっ。以前警告がでるので「mockbuild」ユーザーを作ったのですが、今回はまた別のグループとユーザーがコマンド使用には必要だったようです。「SPEC」ファイルにでも記載されているのでしょうか。
 もう面倒なので、「root」で進めてしまいましょう。

 で、不足分のパッケージをインストールします。
 再度作成し、完成しました。場所は「root」になっているので留意しておきましょう。

 出来上がった「vsftpd-3.0.2-1.el6.i386.rpm」をインストールします。


■「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 7314) を実行中...
       
      
 ポートを空けて、再起動してやり再度「vsftpd」を起動してやります。
 そして起動を確認。動いてくれているようですね。


■「FTPクライアント」で接続。

       
[root@centos ~]# cp /etc/vsftpd/vsftpd.conf /var/ftp/pub/ ←コピー。

[root@centos pub]# ll
合計 8
-rw-------. 1 root root 4600  5月 15 00:12 2015 vsftpd.conf
[root@centos pub]# chmod 755 vsftpd.conf ←パーミッション変更。

Microsoft Windows [Version 6.3.9600] ←コマンドプロンプト。
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\westbird>ftp ←FTPクライアントに。
ftp> open ←開始。
宛先 192.168.0.66 ←接続先。
192.168.0.66 に接続しました。
220 vsFTPd 3.0.2+ (ext.1) ready...
ユーザー (192.168.0.66:(none)): ftp ←ユーザー名(anonymous)。
331 Please specify the password.
パスワード: ←なし。
230 Login successful.
ftp> ls ←リスト。
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
pub
226 Directory send OK.
ftp: 8 バイトが受信されました 0.00秒 8.00KB/秒。
ftp> cd pub ←フォルダ移動。
250 Directory successfully changed.
ftp> ls 
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
vsftpd.conf
226 Directory send OK.
ftp: 16 バイトが受信されました 0.00秒 16.00KB/秒。
ftp> get vsftpd.conf ←ダウンロード。
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for vsftpd.conf (4600 bytes).
226 Transfer complete.
ftp: 4600 バイトが受信されました 0.00秒 4600.00KB/秒。
ftp> quit ←終了。
221 Goodbye.


C:\Users\westbird>ftp
ftp> open 192.168.0.66
192.168.0.66 に接続しました。
220 vsFTPd 3.0.2+ (ext.1) ready...
ユーザー (192.168.0.66:(none)): bibo-roku ←ローカルユーザー名。
530 This FTP server is anonymous only. ←エラー。
ログインできませんでした。
ftp>

[root@centos ~]# vi /etc/vsftpd/vsftpd.conf ←コンフィグファイル編集。
---省略---
# 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 ←コメントアウト外す。
---省略---

C:\Users\westbird>ftp ←再度。
ftp> open
宛先 192.168.0.66
192.168.0.66 に接続しました。
220 vsFTPd 3.0.2+ (ext.1) ready...
ユーザー (192.168.0.66:(none)): bibo-roku ←ローカルユーザー。
331 Please specify the password.
パスワード:
230 Login successful. ←接続確認。
       
      
 まずは、今回のコンフィグファイルと、コンパイルした場合のコンフィグファイルに違いがあるのか確かめたいので「Windows」にダウンロードします。
「/etc/vsftpd」ディレクトリ内にあるコンフィグファイルを「ftp」ユーザーのルートディレクトリ以下の「pub」内にコピーします。
 FFFTPクライアントにて「anonymous」で接続。成功。ファイル目視完了。ダウンロード失敗。
 原因はパーミッションで「600」を「755」にしてダウンロード成功。

 続いて「bibo-roku」(ローカルユーザー)で接続してみると、「530 This FTP server is anonymous only.」とのこと。
 へぇ。デフォルトでは「anonymous」と「ftp」ユーザーは接続可能だけど、「ローカルユーザー」は駄目なんですね。

 ということで、「/etc/vsftpd/vsftpd.conf」を「vi」コマンドで開き「local_enable=YES」が「ローカルユーザー」の接続(ログイン)許可です。
 関連して「write_enable=YES」(書き込み系コマンド許可)や「local_umask=022」(パーミッションを「755」に。)を設定して、「vsftpd」を再起動します。

 それにしても、理解できている設定をして、自由に動かせると気持ちいいですね!

■「anonymous」ユーザーでアップロード。

       
「FFFTP(Win)(クライアント)」→「vsftpd(CentOS)(サーバ)」 ←接続完了。
550 Permission denied. ←ファイルアップロードエラー。

「Webmin」Anonymous can upload:「Yes」 ←「Yes」に変更。
「Save Changes」→「vsftpdに戻る」→「Restart vsftpd Server」

「FFFTP(Win)(クライアント)」→「vsftpd(CentOS)(サーバ)」 ←接続完了。
553 Could not create file. ←ファイルアップロードエラー。

[root@centos ~]# ll /var/ftp/
合計 4
drwxr-xr-x. 2 root ftp 4096  5月 15 03:47 2015 pub ←「755」を「777」に変更。

「Webmin」
「Save Changes」→「vsftpdに戻る」→「Restart vsftpd Server」

「FFFTP(Win)(クライアント)」→「vsftpd(CentOS)(サーバ)」 ←接続完了。←ファイルアップロード完了。
       
      
「Webmin」で「anonymous」ユーザー時でもアップロードができるように設定したにも拘わらず実行できない理由を調査しました。

---追記---
 下記問題について、次の項目にて解決しています。
---追記---

「FFFTP」(クライアント)「anonymous」ユーザーで接続。ファイルアップロード。
「550 Permission denied.」エラー発生。
 これを解決するのには、「vsftpd.conf」の「#anon_upload_enable=YES」のコメントアウトを外す必要があり同時に「#write_enable=YES」こちらも外すとのこと。

 これらの操作を「Webmin」がしてくれると思い、操作をするも、「vsftpd.conf」には見た目変化なし。
 しかし再度接続してみると、エラーが「553 Could not create file.」変化しています。
 アップロード先のディレクトリ「/ver/ftp/pub」のパーミッションを「775」から「777」に変更。

「vsftpd」再起動後接続。アップロード成功。しかし「vsftpd.conf」に変化なし。
 とりあえず、変化なしは後に置くことにして、
「550 Permission denied.」には「Webmin」Anonymous can upload:「Yes」
「553 Could not create file.」にはパーミッションの変更。
 で対処できました。
 前者は「vi」コマンドなら「#anon_upload_enable=YES」を外すことで対処できました。

 結論。
 初めて「Webmin」で「anonymous」ユーザーのアップロードを許可しようと操作しても、アップロード先の「/var/ftp/pub」のパーミッションを「777」にしないとアップロードはできない。
 つまり、「777」にしておけば「Webmin」で初接続して「Yes」にすればアップロードはできたと思われます。アップロードできなかったのはパーミッションのせいだったと。
 また、「Webmin」で「anonymous」ユーザーのアップロードを許可する操作は、「vsftpd.conf」において「#」が外れた記述にならない。しかし設定は外された状態になる。
 つまり「Webmin」上の設定と「vsftpd.conf」上の設定に連動性が見られない。ただ、「Webmin」上では「Yes」と「No」で選択するが、「vsftpd.conf」上では「#」を付けたり外したりで設定するが、「No」も使えるはずで、その辺が絡んでくるのかも知れない。

 通常、「anonymous」にアップロードは許可しないので問題ありませんが、ちょっと混乱してしまいました。この部分に関してはコマンドラインで設定するのが良いかも知れません。
 ↓解決。


■「Webmin」での設定変更と「vsftpd.conf」との関連

       
例:「anonymous」ユーザーにアップロード権限を与える。

「vsftpd.conf」を手動で設定する場合。
[root@centos ~]# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
write_enable=YES
#anon_upload_enable=YES ←コメントアウトを外す。

「Wibmin」を使用して設定する場合。
「サーバ」→「vsftpd」→「anonymous FTP」→「anonymous enable」→「yes」→「anonymous can uploads」→「yes」→「save changes」
       
      
 いくつかサイトを回ってみたのですが、「vsftpd.conf」を手動で設定する場合、基本的に設定は 「~~~=YES」
 と表記されており
「#~~~=YES」は「No」を表し(というか「設定なし」=「No」という感じかな)、
「~~~=YES」は「Yes」を表しています。

 次に「Webmin」で設定する場合、HTMLのラジオボタン形式で「Yes」「No」を明示的に選択するようになっています。当方は
「Yes」は「~~~=YES」
「No」は「#~~~=YES」
 として、「vsftpd.conf」に反映されると考えておりました。

 しかし実際には「Webmin」で設定し、アップロードはできるようになったのに「vsftpd.conf」の「#anon_upload_enable=YES」はコメントアウトされたままだったので、混乱してしまいました。
「anon_upload_enable=YES」となると考えていたのです。

 色々設定をいじってみたのですが、結果解ったのは、
anon_upload_enable=YES
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
 これらが追記されておりました。「vsftpd.conf」の一番最後に。見落としていました……。
 ごらんのように、「Webmin」で設定した場合、「#」ではなく普通に「YES」「NO」で追記する形で変更されると言うことです。
手動、「Webmin」の両方で設定を変えている場合は、気をつける必要があるかも知れません。

「vsftpd」をインストールする。 [vsftpd]

「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クライアントモード」を起動し、ファイルを確認してダウンロードします。
 できましたー!
 あとは、専用のユーザーを作ってアップロードなども試しましょう。



この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。