LoginSignup
0
0

Windows Server 2022 を sftpサーバとして利用する

Last updated at Posted at 2024-04-29

windows server 2022 を sftpサーバとして利用するための設定になります。

要件

・専用のユーザ(sftpuser)を利用して接続する。
・Administratorユーザは接続禁止とする。
・認証はパスワードとする(公開鍵を使用しない)。
・特定のディレクトリにchrootする。

構築フロー

・ユーザの作成
・ディレクトリの作成
・OpenSSHのインストール
・sshd_configの設定
・接続確認

1.ユーザの作成

sftp接続用ユーザを作成します。
グループ:sftpgroup
ユーザ:sftpuser

Windowsメニューを右クリックし、コンピュータの管理からローカルユーザとグループ - ユーザ および グループにて新規ユーザおよびグループを作成します。
image.png

2.ディレクトリの作成

sftpアクセス用のディレクトリを作成します。
ディレクトリ作成後、sftp用ユーザのフルコントロール権限を付与します。
ディレクトリ:D:\transport\sftproot\sftpsite
image.png

3.OpenSSHのインストール

OpenSSHのインストールを行います。
設定 - アプリ - アプリと機能を表示します。
image.png

オプション機能から機能の追加を表示します。
image.png

OpenSSHサーバーを選択し、インストールを選択します。
image.png

しばらくして、インストールが完了します。
image.png

インストールにはインターネットへの接続が必要になります。


続いてサービスの設定を行います。
サーバマネージャからツール - サービスを選択します。
image.png

OpenSSHサーバのプロパティを選択し、スタートアップの種類を自動に変更し、サービスの状態を開始します。
image.png

後述のsshd_configはサービスを一度起動しないと作成されません。

4.sshd_configの設定

sshd_configの設定変更をします。
以下ディレクトリにコンフィグファイルがあることを確認します。
場所:C:\ProgramData\ssh\sshd_config
image.png

sshd_configについて以下のように修正します。
ファイル修正後、OpenSSHサーバーサービスを再起動します。

sshd_config(一部)
(中略)
#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全文
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接続を実施します。
アクセスが拒否されることを確認しました。
image.png
image.png


sftpuserでsftp接続を確認します。
パスワード認証で接続できることを確認しました。
image.png
image.png

chroot設定しているので、接続先ディレクトリは / となっています。


ファイル転送も問題なく実施できました。
image.png
image.png

参考

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