Disabling Transparent Huge Pages (THP) permanently on Ubuntu 22.04 requires editing system configuration files to apply the change at boot time. There are a few methods to achieve this, but one common approach is to use rc.local
or a custom systemd service since Ubuntu may not have rc.local
enabled by default in newer versions. Here, I’ll show you how to create a systemd service to disable THP.
Step 1: Create a systemd Service File
- Use your favorite text editor to create a new systemd service file. Here, we’ll use
vi
:
1sudo vi /etc/systemd/system/disable-thp.service - Add the following content to the file:
12345678910[Unit]Description=Disable Transparent Huge Pages[Service]Type=oneshotExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled'ExecStart=/bin/sh -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag'[Install]WantedBy=multi-user.target - Save and close the file.
Step 2: Reload Systemd and Enable the Service
After creating the service file, you need to reload the systemd manager configuration, enable the service to start at boot, and then start the service immediately to apply the change without rebooting.
Execute the following commands in the terminal:
1 2 3 |
sudo systemctl daemon-reload sudo systemctl enable disable-thp.service sudo systemctl start disable-thp.service |
Step 3: Verify the Changes
To verify that Transparent Huge Pages have been disabled, you can use the cat
command to check the current settings:
1 2 |
cat /sys/kernel/mm/transparent_hugepage/enabled cat /sys/kernel/mm/transparent_hugepage/defrag |
Both commands should output never
, indicating that Transparent Huge Pages are disabled.
Conclusion
By following these steps, you’ve created a systemd service to disable Transparent Huge Pages permanently on Ubuntu 22.04. This change will persist across reboots, ensuring that THP is disabled each time the system starts. This method is preferred for systems where THP may interfere with the performance of certain applications, especially databases like MongoDB, Redis, etc., that recommend disabling THP for better performance and efficiency.