ulimit

shell builtinLinux/Unix
The ulimit command is one of the most frequently used commands in Linux/Unix-like operating systems. ulimit Get and set user limits for system resources

Quick Reference

Command Name:

ulimit

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

ulimit [options] [arguments]

Common Use Cases

    Syntax

    ulimit [-HSabcdefiklmnpqrstuvxPT] [limit]

    Options

    Option Description
    -H Set the hard limit
    -S Set the soft limit
    -a Show all current limits
    -b The maximum socket buffer size
    -c The maximum size of core files created
    -d The maximum size of a process's data segment
    -e The maximum scheduling priority ("nice")
    -f The maximum size of files written by the shell and its children
    -i The maximum number of pending signals
    -k The maximum number of kqueues allocated
    -l The maximum size that may be locked into memory
    -m The maximum resident set size
    -n The maximum number of open file descriptors
    -p The pipe buffer size
    -q The maximum number of bytes in POSIX message queues
    -r The maximum real-time scheduling priority
    -s The maximum stack size
    -t The maximum amount of CPU time in seconds
    -u The maximum number of processes available to a single user
    -v The maximum amount of virtual memory available to the shell
    -x The maximum number of file locks
    -P The maximum number of pseudoterminals
    -T The maximum number of threads

    Examples

    How to Use These Examples

    The examples below show common ways to use the ulimit command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    Display all limits
    ulimit -a
    Check the maximum number of open files
    ulimit -n
    Set the maximum number of open files to 4096
    ulimit -n 4096
    Check the maximum file size a process can create
    ulimit -f

    Advanced Examples:

    Set unlimited core file size
    ulimit -c unlimited
    Set both soft and hard limits for open files
    ulimit -Hn 8192
    ulimit -Sn 4096
    Set maximum virtual memory to 2GB (2097152 KB)
    ulimit -v 2097152
    Set the maximum CPU time to 60 seconds
    ulimit -t 60
    Check max user processes
    ulimit -u
    Set unlimited stack size
    ulimit -s unlimited
    Save ulimit settings to profile for persistence
    echo "ulimit -n 4096" >> ~/.bashrc
    echo "ulimit -c unlimited" >> ~/.bashrc
    Set different limits for a specific command
    (ulimit -t 10; ./cpu_intensive_command)
    Check max pending signals
    ulimit -i
    Set limit for all shells
    sudo sh -c 'echo "* soft nofile 4096" >> /etc/security/limits.conf'
    sudo sh -c 'echo "* hard nofile 8192" >> /etc/security/limits.conf'

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `ulimit` command is a shell builtin that provides control over the resources available to processes started by the shell and to the shell itself. It's a crucial tool for system administrators and developers to manage system stability, security, and performance by controlling how much of the system's resources a user or process can consume. The command works with two types of limits: 1. **Soft Limits**: These are the current effective limits that are actually enforced by the system. Users can increase their soft limits up to the defined hard limit. Soft limits are set with the `-S` option. 2. **Hard Limits**: These represent the maximum value that can't be exceeded by setting a soft limit. Only root users can increase hard limits. Hard limits are set with the `-H` option. If neither `-H` nor `-S` is specified, both limits are set when a limit value is provided, or the soft limit is displayed when showing current settings. Some of the most commonly used resource limits include: - **File Size (`-f`)**: Controls the maximum size of files that a process can create. This helps prevent processes from filling up the filesystem. - **Open Files (`-n`)**: Limits the number of file descriptors (open files) a process can have. This is particularly important for applications like databases or web servers that may need to handle many connections simultaneously. - **Process Count (`-u`)**: Restricts the number of processes a user can run, which can prevent fork bombs and other denial-of-service attacks. - **CPU Time (`-t`)**: Limits the amount of CPU time a process can consume, preventing a single process from monopolizing the CPU. - **Core Dumps (`-c`)**: Controls the size of core files created when a program crashes, which is useful for debugging but can consume disk space quickly. - **Memory Usage (`-v`, `-m`, `-l`)**: Various options control different aspects of memory usage, helping to prevent memory exhaustion. It's important to note that `ulimit` settings only affect the current shell and any processes launched from it. To make limits persistent across logins, they need to be set in shell configuration files like `.bashrc` or system-wide configuration files like `/etc/security/limits.conf`. In multi-user systems, properly configured resource limits are essential for preventing users from adversely affecting each other or the system as a whole. For example, setting appropriate limits can prevent one user's runaway process from consuming all system memory or file descriptors, which would impact other users. For developers, understanding and setting appropriate `ulimit` values is crucial when deploying applications, especially those that need to handle many concurrent connections or process large amounts of data. Incorrectly set limits can lead to application failures under load, such as the common "too many open files" error in high-traffic web servers. The `ulimit` command is available in most UNIX-like operating systems, including Linux, macOS, and various BSD distributions, though the exact options and behavior may vary slightly between different shells and systems.

    Related Commands

    These commands are frequently used alongside ulimit or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the ulimit command works in different scenarios.

    $ ulimit
    View All Commands