Chinaunix

标题: 问 :关于ulimit 的问题 [打印本页]

作者: ailms    时间: 2007-07-30 23:45
标题: 问 :关于ulimit 的问题
1)ulimit 是否只针对当前的会话有效,一旦退出连接再登录就失效?


  1. [n7css@monitor n7css]$ ulimit -f 1000
  2. [n7css@monitor n7css]$ ulimit -f
  3. 1000
复制代码


退出当前 shell 再次登录,发现可以突破限制了

  1. [n7css@monitor n7css]$ dd if=/dev/zero of=tmp_file bs=1K count=1024
  2. 读入了 1024+0 个块
  3. 输出了 1024+0 个块
  4. [n7css@monitor n7css]$
复制代码



2)root 用户也无法突破普通用户的 ulimit 设置吗?


  1. [n7css@monitor n7css]$ ulimit -f 1000
  2. [n7css@monitor n7css]$ ulimit -f
  3. 1000
复制代码


  1. [root@monitor n7css]# whoami
  2. root
  3. [root@monitor n7css]# pwd
  4. /home/n7css
  5. [root@monitor n7css]# dd if=/dev/zero of=tmp_file bs=1K count=1024
  6. 文件大小超出限制
  7. [root@monitor n7css]#
复制代码

[ 本帖最后由 ailms 于 2007-7-30 23:57 编辑 ]
作者: ailms    时间: 2007-07-30 23:56
又发现一个问题


  1. [n7css@monitor n7css]$ ulimit -u 3
  2. [n7css@monitor n7css]$ ps -u n7css
  3. -bash: fork: 资源暂时不可用
  4. [n7css@monitor n7css]$
  5. [n7css@monitor n7css]$ ulimit -u 30
  6. -bash: ulimit: max user processes: cannot modify limit: 不允许的操作
  7. [n7css@monitor n7css]$
  8. [n7css@monitor n7css]$ ulimit -u
  9. 3
  10. [n7css@monitor n7css]$
复制代码


为什么单独执行 ulimit -u 可以执行,但修改 -u 为 30 就失败呢?

ulimit 是 buitlin 命令,执行时并不需要建立多一个进程吧?

[ 本帖最后由 ailms 于 2007-7-30 23:58 编辑 ]
作者: 寂寞烈火    时间: 2007-07-31 00:52
1)退出当前的shell, 那么临时定义的变量/限制当然不会起作用啦,  可以写在profile/bashrc里
2) 估计和su是否加-有关吧! 简单的su,root使用的还是当前普通用户的环境
1,说明:
ulimit用于shell启动进程所占用的资源.
2,类别:
shell内建命令
3,语法格式:
ulimit [-acdfHlmnpsStvw] [size]

4,参数介绍:

-H 设置硬件资源限制.
-S 设置软件资源限制.
-a 显示当前所有的资源限制.
-c size:设置core文件的最大值.单位:blocks
-d size:设置数据段的最大值.单位:kbytes
-f size:设置创建文件的最大值.单位:blocks
-l size:设置在内存中锁定进程的最大值.单位:kbytes
-m size:设置可以使用的常驻内存的最大值.单位:kbytes
-n size:设置内核可以同时打开的文件描述符的最大值.单位:n
-p size:设置管道缓冲区的最大值.单位:kbytes
-s size:设置堆栈的最大值.单位:kbytes
-t size:设置CPU使用时间的最大上限.单位:seconds
-v size:设置虚拟内存的最大值.单位:kbytes 5

这个东西似乎很深奥, 我对其也是一知半解的~~
作者: ailms    时间: 2007-07-31 00:54
继续顶,该死的字数限制,什么时候取消啊 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
作者: ailms    时间: 2007-07-31 00:56
原帖由 寂寞烈火 于 2007-7-31 00:52 发表
1)退出当前的shell, 那么临时定义的变量/限制当然不会起作用啦,  可以写在profile/bashrc里
2) 估计和su是否加-有关吧! 简单的su,root使用的还是当前普通用户的环境

这个东西似乎很深奥, 我对其也是一知半解 ...



hoho,我是用 su - 转换的,所以就不知道为什么会这样

而且 ulimit 应该不会写 /etc/security/limits.conf ,那么它的设置值又都放在那里呢?是否在内存中,所以才会一退出会话就失效了?
作者: 寂寞烈火    时间: 2007-07-31 00:59
原帖由 ailms 于 2007-7-31 00:56 发表



hoho,我是用 su - 转换的,所以就不知道为什么会这样

而且 ulimit 应该不会写 /etc/security/limits.conf ,那么它的设置值又都放在那里呢?是否在内存中,所以才会一退出会话就失效了?

/home/lee#grep ulimit /etc/profile
ulimit -S -c 0 > /dev/null 2>&1
可以把你需要限定的放在这里, 不同用户的当然放在不同的 用户的环境文件里

[ 本帖最后由 寂寞烈火 于 2007-7-31 01:00 编辑 ]
作者: ailms    时间: 2007-07-31 01:07
多谢烈火兄,我再好好研究一下
作者: yuhe27913    时间: 2007-07-31 01:09
修改/etc/security/limits.conf 是永久修改。你为什么觉得不是这个文件呢?
作者: 寂寞烈火    时间: 2007-07-31 01:12
原帖由 yuhe27913 于 2007-7-31 01:09 发表
修改/etc/security/limits.conf 是永久修改。你为什么觉得不是这个文件呢?

嗯, 其实配置这个文件更加灵活, 他可以针对不同的用户做不同的限制, 我也更倾向于此
作者: ailms    时间: 2007-07-31 01:17
原帖由 yuhe27913 于 2007-7-31 01:09 发表
修改/etc/security/limits.conf 是永久修改。你为什么觉得不是这个文件呢?



如果是,为什么退出会话时就失效了?
作者: ailms    时间: 2007-07-31 01:18

  1. [n7css@monitor n7css]$ cat /etc/security/limits.conf
  2. # /etc/security/limits.conf
  3. #
  4. #Each line describes a limit for a user in the form:
  5. #
  6. #<domain>        <type>  <item>  <value>
  7. #
  8. #Where:
  9. #<domain> can be:
  10. #        - an user name
  11. #        - a group name, with @group syntax
  12. #        - the wildcard *, for default entry
  13. #
  14. #<type> can have the two values:
  15. #        - "soft" for enforcing the soft limits
  16. #        - "hard" for enforcing hard limits
  17. #
  18. #<item> can be one of the following:
  19. #        - core - limits the core file size (KB)
  20. #        - data - max data size (KB)
  21. #        - fsize - maximum filesize (KB)
  22. #        - memlock - max locked-in-memory address space (KB)
  23. #        - nofile - max number of open files
  24. #        - rss - max resident set size (KB)
  25. #        - stack - max stack size (KB)
  26. #        - cpu - max CPU time (MIN)
  27. #        - nproc - max number of processes
  28. #        - as - address space limit
  29. #        - maxlogins - max number of logins for this user
  30. #        - priority - the priority to run user process with
  31. #        - locks - max number of file locks the user can hold
  32. #
  33. #<domain>      <type>  <item>         <value>
  34. #

  35. #*               soft    core            0
  36. #*               hard    rss             10000
  37. #@student        hard    nproc           20
  38. #@faculty        soft    nproc           20
  39. #@faculty        hard    nproc           50
  40. #ftp             hard    nproc           0
  41. #@student        -       maxlogins       4

  42. # End of file
  43. [n7css@monitor n7css]$
  44. [n7css@monitor n7css]$ ulimit -f 2000
  45. [n7css@monitor n7css]$ cat /etc/security/limits.conf
  46. # /etc/security/limits.conf
  47. #
  48. #Each line describes a limit for a user in the form:
  49. #
  50. #<domain>        <type>  <item>  <value>
  51. #
  52. #Where:
  53. #<domain> can be:
  54. #        - an user name
  55. #        - a group name, with @group syntax
  56. #        - the wildcard *, for default entry
  57. #
  58. #<type> can have the two values:
  59. #        - "soft" for enforcing the soft limits
  60. #        - "hard" for enforcing hard limits
  61. #
  62. #<item> can be one of the following:
  63. #        - core - limits the core file size (KB)
  64. #        - data - max data size (KB)
  65. #        - fsize - maximum filesize (KB)
  66. #        - memlock - max locked-in-memory address space (KB)
  67. #        - nofile - max number of open files
  68. #        - rss - max resident set size (KB)
  69. #        - stack - max stack size (KB)
  70. #        - cpu - max CPU time (MIN)
  71. #        - nproc - max number of processes
  72. #        - as - address space limit
  73. #        - maxlogins - max number of logins for this user
  74. #        - priority - the priority to run user process with
  75. #        - locks - max number of file locks the user can hold
  76. #
  77. #<domain>      <type>  <item>         <value>
  78. #

  79. #*               soft    core            0
  80. #*               hard    rss             10000
  81. #@student        hard    nproc           20
  82. #@faculty        soft    nproc           20
  83. #@faculty        hard    nproc           50
  84. #ftp             hard    nproc           0
  85. #@student        -       maxlogins       4

  86. # End of file
  87. [n7css@monitor n7css]$
复制代码


执行 ulimit -f 2000 后 /etc/security/limits.conf 没有任何变化,

况且该文件只有 root 可写,普通用户执行 ulimit 怎么可能修改该文件呢!
作者: yuhe27913    时间: 2007-07-31 01:24
普通用户不写,只是读。/etc/security/limits.conf 需要root用vi修改。
普通用户在自己的会话中可以做临时修改并不意味在要修改/etc/security/limits.conf
作者: yuhe27913    时间: 2007-07-31 01:26
/etc/security/limits.conf 要手动修改
格式是
写的很清楚了
#<domain>      <type>  <item>         <value>

<domain>就是你的username
作者: yuhe27913    时间: 2007-07-31 01:29
即使root在自己的会话中修改file-max,number of processes限制也不会修改/etc/security/limits.conf
你为什么会那么想?
呵呵
作者: 寂寞烈火    时间: 2007-07-31 01:31
[n7css@monitor n7css]$ ulimit -f 2000

这样的设定只对当前环境有效, 当注消shell后就没作用啦
/etc/security/limits.conf文件如楼上所说, 格式已经很清楚啦
作者: ailms    时间: 2007-07-31 01:34
原帖由 yuhe27913 于 2007-7-31 01:29 发表
即使root在自己的会话中修改file-max,number of processes限制也不会修改/etc/security/limits.conf
你为什么会那么想?
呵呵



多谢 yuhe27913,俺明白了

不过还是不能解释为什么 su - 后仍然无法突破限制的问题啊

还有那些 ulimit -u 的问题又怎么解释呢?
作者: yuhe27913    时间: 2007-07-31 01:43
su - 是切换成root并取得root的环境变量啊。
ulimit -u是打印当前用户的the limited number of processes
ulimit -u 30执行不成功我倒是不清楚什么原因,手头也没有linux环境。
作者: ailms    时间: 2007-07-31 01:45


su - 是切换成root并取得root的环境变量啊。


就是啊,既然 euid 都是 root 了,为什么无法突破 n7css 用户的  ulimit -f 限制
作者: yuhe27913    时间: 2007-07-31 01:50
倒,你再做一次测试贴上来吧。
作者: ailms    时间: 2007-07-31 01:54
新鲜热辣的实验结果出炉了 !!!!!!! 还是不行 !!!!!


  1. [n7css@monitor n7css]$ ulimit -f 2000
  2. [n7css@monitor n7css]$ su -
  3. Password:
  4. [root@monitor root]# dd if=/dev/zero of=tmp_file bs=1K count=2048
  5. 文件大小超出限制
  6. [root@monitor root]#
  7. [root@monitor root]# echo $LANG
  8. zh_CN.GB18030
  9. [root@monitor root]#
复制代码

作者: yuhe27913    时间: 2007-07-31 01:56
呵呵,果然是这样。你大概是恶搞吧??
root凭什么引用n7css的环境变量。
作者: ailms    时间: 2007-07-31 02:03
原帖由 yuhe27913 于 2007-7-31 01:56 发表
呵呵,果然是这样。你大概是恶搞吧??
root凭什么引用n7css的环境变量。




算了,睡觉了。

btw :恶搞这种东西我是不会用到技术上的,ls 有时间不妨做一下实验再说吧
作者: h0tr0ck    时间: 2007-07-31 09:33
之前写的关于文件描述符限制的东西,大家指正一下 ,谢谢

ulimit -n number  通过这个命令可以设置linux中单一进程可以同时打开的文件描述符
ulimit -n 通过这个方式可以查看单一进程可以同时打开的文件描述符

在/proc/sys/fs/file-max 文件中可以设置整机可以同时打开的最大的文件描述符

由于标准输入输出和标准错误输出分别占据了0 1 2 这三个数字的描述符,所以每一个进程的文件描述符都是从3开始,所以每个进程能自行指定打开的文件描述符要比限制少3个。

普通用户权限:
在使用普通用户权限打开的终端里面使用ulimit -n number减少了能打开的文件描述符之后不能再增加。

root用户权限:
在root权限登录的终端可以修改到最大的值是100万左右大小的文件描述字,这个可能是在系统内核里面做出了限制(没有验证过),在2.6和2.4内核上面可以设置到100万左右。

此时如果修改的值(值A)大于/proc/sys/fs/file-max文件中的值(值B),系统会提示文件描述符超出规定范围,但是仍然可以对文件描述符大于值B的文件进行正常的读写操作。(此处没有详细考虑会出现什么问题)


/proc/sys/fs/file-max文件的值每次开机都需要更改


所有进程的文件描述符都不能超过ulimit命令指定的文件描述符大小,超过则不能打开文件。



C语言操作资源
在linux系统的库函数里面提供了修改和获取限制资源的函数
getrlimit和setrlimit这两个函数
需要用到的数据结构为  struct rlimit
作者: cx6445    时间: 2007-07-31 09:39
原帖由 ailms 于 2007-7-31 02:03 发表




算了,睡觉了。

btw :恶搞这种东西我是不会用到技术上的,ls 有时间不妨做一下实验再说吧



你的ls已经说得很清楚了,root凭什么引用n7css的环境变量。
作者: ailms    时间: 2007-07-31 10:06
原帖由 cx6445 于 2007-7-31 09:39 发表



你的ls已经说得很清楚了,root凭什么引用n7css的环境变量。



既然 root 不会引用 n7css 的环境变量,那为什么 root 不能创建该文件?

还有你所谓的环境变量是那个?能具体说明吗?
作者: ailms    时间: 2007-07-31 10:17
to 所有朋友 :能否把你们的实验结果贴上来呢?我们以实验结果说话,不要再做无畏的推论,ok ?
作者: mxbao    时间: 2007-07-31 11:19
[root@3.7.0Biz-47 /home/bbb]# useradd hh
[root@3.7.0Biz-47 /home/bbb]# su hh
/bin/cat: /www/prima/VERSION: Permission denied
[hh@ /home/bbb]$ ulimit -f 1000
[hh@ /home/bbb]$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) 1000
pending signals                 (-i) 1024
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 12159
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[hh@ /home/bbb]$ su -
Password:
[root@3.7.0Biz-47 ~]# dd if=/dev/zero of=/tmp/test bs=1k count=1024
File size limit exceeded

[root@3.7.0Biz-47 ~]#
貌似不能突破哦
作者: ailms    时间: 2007-07-31 13:06
原帖由 mxbao 于 2007-7-31 11:19 发表
[root@3.7.0Biz-47 /home/bbb]# useradd hh
[root@3.7.0Biz-47 /home/bbb]# su hh
/bin/cat: /www/prima/VERSION: Permission denied
[hh@ /home/bbb]$ ulimit -f 1000
[hh@ /home/bbb]$ ulimit -a
core f ...


就是啊。

:em11: :em11:
作者: yuhe27913    时间: 2007-07-31 14:43
hi,这个问题果然有趣。
事先我的root是unlimited
oracle 做了ulimt -n 8848   之后su -到root时候root ulimit -n 是8848
而且重新打开子shell也是这样。看来ulimit并非像环境变量那么简单。
作者: ailms    时间: 2007-07-31 14:45
俺的想法是 :

由于 ulimit 是影响当前进程和子进程的,而 su - 后的进程也是当前 shell 的一个子进程

所以 ulimit 可能是根据 PPID 来控制的
作者: yuhe27913    时间: 2007-07-31 16:24
原帖由 ailms 于 2007-7-31 14:45 发表
俺的想法是 :

由于 ulimit 是影响当前进程和子进程的,而 su - 后的进程也是当前 shell 的一个子进程

所以 ulimit 可能是根据 PPID 来控制的


也不完全是,ulimit首先参考的是/etc/security/limit.conf中的设置
你可以做一个测试,在该参数文件中加入root的条目,无论用户是su到root还是su -到root 它的ulimit相关值总是以/etc/security/limit.conf中的设定为准。
作者: ailms    时间: 2007-08-02 13:13
原帖由 yuhe27913 于 2007-7-31 16:24 发表


也不完全是,ulimit首先参考的是/etc/security/limit.conf中的设置
你可以做一个测试,在该参数文件中加入root的条目,无论用户是su到root还是su -到root 它的ulimit相关值总是以/etc/security/limit.conf ...



