• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

chcon command examples in Linux

by admin

The chcon command helps to change the SELinux context or TYPE of what will most often be a single or perhaps sometimes a few files that can be referenced easily together with some form of a wildcard. chcon along with semanage and restorecon can be used to fix an incorrect SELinux context. You can use chcon command in 2 ways to fix or change the SELinux context.

Manually way of specifying the correct context

In this way, we can use the -t option to change the context of the file. This method of modifying the context of the file is not recommended, as errors can happen while using it.

# chcon -t httpd_sys_content_t index.html

You can see in the ‘ls -Z’ output that the command was successful.

# ls -Z
-rw-rw-r--. apacheuser apacheuser unconfined_u:object_r:httpd_sys_content_t:s0 index.html

Reference file with proper context

The other way to use chcon is to reference a file that has the proper context.

# chcon --reference some_file.html index.html

So, I used the –reference option and specified the file that I wanted to use as a reference. The file that I wanted to change is listed at the end of the command.

Examples of using chcon command

1. To change a type of a web directory

# chcon -R -t httpd_sys_content_t /web/

2. To change a security context by using the reference file:

# chcon --reference=/tmp/file2 /tmp/file2

3. To set security context on files recursively:

# chcon -R httpd_sys_content_t /web/

4. To change files user security context:

# chcon -u mike_u /file

5. To change files role security context:

# chcon -u object_r /file

6. To change files type security context:

# chcon -u admin_home_t /file

7. To change files level security context:

# chcon -u s0 /file

Filed Under: CentOS/RHEL, Linux

Some more articles you might also be interested in …

  1. How to analyze basic system performance using – vmstat, sar, iostat and mpstat
  2. How to re-create the yum cache (force a fetch of the cache data) from enabled repositories in CentOS/RHEL
  3. Error: Can’t open display: hostname:x.y when running xclock
  4. User Environment Variables With “su” and “sudo” in Linux
  5. Beginners Guide to Tuning Profiles in CentOS/RHEL
  6. How to Migrate virtual machines from one host to another in the Red Hat Virtualization environment
  7. RHV – How to shutdown a VM from AdminPortal
  8. How to Manage Virtual Machines from the CentOS/RHEL 8 Web Console-Cockpit
  9. “dracut-initqueue[286]: Warning: dracut initqueue timeout – starting timeout scripts” – CentOS/RHEL 7 booting issue
  10. How to use Magic SysRq tool in CentOS / RHEL

You May Also Like

Primary Sidebar

Recent Posts

  • fprintd-delete Command Examples in Linux
  • fprintd-delete: command not found
  • foreman: command not found
  • foreman Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright