OS/Linux

[Linux] ulimit란? 내용 정리

yeit 2024. 1. 31. 21:43
728x90

 

ulimit(user limit)란?

프로세스가 사용할 수 있는 자원 한도를 설정하는 명령어. soft랑 hard로 구분된다.

  • Soft ulimit : 현재 프로세스에 적용되는 한도로, 프로세스가 자원을 사용하는 기본 한도
  • Hard ulimit : 사용자나 프로세스가 설정할 수 있는 최대한도, Hard limit은 루트 사용자만 변경 가능

서버 환경에서 동시에 많은 프로세스가 실행될 때 max user process 부족이나 open files 부족과 같은 문제가 발생할 수 있다. 주로 이런 경우에 ulimit 명령어를 사용하여 해당 값들을 조정해 준다.

 

 

ulimit 확인

# Soft 확인
# ulimit -a 또는 # ulimit -a -S

# Hard 확인
# ulimit -a -H

 

 

ulimit 옵션

ulimit명령어를 치면 괄호 안에 알파벳( -c, -d, -f 등)이 있는데, 이 알파벳들이 해당 설정을 변경하는 옵션이다.

  • -c (core dump size) : 코어 덤프 파일의 최대 크기를 설정
    코어 덤프란? 프로세스가 비정상적으로 종료될 때 생성되는 메모리 덤프 파일
  • -d (data seg size) : 데이터 세그먼트의 최대 크기를 설정
  • -f (file size) : 프로세스가 생성하는 파일의 최대 크기를 제한
  • -n (open files) : 프로세스가 동시에 열 수 있는 파일 디스크립터의 최대 개수 설정
  • -s (stack size) : 스택의 최대 크기 설정, 각 스레드 또는 프로세스에 할당되는 스택의 크기를 조절
  • -u (max user processes) : 사용자가 동시에 실행할 수 있는 프로세스의 최대 개수를 제한
  • -l (max locked memory) : lock 된 메모리의 최대 크기를 제한
  • v (virtual memory size) : 프로세스가 사용할 수 있는 가상 메모리의 최대 크기를 설정

추가) OutOfMemoryError: unable to create new native thread가 발생하는 경우, max user processes값을 조정해 주어 문제를 해결할 수 있다.

 

 

ulimit 설정 방법

방법 1. ulimit 명령을 통한 설정 _임시로 변경

이 방법은 세션이 끊기게 되면 설정했던 값들도 초기화된다.

# open files 설정 변경
# ulimit -n 2048

# max user processes 설정 변경
# ulimit -u 4096
# soft limit과 hard limit 동시에 설정
# ulimit -u 4096:4096

# hard limit 설정
# ulimit -Hu 8192

 

 

방법 2. /etc/security/limits.conf 파일 수정을 통한 설정 _영구적으로 변경, user별로 설정 가능

limits.conf 파일 밑에 부분에 있는 <domain> <type> <item> <value>에 맞춰 원하는 설정 값을 넣어주면 된다.

파일 수정 후, 세션을 재시작하거나 재로그인하면 변경 사항이 적용된다.

  • <domain> : 사용자명 (사용자 이름, 그룹 이름, *(와일드카드)와 같은 값이 들어갈 수 있음)
  • <type> : soft limit 또는 hard limit 중 선택
  • <item> : 리소스 제한 항목, 각 항목들은 limits.conf파일 상단에 주석 처리된 부분을 보면 설명되어 있음
  • <value> : 해당 리소스에 대한 값
[root@server1 ~]# cat /etc/security/limits.conf 

# /etc/security/limits.conf
#
#This file sets the resource limits for the users logged in via PAM.
#It does not affect resource limits of the system services.
#
#Also note that configuration files in /etc/security/limits.d directory,
#which are read in alphabetical order, override the settings in this
#file in case the domain is the same or more specific.
#That means for example that setting a limit for wildcard domain here
#can be overriden with a wildcard setting in a config file in the
#subdirectory, but a user specific setting here can be overriden only
#with a user specific setting in the subdirectory.
#
#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
#
#<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
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

*             hard    nofile     2048
testuser      soft    nproc      4096


# End of file

 

 

 

 

728x90