Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active July 27, 2016 09:10
Show Gist options
  • Save nishinoshake/aad4c898b194b74094fc1f9f6fdc4744 to your computer and use it in GitHub Desktop.
Save nishinoshake/aad4c898b194b74094fc1f9f6fdc4744 to your computer and use it in GitHub Desktop.
FTPの制限ユーザ作成

##制限の考え方 ユーザ毎ログインディレクトリを変更することで、
ルートなどの危ないところにはアクセスさせ内容にする。

ファイルの場所 => /etc/vsftpd/

FTPを使うのはWebサーバ用にファイルをアップする目的だと思うので、 FTPのためだけにユーザを作成せずに、バーチャルユーザを作成し、 そいつをapacheなどのユーザに化けさせて接続させてあげると、 Webサーバのユーザとファイルの所有者などがごちゃごちゃにならずによい。

バーチャルユーザのリストを作成

cd /etc/vsftpd/
vi vsftpd_user.txt
user01
user01_password
user02
user02_password

リストをDBファイルに変換

db_load -T -t hash -f vsftpd_user.txt vsftpd_user.db
chmod 600 vsftpd_user.db

##vsftpd.confの設定 vi vsftpd.conf

# anonymousユーザを無効
anonymous_enable=NO

# ローカルユーザを許可
local_enable=YES

# ASCIIモードを有効にします
ascii_upload_enable=YES
ascii_download_enable=YES

# chrootを可能にする
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list

# ユーザごとにログインディレクトリを変更する
# ホームディレクトリを変えるより柔軟
# userconf配下にユーザ名でファイルを作成してディレクトリ追記
user_config_dir=/etc/vsftpd/userconf

# ユーザリストをホワイトリストに設定
userlist_enable=YES
userlist_deny=NO

# chrootするための設定
allow_writeable_chroot=YES

# ログインユーザをバーチャルユーザとみなす
guest_enable=YES

# バーチャルユーザをapacheに設定
guest_username=apache

# バーチャルユーザーがファイルアップロードする事を許可する
anon_upload_enable=YES

# バーチャルユーザーが新規にディレクトリを作成する事を許可する
anon_mkdir_write_enable=YES

# バーチャルユーザーがファイル名の変更やファイル削除をする事を許可する
anon_other_write_enable=YES

# World Readableなファイルのみをダウンロードできるというオプションを無効にする
anon_world_readable_only=NO

# umask
anon_umask=022

# バーチャルユーザーをマッピングしたOSユーザーと同等の権限にする
virtual_use_local_privs=YES

# SSLを有効化
ssl_enable=YES
rsa_cert_file=/etc/pki/tls/certs/vsftpd.pem
ssl_sslv2=NO
ssl_sslv3=NO
ssl_tlsv1=YES

PAMの設定を変更

LinuxではPAM(Pluggable Authentication Modules)というのを使って、各アプリケーションで認証を行っている。
デフォルトではFTPもLinuxのユーザ認証になっているので、先ほど作成したDBで認証してもらうように設定を変更する。

vi /etc/pam.d/vsftpd
# もろもろ
# コメントアウト
# 64bit OS はlib64
auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd
account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd

##user_listの設定 接続を許可するユーザを1行づつ記入

##chroot_listの設定 制限無しでアクセスできるユーザを1行づつ記入

##ログインディレクトリを記入 user_config_dirで指定したディレクトリ配下にユーザ名でファイルを作成し、 下記の通りディレクトリを指定する。
local_root=/var/www/html

##参考 http://qiita.com/egnr-in-6matroom/items/38392a6509d4d0575927

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment