ルートなしでポート80でPython 3 http.serverを実行する方法は?

ルートなしでポート80でPython 3 http.serverを実行する方法は?

Python 3を実行したいです。httpサーバー存在するポート80いいえ追加のソフトウェア(authbindなど)をインストールせずにrootとして実行します。私はアーチLinuxを使用しています。可能であれば、systemdを使用してこれを実行し、起動時に自動的に起動することをお勧めします。

私は次の簡単なラッパーを作成しました。

#!/bin/sh
cd /srv/http/mywebsite/
python -m http.server 80

以下のユニットファイルを使用して実行できます。

[Unit]
Description=Python 3 http.server

[Service]
Type=simple
ExecStart=/usr/local/bin/website_start.sh

[Install]
WantedBy=multi-user.target

これは「動作」ですが、安全ではありません。ユーザーとグループを追加すると、「許可拒否」エラーが発生します(ポート80にはroot権限が必要なため)。

[Unit]
Description=Python 3 http.server

[Service]
Type=simple
ExecStart=/usr/local/bin/website_start.sh
User=http
Group=http

[Install]
WantedBy=multi-user.target

エラーは次のとおりです。

Jun 23 00:58:57 myvps systemd[43060]: http_python_server.service: Failed to execute command: Permission denied

Jun 23 00:58:57 myvps systemd[43060]: http_python_server.service: Failed at step EXEC spawning /usr/local/bin/website_start.sh: Permission denied

error: connect_to website port 80: failed.

ベストアンサー1

サービスプロセスにポート<1024を使用する機能を付与できますが、他のroot権限を付与することはできません。

[Unit]
Description=Python 3 http.server

[Service]
Type=simple
ExecStart=/usr/local/bin/website_start.sh
User=http
Group=http
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

man 7 capabilitiesもっと知りたいなら読んでください。

この/sbin/getpcapsコマンドは、PID ごとにプロセスで使用可能な機能を照会するために使用できます。一般に、ルート所有プロセスには多くの機能のリストがありますが、非ルートプロセスにはまったく機能がありません。

おすすめ記事