Openssh - ルートが sftp と ssh の両方にアクセスできるようにする

Openssh - ルートが sftp と ssh の両方にアクセスできるようにする

サーバー上の sftp を root ユーザーがアクセスできるように設定したいと思います。

の設定を/etc/ssh/sshd_config次のように変更しました:

PermitRootLogin yes
AuthorizedKeysFile  .ssh/authorized_keys
PermitEmptyPasswords yes
ChallengeResponseAuthentication no
Compression no
ClientAliveInterval 15
ClientAliveCountMax 4
#Other options are commented
[...]
Subsystem sftp /usr/libexec/sftp-server

Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp

動作し、sftp でサーバーに接続できますが、ssh で接続できなくなりました。次のメッセージが表示されました:

This service allows sftp connections only.

同じことがMatch group root

ssh も維持しながら、root を許可するように sftp を構成するにはどうすればよいでしょうか?


アップデート:

ForceCommand 内部 sftp オプションのコメント化は引き続き機能しますが、sftp で接続できません。sshd_config:
Subsystem   sftp    /usr/libexec/sftp-server
Match user root
ChrootDirectory /
X11Forwarding no
AllowTcpForwarding no
#ForceCommand internal-sftp

エラー:

user@notebook:~$ sftp root@my_device
Warning: the ECDSA host key for 'my_device' differs from the key for the IP address 'xx.xx.xx.xx'
Offending key for IP in /home/myUser/.ssh/known_hosts:78
Matching host key in /home/myUer/.ssh/known_hosts:93
Are you sure you want to continue connecting (yes/no)? yes
Connection closed.  
Connection closed

サーバー側:

root@my_device:~# cat /var/log/auth.log | grep ssh
2024-01-17T19:25:21.659884+00:00 my_device sshd[30592]: Accepted none for root from xx.xx.xx.xx port 38024 ssh2
2024-01-17T19:25:21.881328+00:00 my_device sshd[30592]: Received disconnect from xx.xx.xx.xx port 38024:11: disconnected by user
2024-01-17T19:25:21.881997+00:00 my_device sshd[30592]: Disconnected from user root xx.xx.xx.xx port 38024

Subsystem sftp /usr/libexec/sftp-server注: sftp で接続しようとしたときに別のエラーが発生したため、 削除した場合:subsystem request failed on channel 0

答え1

ディレクティブはForceCommandroot のみが sftp を使用できるようにするため、その行をコメント アウトしてみてください。

答え2

OpenSSH のマニュアルページsshd_config(5)これを次のように説明しています。

ForceCommand

で指定されたコマンドの実行を強制しますForceCommand。クライアントによって提供されたコマンドや~/.ssh/rc存在する場合は無視します。 コマンドは、-cオプションを指定したユーザーのログイン シェルを使用して呼び出されます。 これは、シェル、コマンド、またはサブシステムの実行に適用されます。 ブロック内で最も役立ちますMatch。 クライアントによって最初に提供されたコマンドは、SSH_ORIGINAL_COMMAND環境変数で使用できます。 のコマンドを指定すると、internal-sftpで使用するときにサポート ファイルを必要としないインプロセス SFTP サーバーの使用が強制されますChrootDirectory。 デフォルトは ですnone

ここで述べたように、ForceCommand internal-sftpSFTP のみが許可されます。

さらに、PermitRootLogin yesルートのパスワード認証を有効にするため危険であり、ChrootDirectory /制限的なものではないchroot 刑務所まったくありません。

  • 公開鍵認証を使用するか、ログインを完全にroot無効にするroot
  • sudo直接rootログインの代わりに使用する
  • sudo権限を持つすべてのユーザーに公開鍵認証を使用する
  • パスワードを使用しますsudo(前のものと組み合わせると、実質的には昇格された権限の MFA になります)。

Server Fault はビジネス環境に関連する質問のためのものであり、現在の構成はこのコンテキストでは適切なプラクティスを示していないため、私はこれらを常に思い出させます。

答え3

SSH 構成をチェックし、安全な SSH 構成を作成するこの Ansible プレイブックを実行してみることができます。 https://github.com/dev-sec/ansible-collection-hardening/tree/master/roles/ssh_hardening

これを実行した後は、sshd_configでrootログインを許可するだけで、sshとsftpの両方が機能するはずです。

答え4

さて、ここでの問題は、/usr/libexec/sftp-serverサーバー側に存在しないことでした。新しい Yocto ビルドでイメージに追加したところ、デフォルトの設定を使用して root で ssh および sftp で接続できるようになりました。

# override default of no subsystems
Subsystem   sftp    /usr/libexec/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

関連情報