Linux ulimit Command Tutorial with Example

The ulimit command is used to set limits about different attributes of the Linux operating system. The ulimit command is provided by all popular Linux distributions like Ubuntu, Mint, Debian, CentOS, RHEL, etc. because it is a kernel too. In this tutorial, we will examine ulimit related terms like Soft Limit, Hard Limit, Set Limit For User and Group, Set Process Limit, File Open Limit, etc. The ulimit is not a separate binary or executable located under the /bin or /usr/bin etc. It is a built-in command provided by the Bash Shell.

ulimit Command Syntax and Options

The ulimit command has very simple syntax where it provides a lot of options which is also called as key.

ulimit LIMIT_TYPE OPTION LIMIT
  • LIMIT_TYPE is the type of limit like Soft Limit or Hard Limit.
  • OPTION is the option we want to limit and set.
  • LIMIT is the limit value we want to set as related to the OPTION.
Key/OptionDescription
-SSet a soft limit for the given resource.
-HSet a hard limit for the given resource.
-aAll current limits are reported.
-bThe maximum socket buffer size.
-cThe maximum size of core files created.
-dThe maximum size of a process’s data segment.
-eThe maximum scheduling priority (“nice”)
-fThe maximum size of files created by the shell(default option).
-iThe maximum number of pending signals.
-kThe maximum number of kqueues that may be allocated.
-lThe maximum size that can be locked into memory.
-mThe maximum resident set size.
-nThe maximum number of open file descriptors.
-pThe pipe buffer size.
-PThe maximum number of pseudoterminals.
-qThe maximum number of bytes in POSIX message queues.
-rThe maximum real-time scheduling priority.
-RThe maximum time a real-time process can run before blocking, in microseconds.
-sThe maximum stack size.
-tThe maximum amount of cpu time in seconds.
-TThe maximum number of threads.
-uThe maximum number of processes available to a single user.
-vThe maximum amount of virtual memory available to the process.
-xThe maximum number of file locks.
ulimit Commant Options

Hard Limit and Soft Limit

Before starting to using ulimit command let’s talk about the hard limit and soft limit. The ulimit command uses the terms hard limit and soft limit. Hard limit is used to specify the limit set to prevent the given attribute to pass the given value. The soft limit can be bypassed sometimes for some periods which is softer than the hard limit.

List Current Limits For Operating System and Kernel

Before starting to set or change limits about different kernel and operating system attributes we should list the current limits configured by default. The -a option is used to list all current limits for different attributes like “core file size” , “data segmentation size” etc.

$ ulimit -a

The output will be like below.

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15380
max locked memory (kbytes, -l) 500491
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 15380
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
List Current List Current Limits For Operating System and Kernel

From this configuration, we can see the core file size is 0 which means we can not create core dumps for the ended processes. In order to create core dumps the size of the core dumps should be increased with this core file size configuration using the ulimits file.

Enable Core Dumps Creation

As the default configuration for the core dump is set to 0 there will be no core dump about aborted or killed processes. We can create a core dump by enabling the -c option like below. We can specify the unlimited in order to set the size unlimited.

$ ulimit -c unlimited

We have set the limit as unlimited for the current user core dump file. We can list this configuration by running the ulimit -a command like below.

$ ulimit -a

Limit Minimum and Maximum Value Range

The ulimit command will set some value for the specified attribute. The soft limit has the maximum value where the hard limit set. The soft limit value can not exceed the hard limit value. The general start value for the hard and soft limit is 0.

limits.conf Configuration File

The ulimit related configurations are stored inside the file named limits.conf . The limits.conf file is located /etc/security/limits.conf for the most of the Linux distributions. Lets take a look inside the limits.conf configuration file.

# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)                                                                                                                                                                      
#        - nice - max nice priority allowed to raise to values: [-20, 19]                                                                                                                                                                  
#        - rtprio - max realtime priority                                                                                                                                                                                                  
#        - chroot - change root to directory (Debian-specific)                                                                                                                                                                             
#                                                                                                                                                                                                                                          
#<domain>      <type>  <item>         <value>                                                                                                                                                                                              
#                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                           
#*               soft    core            0                                                                                                                                                                                                 
#root            hard    core            100000                                                                                                                                                                                            
#*               hard    rss             10000                                                                                                                                                                                             
#@student        hard    nproc           20                                                                                                                                                                                                
#@faculty        soft    nproc           20                                                                                                                                                                                                
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4

# End of file

In this file, there are different examples about setting the specific user or group limits for hard and soft limits.

Set Limit For A User

The ulimit command can be used to set limits for a specific user where this user can be different than the current user. In order to set a limit for another user, we need the root user privileges which can be provided with the sudo command. In the following example, we will set the user ismail process count as 1000 as a hard limit.

ismail      hard       nproc     1000

Set Limit For A Group

If we want to set a limit for a group of users we can use the Linux-provided group names. The Linux group names are provided inside the /etc/groups file. We will also add the @ sign before the group name and then provide the group limit like below which is very similar to the user limit.

@ismail      hard       nproc     1000

Limit Process Count

Even we have previously examined the limit for the process count we will learn how to set limits for the process count for a specific user or group. In the following example, we will set the maximum number of the process as 1024 for the user named ismail and for the group named linuxtect. We will provide the nproc and hard configuration parameters like below.

ismail      hard       nproc     1024
@linuxtect  hard       nproc     1024

Limit Open Files

Another popular case for limiting is setting limit for the current open files. The nofile parameter can be used to set opened file number limit. In the following example we will set number of the opened file limit as 1024 for the users ismail and group linuxtect.

ismail      hard       nofile    1024
@linuxtect  hard       nofile    1024

Leave a Comment