I am using one of those cloud VPS providers that provide most of their storage on the /data disk, so my webroot is at /data/www instead of /var/www. And there is a symlink so that Apache works fine with default config.

I think the goal is to copy all the rules under /var/www and create the same rules except /var/www is changed to /data/www. Is there a way to do it quickly?

[~]$ sudo semanage fcontext -l | grep -i /var/www
/var/www(/.*)?                                     all files          system_u:object_r:httpd_sys_content_t:s0
/var/www(/.*)?/logs(/.*)?                          all files          system_u:object_r:httpd_log_t:s0
/var/www/[^/]*/cgi-bin(/.*)?                       all files          system_u:object_r:httpd_sys_script_exec_t:s0
/var/www/apcupsd/multimon\.cgi                     regular file       system_u:object_r:apcupsd_cgi_script_exec_t:s0
... many more ...

I only know how to do them one by one, by giving:

sudo semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?"
sudo restorecon -R -v /data/www

This takes care of the first rule. Is there a way to do it fast?


Are you looking for substitute by chance?  From man semanage-fcontext:


    -e EQUAL, --equal EQUAL
      Substitute target path with sourcepath when generating default label.  This is used with fcontext.  Requires source and target path arguments.  The context labeling for the target subtree is made equivalent to that defined for the source.

    Substitute /home1 with /home when setting file context

    # semanage fcontext -a -e /home /home1
    # restorecon -R -v /home1


mkdir -p /data/www2/html
touch /data/www2/html/foo
semanage fcontext -a -e /var/www /data/www2
restorecon -R /data/www2
ls -lhZ /data/www2/html/
-rw-r--r--. 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 0 15. Jul 15:31 foo
  • It works, thank you! This is exactly what is needed for the case of transplanting a directory to a data disk. – gus Jul 16 '20 at 4:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.