sftp限定ユーザの公開

ファイルダウンロードのみに限定したsftpを公開したい。
このサーバの存在も隠蔽するために、以前の記事で、新しいIPアドレスを追加した。
この新しく追加したIPアドレスで、sftpを公開する。

前提

  • CentOS7を使用する。
  • ユーザにはsshを許可せず、sftpによる接続のみ許可する。
  • sftpユーザによるアクセス時の認証は公開鍵のみとして、パスワード認証は使用しない。
  • sftpで接続後の操作は次のように限定する。
    ダウンロードのみ許可する。(アップロードはできない。)
    システムや別ユーザの存在等の情報がわからないよう、ユーザのホームディレクトリ配下しか参照できないようにする。(chroot)
  • 信頼できるユーザを想定しているので操作ログを取得する予定はないが、取得する場合は下記が参考になるかもしれない。
    How to log internal-sftp chroot jailed users
  • 利用者には、別途、接続先IPアドレス、秘密鍵を通知する。
  • 今後、同様のユーザを追加する可能性があるので、ユーザ単位ではなく、グループ単位で上記を実現できるようにする。このグループはsftpとする。
  • chroot設定の弊害で、対象ディレクトリへのファイル書き込みは、rootしかできない…

手順

サーバ設定

sshdの設定ファイルを次のように編集する。
公開鍵認証は既定で有効なので明示的な設定は不要。
sftpグループに所属するユーザに対して、ユーザのホームディレクトリでchrootし、利用可能なコマンドをinternal-sftpで限定する。

[code gutter=”false” highlight=”7,15-20″]# groupadd sftp
# cd /etc/ssh/
# vi sshd_config

# To disable tunneled clear text passwords, change to no here!
PermitEmptyPasswords no
PasswordAuthentication no

# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
Match Group sftp
ChrootDirectory %h
X11Forwarding no
AllowTcpForwarding no
PermitTTY no
ForceCommand internal-sftp

# systemctl restart sshd.service
[/code]

なお、使用できるそれ以外のトークはman sshd_configで確認できる。
[code gutter=”false”]TOKENS
Arguments to some keywords can make use of tokens, which are expanded at runtime:

%% A literal ‘%’.
%F The fingerprint of the CA key.
%f The fingerprint of the key or certificate.
%h The home directory of the user.
%i The key ID in the certificate.
%K The base64-encoded CA key.
%k The base64-encoded key or certificate for authentication.
%s The serial number of the certificate.
%T The type of the CA key.
%t The key or certificate type.
%u The username.
[/code]

sftpユーザの追加

次のようにユーザを作成し、鍵を生成する。
作成した秘密鍵(/home/xxxuser/.ssh/id_rsa)を別途ユーザに通知する。
chrootを行うにあたり、親ディレクトリも含めディレクトリのオーナはrootで755が必要とのこと。man sshd_configにて、”At session startup sshd(8) checks that all compo‐nents of the pathname are root-owned directories which are not writable by any other user or group.”とある。)
[code gutter=”false”]# useradd -G sftp -s /sbin/nologin xxxuser
# su – xxxuser

$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xxxuser/.ssh/id_rsa):
Created directory ‘/home/xxxuser/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/xxxuser/.ssh/id_rsa.
Your public key has been saved in /home/xxxuser/.ssh/id_rsa.pub.

$ cd .ssh/
$ cat id_rsa.pub >> authorized_keys
$ chmod 600 ./authorized_keys
$ exit

# cd /home
# chown root.root ./xxxuser
# chmod 755 ./xxxuser
# ls -l

drwxr-xr-x 5 root root 4096 Jul 4 16:03 xxxuser

[/code]

なお、権限が正しく設定されていないと、次のエラーが出力される場合がある。
[code gutter=”false”]# tail -f /var/log/messages

… sshd[4315]: fatal: bad ownership or modes for chroot directory "/home/xxxuser" [postauth]…
[/code]

接続時のクライアント設定

ユーザはWindowsを使っている想定なので、Windowsのsftpクライアントを使用する。
ここでは、WinSCPで接続する想定で手順を記載する。

下記からwinscpをダウンロードしてインストールする。
FTPクライアント – 窓の杜ライブラリ

設定の手順は次の通り。

  1. WinScpを起動し、ログインウインドウの設定をクリックする。
  2. 表示された設定画面の[認証]をクリックし、[秘密鍵]に上記の秘密鍵のファイルid_rsaを指定する。(「すべてのファイル」を選択しないと表示されないことに注意。)
  3. 「秘密鍵OpenSSHをPuTTY形式に変換しますか?」と聞かれるので、[OK]をクリックする。
  4. 接続先やユーザ名を入力する。再接続することも考慮し、[保存]をクリックする。
  5. [ログイン]をクリックし、sftp接続できることや、ファイルをダウンロードできることを確認する。