LoginSignup
7
17

More than 3 years have passed since last update.

OpenSSHでSFTPサーバを構築する

Posted at

SFTPサーバの用意

/etc/ssh/sshd_config を適切に設定すればSFTPサーバの環境を用意することができる。

SCPとSFTPの違い

参考:SCPとSFTPの違い | Skyarch Broadcasting
cpとftpの違いとして認識するとよい。opensshのデフォルトポートを利用するなら22番で両方ともsshの設定変更で実現可能。
今回はSFTPを用意する。

環境

  • Linux
    • CentOS Linux release 7.7.1908 (Core) ... 作業時の最新を用意してみた
  • Client
    • FTPクライアントソフトを用いることを想定
    • Windows:WinSCP, Mac:Cyberduck など
  • 接続ユーザ名/グループ
    • test001/sftp ... パスワード認証想定
    • test002/sftp ... 鍵認証想定
    • グループ名は共通。グループでの制御でもよいが、今回はユーザ単位とする。

今回はクライアントの設定は省略する。なお、鍵認証とパスワード認証の両方を用意してみる。

SFTPの設定

/etc/ssh/sshd_config
AcceptEnv LANG LC_*
ChallengeResponseAuthentication no
PrintMotd no
PubkeyAuthentication yes
Subsystem sftp /usr/libexec/openssh/sftp-server
UsePAM yes
X11Forwarding no

Match user test001
    ChrootDirectory /home/test001/
    ForceCommand internal-sftp
    PasswordAuthentication yes

Match user test002
    AuthorizedKeysFile /home/test002/.ssh/authorized_keys
    ChrootDirectory /home/test002/
    ForceCommand internal-sftp
    PasswordAuthentication no

設定としてはこのように用意。
test001はパスワード認証とした場合、 passwd test001 等であらかじめユーザを用意したのにちパスワードを用意。
test002は鍵認証のため、秘密鍵+公開鍵の組み合わせを用意して、 公開鍵を /home/test002/.ssh/authorized_keys に保存すること。sshと考え方は同じ。

このとき、ログインシェルはありでよいが、sshをさせたくないので ForceCommand internal-sftp
としている。

% ssh test001@<該当IPアドレス>
sign_and_send_pubkey: no mutual signature supported
test001@<該当IPアドレス>'s password:
This service allows sftp connections only.
Connection to <該当IPアドレス> closed.

これにより、上記のとおりでsshに制限がかかる。
sign_and_send_pubkey: no mutual signature supported との表記。

トップディレクトリを/にしたい場合

権限がゆるいと /home/test001 よりトップにさかのぼれてしまう。
/home/test001chown root:root として、ディレクトリ内に該当のユーザがアクセス可能なディレクトリを別で用意してやりとりするとよい。

参考サイト

SFTPで特定のディレクトリのみ利用可能とする - Qiita

7
17
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
7
17