semange と chcon の違いは、chcon は「一時的」であり、システムがファイル/ディレクトリに存在するコンテキストのラベルを変更すると失われます。semanage を使用すると、selinux コンテキストが永続的になります。
semanage を機能させるには、ファイルまたはディレクトリへのフル パスを指定する必要があります。これが semanage fcontext -a -t public_content_rw_t upload/ の理由です。 動作しませんが semanage fcontext -a -t public_content_rw_t "/var/ftp/upload(/.*)? します; restorecon はフル パスを必要としません。
これはデフォルトのコンテキストです
[example@unixlinux.online ftp]# ll -Zd upload/ drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 upload/
コンテキストを変更するためにこのコマンドを試しました
[example@unixlinux.online ftp]# semanage fcontext -a -t public_content_rw_t upload/ [example@unixlinux.online ftp]# ll -Zd upload/ drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 upload/
[example@unixlinux.online ftp]# restorecon -R -v upload [example@unixlinux.online ftp]# ll -Zd upload/ drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 upload/
動作していませんが、このコマンドはコンテキストを /etc/selinux/targeted/contexts/files/file_contexts.local に書き込みます ファイル
ここを参照
# This file is auto-generated by libsemanage # Do not edit directly. upload/ system_u:object_r:public_content_rw_t:s0
今、私はこのコマンドを試しました (Working Command )
[example@unixlinux.online ftp]# semanage fcontext -a -t public_content_rw_t "/var/ftp/upload(/.*)?"
[example@unixlinux.online ftp]# restorecon -R -v upload restorecon reset /var/ftp/upload context unconfined_u:object_r:public_content_t:s0->unconfined_u:object_r:public_content_rw_t:s0
Now context is changed. [example@unixlinux.online ftp]# ll -Zd upload/ drwxr-xr-x. root root unconfined_u:object_r:public_content_rw_t:s0 upload/
しかし、なぜそれが機能しているのか本当にわかりません。コマンドの違いを見てください。
man ftpd_selinux の man ページで答えを得ました
semanage fcontext -a -t public_content_rw_t "/var/ftpd/incoming(/.*)?"