$linuxjunkies
>

restorecon(8)

Restore SELinux security context for files and directories.

UbuntuDebianFedoraArch

Synopsis

restorecon [OPTION]... FILE...

Description

restorecon is used to restore the default SELinux security contexts (labels) for files and directories. It reads the SELinux policy configuration to determine what the security context should be, then applies those contexts to the specified files.

This command is useful after restoring files from backup, copying files between systems, or when SELinux security contexts become incorrect or missing. It works recursively on directories and can process entire filesystem trees.

Common options

FlagWhat it does
-rRecursive; restore contexts for all files and subdirectories
-RRecursive; follow all symbolic links (like -r but follows symlinks)
-vVerbose; print files as they are processed
-nNo changes; show what would be relabeled without making changes
-e DIRECTORYExclude directory from relabeling (can be used multiple times)
-iIgnore files that do not exist
-pShow progress; display the number of files processed
-FForce relabeling; treat all files as unlabeled
-cChange only files whose context differs from the default
-m FILEUse an alternative SELinux policy file instead of default

Examples

Restore the default SELinux context for a single file

restorecon /home/user/file.txt

Recursively restore contexts for all files in a web directory

restorecon -r /var/www/html

Recursively restore home directory contexts with verbose output showing each file

restorecon -v -r /home

Preview what would be relabeled in /etc without making actual changes

restorecon -n -r /etc

Recursively restore /var/log but exclude the archive subdirectory

restorecon -r -e /var/log/archive /var/log

Recursively restore the entire filesystem with progress display

restorecon -R -p /

Force relabel a file, treating it as if it has no context

restorecon -F /etc/shadow

Related commands