windows server 2022 を sftpサーバとして利用するための設定になります。
要件
・専用のユーザ(sftpuser)を利用して接続する。
・Administratorユーザは接続禁止とする。
・認証はパスワードとする(公開鍵を使用しない)。
・特定のディレクトリにchrootする。
構築フロー
・ユーザの作成
・ディレクトリの作成
・OpenSSHのインストール
・sshd_configの設定
・接続確認
1.ユーザの作成
sftp接続用ユーザを作成します。
グループ:sftpgroup
ユーザ:sftpuser
Windowsメニューを右クリックし、コンピュータの管理
からローカルユーザとグループ
- ユーザ
および グループ
にて新規ユーザおよびグループを作成します。
2.ディレクトリの作成
sftpアクセス用のディレクトリを作成します。
ディレクトリ作成後、sftp用ユーザのフルコントロール権限を付与します。
ディレクトリ:D:\transport\sftproot\sftpsite
3.OpenSSHのインストール
OpenSSHのインストールを行います。
設定
- アプリ
- アプリと機能
を表示します。
インストールにはインターネットへの接続が必要になります。
続いてサービスの設定を行います。
サーバマネージャ
からツール
- サービス
を選択します。
OpenSSHサーバ
のプロパティを選択し、スタートアップの種類を自動
に変更し、サービスの状態を開始
します。
後述のsshd_config
はサービスを一度起動しないと作成されません。
4.sshd_configの設定
sshd_configの設定変更をします。
以下ディレクトリにコンフィグファイルがあることを確認します。
場所:C:\ProgramData\ssh\sshd_config
sshd_configについて以下のように修正します。
ファイル修正後、OpenSSHサーバー
サービスを再起動します。
(中略)
#PermitRootLogin prohibit-password
PermitRootLogin no ### おまじない ###
(中略)
#PubkeyAuthentication yes
PubkeyAuthentication no ### パスワード認証のみ有効化 ###
(中略)
#Match Group administrators ### 無効化 ###
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ### 無効化 ###
DenyGroups administrators ### Administratorsグループの接続無効化 ###
AllowGroups sftpgroup ### sftpgroupグループの接続有効化 ###
Match User sftpuser ### sftpuser向け設定 ###
ChrootDirectory D:\transport\sftproot\sftpsite ### sftpuserのchroot設定 ###
sshd_config全文
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
PubkeyAuthentication no
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
# GSSAPI options
#GSSAPIAuthentication no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp sftp-server.exe
# Example of overriding settings on a per-user basis
#Match User anoncvs
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
DenyGroups administrators
AllowGroups sftpgroup
Match User sftpuser
ChrootDirectory D:\transport\sftproot\sftpsite
5.接続確認
Administratorユーザでsftp接続を実施します。
アクセスが拒否されることを確認しました。
sftpuserでsftp接続を確認します。
パスワード認証で接続できることを確認しました。
chroot設定しているので、接続先ディレクトリは /
となっています。
参考