For AI agents: a documentation index is available at https://www.mongodb.com/zh-cn/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Configure the MongoDB SELinux Policy

Security Enhanced Linux (SELinux) is a Linux kernel security module that enforces mandatory access control policies. This allows you to confine a process to the files, directories, and ports that its policy explicitly permits. This can block access even when the standard file permissions otherwise grant access.

MongoDB provides an SELinux policy for use with mongod. This is available for MongoDB Enterprise and MongoDB Community on systems that use:

  • Red Hat Enterprise Linux or related distributions

  • MongoDB installed with the package manager

If your MongoDB deployment uses a custom value for any of the following settings, you cannot use the MongoDB SELinux policy:

The MongoDB SELinux policy only covers mongod. If you want to use SELinux on a system that also uses mongos, mongosh, or mongocryptd, you need to configure a custom policy.

You must run the following command from the directory into which the SELinux policy was previously cloned before you can downgrade to an earlier MongoDB version:

sudo make uninstall

You can customize the MongoDB SELinux policy with the semanage command. For example, to add port 30000 to mongod_port_t, run the following command:

sudo semanage port -at mongod_port_t -p tcp 30000

This configures SELinux to allow TCP connections on both the standard MongoDB ports and port 30000.

Ensure that you have the prerequisite packages installed on your system:

sudo dnf install git make checkpolicy policycoreutils \
selinux-policy-devel

SELinux enforces policies by default, which means it blocks processes that violate its configured restrictions. In development environments, users sometimes disable SELinux or configure it to log but permit all operations. Before you begin, check that SELinux is enforcing its policies.

To check which mode SELinux is in, run the following command:

getenforce
Enforcing

If the command returns Permissive, you can reset it to enforcing with the setenforce command:

sudo setenforce 1

To make the change persistent, set the SELINUX variable in the /etc/selinux/config file:

SELINUX=enforcing
1

To download the MongoDB SELinux policy, use Git to clone the repository:

git clone https://github.com/mongodb/mongodb-selinux
2

To build the policy, change into the mongodb-selinux directory and run make:

cd mongodb-selinux
make
3

To apply the policy, run the install target with sudo:

sudo make install

SELinux now uses the MongoDB policy, allowing you to run mongod while SELinux is in enforcement mode.