/etc/security/limits.conf 我没有改过,都是默认值

所以上述的问题和该文件关系应该不大。
作者: ailms    时间: 2007-08-02 13:14
还是想问一下 :为什么 ulimit -t 无法自动结束 vi 、watch 这类需要全屏操作的程序呢?
作者: ailms    时间: 2007-08-02 15:23

作者: iventor    时间: 2007-08-06 02:56
就算RLIMIT_CPU等于0(进程能运行1秒钟), vi也是能够运行, x保存后shell程序立即被SIGKILL.

time bash -c 'ulimit -t 0; vi zhaoke; while :; do :; done'


原帖由 ailms 于 2007-8-2 13:14 发表
还是想问一下 :为什么 ulimit -t 无法自动结束 vi 、watch 这类需要全屏操作的程序呢?

作者: iventor    时间: 2007-08-06 23:39
标题: 回复 #1 ailms 的帖子
1. ulimit命令只对当前shell及由shell启动进程的资源进行限制, 退出后原先的ulimit设定(命令行)无效.
2. root用户有自己的ulimit设置(从/etc/security/limits.conf读入), 普通用户su到root用户后还是受
普通用户下ulimit命令所生成的数值限制.

ulimit -f 1000命令中的1000是1000K, 1M=1024K.
所以结果"文件大小超出限制"是合理的.

实例:

[zhaoke@islab ~]$ ulimit -f
unlimited
[zhaoke@islab ~]$ ulimit -f 1000
[zhaoke@islab ~]$ ulimit -f
1000
[zhaoke@islab ~]$ su -
Password:
[root@islab ~]# ulimit -f
1000
[root@islab ~]# dd if=/dev/zero of=tmp_file bs=1k count=1024
File size limit exceeded


[zhaoke@islab ~]$ ulimit -f
unlimited
[zhaoke@islab ~]$ ulimit -f 1024
[zhaoke@islab ~]$ ulimit -f
1024
[zhaoke@islab ~]$ su -
Password:
[root@islab ~]# ulimit -f
1024
[root@islab ~]# dd if=/dev/zero of=tmp_file bs=1k count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.00924075 s, 113 MB/s
[root@islab ~]# ll tmp_file
-rw-r--r-- 1 root root 1048576 2007-08-06 23:36 tmp_file
作者: ailms    时间: 2007-08-07 00:05
多谢 iventor

这些实验结果我也知道,我想知道的是“为什么会这样?”

尤其是最后一个 ulimit -u 的 case
作者: iventor    时间: 2007-08-07 00:46
ulimit -u 3设置的是n7css用户在shell中能够fork的最多子进程数(RLIMIT_NPROC), -u设置包括软硬限制, 也就是说n7css用户的软硬限制fork数为3, 以后在该shell下软硬限制数可以设置为3, 也可以设置为2甚至1.  但是每一次设置的max fork值不能超过上一次的设置. 该用户的shell会话结束后, RLIMIT_NPROC数清除.

[zhaoke@islab ~]$ ulimit -u 3
[zhaoke@islab ~]$ ulimit -u
3
[zhaoke@islab ~]$ ulimit -u 30
bash: ulimit: max user processes: cannot modify limit: Operation not permitted
[zhaoke@islab ~]$ ulimit -Hu 30
bash: ulimit: max user processes: cannot modify limit: Operation not permitted
[zhaoke@islab ~]$ ulimit -Su 30
bash: ulimit: max user processes: cannot modify limit: Invalid argument
[zhaoke@islab ~]$ ulimit -Su 2
[zhaoke@islab ~]$ ulimit -u
2
[zhaoke@islab ~]$ ulimit -Su 3
[zhaoke@islab ~]$ ulimit -u
3
[zhaoke@islab ~]$ ulimit -Su 4
bash: ulimit: max user processes: cannot modify limit: Invalid argument
[zhaoke@islab ~]$ ulimit -Hu 4
bash: ulimit: max user processes: cannot modify limit: Operation not permitted



原帖由 ailms 于 2007-7-30 23:56 发表
又发现一个问题


[n7css@monitor n7css]$ ulimit -u 3
[n7css@monitor n7css]$ ps -u n7css
-bash: fork: 资源暂时不可用
[n7css@monitor n7css]$
[n7css@monitor n7css]$ ulimit -u 30
-bash: ulim ...

[ 本帖最后由 iventor 于 2007-8-7 00:57 编辑 ]




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2