


How to limit user resources in Linux? How to configure ulimit?
May 29, 2025 pm 11:09 PMLinux system restricts user resources through the ulimit command to prevent excessive use of resources. 1. Ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Temporary modifications use the ulimit command directly, such as ulimit -n 2048, but it is only valid for the current session. 3. For permanent effect, modify /etc/security/limits.conf and PAM configuration files, and add session required pam_limits.so. 4. The systemd service needs to set parameters such as LimitNOFILE, LimitNPROC in the unit file, and overload systemd takes effect.
The Linux system restricts user resources, mainly through the mechanism provided by the kernel to control the process's use of system resources. Among them, ulimit
is a commonly used command line tool to set or view resource restrictions under the current shell session. It is suitable for preventing a user or program from occupying too much resources, such as memory, CPU time, number of opened files, etc.
Here are some common configuration methods and precautions:
1. What is ulimit? What resources can be limited?
ulimit
is a built-in command in Linux shell to control the usage limit of system resources. What it may limit includes but is not limited to:
- Maximum number of open file descriptors (
-n
) - The maximum memory size that a single process can use (
-v
) - Maximum number of threads (
-u
) - CPU time (
-t
) - Stack space size (
-s
)
These limitations are divided into soft limit and hard limit :
- The soft limit is the value that you are currently in effect. Ordinary users can only adjust to a value that does not exceed the hard limit.
- The hard limit is set by root and cannot be exceeded by ordinary users.
For example, to view the resource limits of the current user, you can use:
ulimit -a
2. How to temporarily modify ulimit settings?
If you just want to temporarily modify certain restrictions in the current terminal session, you can use the ulimit
command directly. For example:
ulimit -n 2048 # Set the number of open files to 2048 ulimit -u 1024 # Set up to 1024 processes per user
Note that this modification is only valid in the current shell session, and will be invalid after restarting or opening a new terminal.
In addition, some restrictions can only be raised to the range of hard restrictions. If you need to break through hard restrictions, you have to use root permissions to modify them.
3. How to take effect permanently? How to change the configuration file?
To make the ulimit setting take effect every time you log in, you need to modify the system configuration file.
There are mainly two documents:
-
/etc/security/limits.conf
-
/etc/pam.d/common-session
(Debian system) or/etc/pam.d/login
(RedHat system)
Add something like this in limits.conf
:
* soft nofile 4096 * hard nofile 8192 www-data soft nproc 2048 www-data hard nproc 4096
This means that the soft limit for all users ( *
) is 4096 file descriptors, and the hard limit is 8192; and the process limit for www-data
users is also set.
In addition, make sure that the PAM module is loaded with pam_limits.so
and add it to /etc/pam.d/common-session
:
session required pam_limits.so
In this way, the system will apply the ulimit rules you set every time the user logs in.
4. How to set resource restrictions for systemd service?
If you are using a service managed through systemd, such as nginx, redis, and custom scripts, you cannot rely on limits.conf
, because the systemd service does not follow the PAM process.
At this time, resource restrictions should be set in the service unit file. For example, add in /etc/systemd/system/myapp.service
:
[Service] LimitNOFILE=8192 LimitNPROC=4096
Then reload systemd and restart the service:
systemctl daemon-reexec systemctl restart myapp
This will allow you to control the resource limit of a certain service individually.
Basically that's it. Ulimit looks simple, but pay attention to the way it takes effect and scope of action. Sometimes you set it but it doesn't take effect, it may be because the service does not go through the PAM process or is overwritten by systemd. When encountering problems, remember to check the logs, or use the prlimit
command to view the actual limitations of the specific process.
The above is the detailed content of How to limit user resources in Linux? How to configure ulimit?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Airdrops in the cryptocurrency field are a marketing promotion method for the project to distribute a certain number of tokens for free to community members or potential users. In this way, the project party hopes to increase the visibility of the tokens and attract more users to participate in the project, thereby expanding the size of the community and increasing the liquidity of the tokens. For users, airdrops provide opportunities to obtain project tokens without initial investment, and are one of the ways to get in touch with and understand new projects in the early stage.

Currency circle contract trading is a derivative trading method that uses a small amount of funds to control assets with larger value. It allows traders to speculate on the price trends of crypto assets without actually owning them. Entering the contract market requires understanding its basic operations and related concepts.

When choosing a suitable formal Bitcoin trading platform, you should consider comprehensively from the dimensions of compliance, transaction depth, and functional support. The above ten platforms are widely recognized among global users and provide safe and direct official websites. It is recommended that users give priority to accessing and registering through the official website to avoid third-party links and ensure the security of account assets. In the future, the functions of trading platforms will be more intelligent, and it is recommended to continue to pay attention to the updates and activity policies of each platform.

How do novice users choose a safe and reliable stablecoin platform? This article recommends the Top 10 stablecoin platforms in 2025, including Binance, OKX, Bybit, Gate.io, HTX, KuCoin, MEXC, Bitget, CoinEx and ProBit, and compares and analyzes them from dimensions such as security, stablecoin types, liquidity, user experience, fee structure and additional functions. The data comes from CoinGecko, DefiLlama and community evaluation. It is recommended that novices choose platforms that are highly compliant, easy to operate and support Chinese, such as KuCoin and CoinEx, and gradually build confidence through a small number of tests.

As an investment method, the currency circle contract order has attracted many investors who want to participate in cryptocurrency contract trading but do not have sufficient time and expertise. The basic principle is to associate your trading account with the outstanding trader's account selected on the platform, and the system will automatically synchronize the trader's opening and closing operation. The user does not need to manually analyze the market and execute the transaction, and the follower is done by the trader. This model seems to simplify the trading process, but it is accompanied by a series of issues that require careful consideration.

The Virtual Digital Coin Exchange APP is a powerful digital asset trading tool, committed to providing safe, professional and convenient trading services to global users. The platform supports a variety of mainstream and emerging digital asset transactions, with a bank-level security protection system and a smooth operating experience.

Bitcoin is neither a pure scam nor a single future trend, but an innovative asset that combines controversy and value. Its core value is reflected in: 1. Anti-inflation characteristics, fixed total volume makes it scarce and is regarded as digital gold; 2. Global liquidity, supporting low-cost cross-border transactions; 3. Decentralization and censorship resistance, ensuring user asset autonomy; 4. Technological innovation, underlying blockchain promotes the transformation of trust mechanisms and data storage. Despite the challenges of regulatory and volatility, Bitcoin continues to have far-reaching impacts in the financial and technology fields.

Against the backdrop of violent fluctuations in the cryptocurrency market, investors' demand for asset preservation is becoming increasingly prominent. This article aims to answer how to effectively hedge risks in the turbulent currency circle. It will introduce in detail the concept of stablecoin, a core hedge tool, and provide a list of TOP3 stablecoins by analyzing the current highly recognized options in the market. The article will explain how to select and use these stablecoins according to their own needs, so as to better manage risks in an uncertain market environment.
