简体   繁体   中英

How to run command in su mode in bash script?

I need to run these two commands :

ulimit -s 1024

echo 120000 > /proc/sys/kernel/threads-max

The first one can be run just in user mode (not using sudo or su) and the second can only be run in su mode . I want to write a bash script that let me run these two commands. The first one is OK. For the second one, I need to su (change user to root), run the command, and then exit. Actually, I want to run the second command in su mode using a bash script. Any idea?

如果您的用户有权使用“ sudo tee”,那么一种解决方案是:

echo 120000 | sudo tee /proc/sys/kernel/threads-max

As a security measure, you cannot run scripts as a superuser without prepending sudo . If you want it to be passwordless, you need to run visudo and allow your (or the executing user) to run this command as a superuser without password confirmation.

The other way is to use the setuid bit on compiled code. Compile a simple program which will execute the echo 120000 > /proc/... , then change it to be owned by root: chown 0:0 executable_name , and chmod u+s executable_name to set the setuid bit on it. This will cause execution of this program to be ran with permissions of its owner, which is root.

This is the same way which allows passwd to modify a file which requires super-user privileges without actually being a super-user or sudoer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM