LoginSignup
41
56

More than 5 years have passed since last update.

接続できるディレクトリを制限して複数のSFTPユーザを作る

Last updated at Posted at 2016-08-04

《課題》

やりたいこと

  • SFTPユーザ毎に接続できるディレクトリを制限
  • 公開鍵認証
  • chrootで必要以上にディレクトリは見せない
  • 共有するディレクトリのパーミッションは755、ファイルのパーミッションは644

sftpグループに所属するユーザ
/var/www/vhosts/以下はすべてアクセス可能

sftp-limitグループに所属するユーザ
指定したディレクトリ以下のみアクセス可能
※ただし/etc/ssh/sshd_configは初期の設定後は変更をしない

今回の設定

  • ディレクトリ構成とアクセス権
/var/www/vhosts/           : 755 root root      ← sftpは以下にアクセス可
`-- example.com            : 755 sftp_user sftp
    `-- html               : 755 sftp_user sftp
        |-- index.html     : 644 sftp_user sftp
        `-- limit          : 755 sftp_user sftp ← sftp-limitは以下にアクセス可
            `-- index.html : 644 sftp_user sftp
  • /var/www/vhosts/をルートディレクトリに

《前提条件》

今回はAWSでAmazon Linux環境で構築

# cat /etc/system-release
Amazon Linux AMI release 2016.03

# ssh -V
OpenSSH_6.6.1p1, OpenSSL 1.0.1k-fips 8 Jan 2015

《手順》

▼OpenSSHの初期設定の確認

# vi /etc/ssh/sshd_config
/etc/ssh/sshd_config
#Port 22#PubkeyAuthentication yes

︙

AuthorizedKeysFile .ssh/authorized_keys

︙

PasswordAuthentication no

︙

#Port 22
ポートは22番

#PubkeyAuthentication yes
公開鍵認証の許可

AuthorizedKeysFile .ssh/authorized_keys
公開鍵パス(ホームディレクトリ以下のパス)

PasswordAuthentication no
パスワード認証は不可

※もともと#でコメントアウトしているものはデフォルト値

▼sftpグループの設定

グループの作成とOpenSSHの設定

1. グループの追加

# groupadd sftp

2. OpenSSHの設定

# vi /etc/ssh/sshd_config
/etc/ssh/sshd_config
︙

Match group sftp
       ChrootDirectory /var/www/vhosts
       ForceCommand internal-sftp

Match group sftp
sftpグループに所属するユーザのみ有効の設定
ChrootDirectory /var/www/vhosts
接続したときに"/var/www/vhosts"をルートディレクトリにする
(/var/www/vhostsより上はアクセスできない)
※指定するディレクトリは所有者root:root、パーミッション755にする
ForceCommand internal-sftp
sftpコマンドのみに制限

3. OpenSSHの設定更新

# sshd -t
# service sshd reload
Reloading sshd:                                            [  OK  ]

# sshd -t`
構文チェック。何も表示されなければOK

ユーザの追加

1. ユーザ追加

# useradd -g sftp sftp_user

今回は共有するファイル、ディレクトリの所有者はsftp_userとする

-g グループ名
プライマリグループを指定

2. 公開鍵の登録

(1) 作成したユーザに切り替え
# su - sftp_user
(2) .sshディレクトリの作成
$ cd ~
$ mkdir -m 700 .ssh
(3) authorized_keysに公開鍵を登録
$ vi .ssh/authorized_keys
.ssh/authorized_keys
公開鍵の内容
$ chmod 600 .ssh/authorized_keys

※キーペアはssh-keygenコマンドなどで作成

(4) 元のユーザに戻る
$ exit
※ディレクトリ、ファイルの所有者、パーミッションについて
ChrootDirectory
/var/www/vhosts             : 755 root root
鍵認証
/home/
`-- sftp_user               : 700 sftp_user sftp
    `-- .ssh                : 700 sftp_user sftp
        `-- authorized_keys : 600 sftp_user sftp

接続の確認

・別の端末からsftpコマンドで接続

$ sftp -i 秘密鍵 ユーザ名@ホスト名
sftp>

※秘密鍵のパーミッションは600

・リモートのディレクトリとファイルを確認

sftp> pwd
Remote working directory: /
sftp> ls
example.com

/var/www/vhostsと同じものが表示されている

sftp> cd /example.com/html/
sftp> ls
index.html   limit

/example.com/html/のindex.htmlは見えている

sftp> cd /example.com/html/limit/
sftp> ls
index.html

/example.com/html/limit/のindex.htmlは見えている

・sftpコマンドの終了

sftp> quit

・別の端末からsshコマンドで接続

# ssh -i 秘密鍵 ユーザ名@ホスト名
Could not chdir to home directory /home/sftp_user: No such file or directory
This service allows sftp connections only.
Connection to xxx.xxx.xxx.xxx closed.

接続できない

▼sftp-limitグループの設定

グループの作成とOpenSSHの設定

1. グループの追加

# groupadd sftp-limit

2. OpenSSHの設定

# vi /etc/ssh/sshd_config
/etc/ssh/sshd_config
︙

Match group sftp-limit
       ChrootDirectory %h/www/vhosts
       ForceCommand internal-sftp

Match group sftp
       ChrootDirectory /var/www/vhosts
       ForceCommand internal-sftp

※sftpグループの設定より上に書く

Match group sftp-limit
sftp-limitグループに所属するユーザのみ有効の設定
ChrootDirectory %h/www/vhosts
接続したときに"ホームディレクトリ/www/vhosts"をルートディレクトリにする
※指定するディレクトリは所有者root:root、パーミッション755にする
ForceCommand internal-sftp
sftpコマンドのみに制限

3. OpenSSHの設定更新

# sshd -t
# service sshd reload
Reloading sshd:                                            [  OK  ]

# sshd -t`
構文チェック。何も表示されなければOK

ユーザの追加

1. sftp_userユーザの情報を確認

# id sftp_user
uid=501(sftp_user) gid=501(sftp) groups=501(sftp)

2. ユーザ追加

# useradd -o -u 501 -g sftp -G sftp-limit sftp_user_limit

sftp_userと同じuidをもったユーザとして作成することで、共有されたディレクトリ、ファイルにグループので書き込み権限を与えなくてもいいようにする
-o
他ユーザと同一uidを持ったユーザの作成
-u 501
uidの指定(sftp_userと同じもの)
-g グループ名
プライマリグループを指定
-G グループ名
セカンダリグループを指定

3. 公開鍵の登録

(1) 作成したユーザに切り替え
# su - sftp_user_limit
(2) .sshディレクトリの作成
$ cd ~
$ mkdir -m 700 .ssh
(3) authorized_keysに公開鍵を登録
$ vi .ssh/authorized_keys
.ssh/authorized_keys
公開鍵の内容
$ chmod 600 .ssh/authorized_keys

※キーペアはssh-keygenコマンドなどで作成

(4) 元のユーザに戻る
$ exit

4. 共有ディレクトリの設定

(1) ディレクトリを作成
# cd /home/sftp_user_limit
# mkdir -p www/vhosts/example.com/html/limit
(2) 共有するディレクトリをマウント
  • 一時的なマウント
# mount --bind /var/www/vhosts/example.com/html/limit/ /home/sftp_user_limit/www/vhosts/example.com/html/limit/
  • 恒久的なマウント
# vi /etc/fstab
/etc/fstab
︙
/var/www/vhosts/example.com/html/limit/ /home/sftp_user_limit/www/vhosts/example.com/html/limit/ none bind 0 0
(3) 所有者とパーミッションの調整
# chown root:root /home/sftp_user_limit
# chmod 701 /home/sftp_user_limit

※ChrootDirectoryは指定したディレクトリまでの所有者が全部rootの必要があるのでので、ホームディレクトリをrootに変更する。ホームディレクトリをrootに変更すると次は鍵認証ができなくなるので、パーミションを701に変更ことで鍵認証を可能にする。

※ディレクトリ、ファイルのパーミッションについて
ChrootDirectory
/home/
`-- sftp_user_limit         : 701 root root
    |-- .ssh                : 700 sftp_user sftp
    |   `-- authorized_keys : 600 sftp_user sftp
    `-- www                 : 705 root root
        `-- vhosts          : 705 root root

接続の確認

・別の端末からsftpコマンドで接続

$ sftp -i 秘密鍵 ユーザ名@ホスト名
sftp>

※秘密鍵のパーミッションは600

・リモートのディレクトリとファイルを確認

sftp> pwd
Remote working directory: /
sftp> ls
example.com

/var/www/vhostsと同じものが表示されている

sftp> cd /example.com/html/
sftp> ls
limit

/example.com/html/のindex.htmlは見えない

sftp> cd /example.com/html/limit/
sftp> ls
index.html

/example.com/html/limit/のindex.htmlは見えている

・sftpコマンドの終了

sftp> quit

・別の端末からsshコマンドで接続

# ssh -i 秘密鍵 ユーザ名@ホスト名
Could not chdir to home directory /home/sftp_user_limit: No such file or directory
This service allows sftp connections only.
Connection to xxx.xxx.xxx.xxx closed.

接続できない

41
56
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
41
56