Changing Limits for Services with CentOS 7 / RHEL 7 / Systemd 7

Hitting too many open file limit for apache? In the previous OS versions, changing limits like open file number would be set in

/etc/security/limits.conf

or directly inside the start up script. In some ways the new method brings better organization to the limits, but its change is very confusing to people who were expecting the same. I myself was befuddled with the open file limit with the web server saying “Too many open files” and despite changing limits in the old way (which still exists), it just wasn’t solving it.

The limits are set according to specification set here:

/usr/lib/systemd/system/[name of service].service
/usr/lib/systemd/system/httpd.service   // apache example
/usr/lib/systemd/system/mariadb.service   // mariadb example

But it’s not recommended that you edit that. It’s because that’ll be over written with package update. What you should edit is:

/etc/systemd/system/[name of service].service.d/limits.conf

Often the folder for it won’t exist either. So you’ll have to make the folder first like (with httpd example):

# mkdir -p /etc/systemd/system/httpd.service.d/

Now create/edit/append a file so that the contents would be like below.

# cat /etc/systemd/system/httpd.service.d/limits.conf
[Service]
LimitNOFILE=20000

Now reload the system daemon so it learns of the update. This action alone doesn’t not restart any service.

# systemctl daemon-reload

And also restart the service itself.

# systemctl restart httpd

That’s it!

7 thoughts on “Changing Limits for Services with CentOS 7 / RHEL 7 / Systemd

  1. Reply walt Jan 28,2016 6:23 pm

    How can you verify if the limit is applied correctly?

  2. Reply Gaurav Jan 20,2017 2:49 am

    How do we set the other parameters such as the stack size, no of process etc.
    What is the corresponding parameter for it like LimitNOFILE
    Can you point to the relevant link

  3. Reply Stv Mar 10,2017 11:52 am

    Hi,
    A small correction;
    The “su – apache -c ‘ulimit -aHS’ -s ‘/bin/bash'” responds to the user not the process.

    To find the limits for the process, point to the PID ogf the wrapper:
    e.g. for haproxy
    pidof haproxy-systemd-wrapper | awk ‘{print “/proc/”$1″/limits”}’ | xargs cat | grep -i open

  4. Reply Tomas Apr 12,2017 3:34 pm

    You can probably simplify your steps by using
    systemctl edit [name of service].service
    That I believe will take care of both creating the directory and file structure and take care of daemon-reload once editing is complete.

Leave a Reply