Linux 使用 sudo ulimit 时找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17483723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:23:19  来源:igfitidea点击:

command not found when using sudo ulimit

linuxshellubuntu-12.04

提问by sunpy

I was using ubuntu 12.04 ,on which I run ulimit -n ,it is showing 1024, I want to increase my open file limit from 1024 to 65535,so I tried the following command:

我正在使用 ubuntu 12.04 ,我在上面运行 ulimit -n ,它显示 1024,我想将打开文件限制从 1024 增加到 65535,所以我尝试了以下命令:

sudo ulimit -n 65535

but i get the following error:

但我收到以下错误:

sudo: ulimit: command not found

How to increase the file limit from 1024 to 65535? Any help will be appreciated.

如何将文件限制从 1024 增加到 65535?任何帮助将不胜感激。

回答by andrewdotn

ulimitis a shell builtin like cd, not a separate program. sudolooks for a binary to run, but there is no ulimitbinary, which is why you get the error message. You need to run it in a shell.

ulimit是一个内置的 shell cd,而不是一个单独的程序。sudo查找要运行的二进制文件,但没有ulimit二进制文件,这就是您收到错误消息的原因。您需要在 shell 中运行它。

However, while you do need to be root to raise the limit to 65535, you probably don't want to run your program as root. So after you raise the limit you should switch back to the current user.

然而,虽然您确实需要成为 root 才能将限制提高到 65535,但您可能不想以 root 身份运行您的程序。因此,在提高限制后,您应该切换回当前用户。

To do this, run:

为此,请运行:

sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"

and you will get a new shell, without root privileges, but with the raised limit. The execcauses the new shell to replace the process with sudoprivileges, so after you exit that shell, you won't accidentally end up as root again.

你会得到一个新的 shell,没有 root 权限,但有提高的限制。这exec会导致新 shell 用sudo特权替换该进程,因此在您退出该 shell 后,您不会意外地再次以 root 身份结束。

回答by Craig

I've had to deal with issues like this in the past. Since there is no setuid mechanism for shell scripts (because it's insecure), I've found writing a simple C wrapper with a setuid is suffice and then using a system call to modify the ulimits of the running process before dropping privileges and executing your shell script.

过去我不得不处理这样的问题。由于 shell 脚本没有 setuid 机制(因为它不安全),我发现用 setuid 编写一个简单的 C 包装器就足够了,然后在删除权限和执行 shell 之前使用系统调用来修改正在运行的进程的 ulimits脚本。