SFTP用ユーザのchroot環境の構築

セキュリティ向上のため、SFTP用ユーザが上位ディレクトリにアクセスできないchroot環境を構築します。
ここでは、「www_user」というユーザが「/var/www」より上にアクセスできない環境を構築してみます。
※ユーザ名、所属グループは任意に指定して下さい。

1. ユーザの作成

ここでは「www_user」を作成します。
# groupadd www_user
# useradd -g www_user -G apache -d /var/www -s /sbin/nologin www_user
# passwd www_user

2. ディレクトリの設定

(1) /var/wwwのオーナーの設定
上位ディレクトリのオーナーは必ず「root」でなければなりません。
# chown root:root /var/www

(2) /var/www/htmlの設定
このオーナーを「www_user」とします。
# cd /var/www
# chown -R www_user:apache html

このディレクトリのパーミッションを「775」にします。(apacheユーザでも書き込めるように)
# cd /var/www
# chmod -R 775 html

3. sshdの設定

「/etc/ssh/sshd_config」の設定を行います。
# vi /etc/ssh/sshd_config

Subsystemの行を以下のように書き換えます。
#Subsystem      sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp

最終行に以下を追加します。
Match User www_user
        PasswordAuthentication yes
        ChrootDirectory /var/www
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp -u 002        

また、例えばid01、id02というようにユーザ名の一部を可変にしたい場合は、以下のように指定します。
Match User id*
        PasswordAuthentication yes
        ChrootDirectory /var/www/%u
        X11Forwarding no
        AllowTcpForwarding no
        ForceCommand internal-sftp -u 002        

設定が文法的に正しいかチェックします。
# /usr/sbin/sshd -t
何も表示されない場合にはエラーがないので、sshdサービスを再起動します。
# systemctl restart sshd.service

4. トラブルシューティング

(1) 「sshd: ssh-rsa algorithm is disabled」エラーが出る場合
「sshd -t」コマンドを実行すると「main: sshd: ssh-rsa algorithm is disabled」とエラーが出る場合は以下のように対処します。
# vi /etc/ssh/sshd_config
【旧】
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
【新】                                                  ↓
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
※「ssh_host_ed25519_key」のみを有効化します。
sshdを再起動します。
# systemctl restart sshd.service

(2) アクセスするアプリが古くて「ssh-rsa」を使用したい場合
「ssh-rsa」を有効化します。
# vi /etc/ssh/sshd_config
以下の記述を追加します。
HostKeyAlgorithms=+ssh-rsa
PubkeyAcceptedAlgorithms=+ssh-rsa
sshdを再起動します。
# systemctl restart sshd.service
※「sshd -t」コマンドを実行すると「main: sshd: ssh-rsa algorithm is disabled」エラーが表示される場合がありますが無視します。

(3) ユーザ追加時に「sss_cache」エラーが発生する場合
# useradd (userid)
ユーザを追加しようとした場合以下のエラーが発生することがあります。
[sss_cache] [sysdb_domain_cache_connect] (0x0010): DB version too old [0.23], expected [0.24] for domain implicit_files!
Higher version of database is expected!
In order to upgrade the database, you must run SSSD.
Removing cache files in /var/lib/sss/db should fix the issue, but note that removing cache files will also remove all of your cached credentials.
Could not open available domains
[sss_cache] [sysdb_domain_cache_connect] (0x0010): DB version too old [0.23], expected [0.24] for domain implicit_files!
Higher version of database is expected!
In order to upgrade the database, you must run SSSD.
Removing cache files in /var/lib/sss/db should fix the issue, but note that removing cache files will also remove all of your cached credentials.
Could not open available domains

その場合は「/var/lib/sss/db」のファイルを削除します。
# cd /var/lib/sss/db/
# systemctl stop sssd.service
# rm -f *
# systemctl start sssd.service



最終更新:2025年06月27日 08:37