简体   繁体   中英

Enter password for a sudo command in a chain of commands

In Linux how can I enter the password for one of the commands in a chain of commands which requires a sudo. One example which I can think of is when after running a long compile job, I want to shutdown the machine.

make ; sudo init 0 

I want the shutdown command to run only after make finishes, but would like to enter the password right away, because I won't be there when the first command is done. Also, I don't want to run "make" with super user privileges. So switching to root and running the commands is also out of the question.

Thanks!

sudo sh -c "su -c 'make' $USER && init 0"

Change your uid early and often. That's the Chicago way!

You can run sudo -v to update the sudo timestamp - this means you won't need to enter a password for x minutes (default is 5). You can change the timeout by editing the timestamp_timeout in the sudoers file.

You also might want to change your command to

make && sudo init 0

which will only shut down if make completes successfully.

为什么不把密码输入sudo

make; echo 'password' | sudo -S init 0

One possibility is to use Expect to automate the password input.

Here's a simple HOW-TO for expect and passwords: http://www.linux.com/archive/feed/56066

Another possibility is to edit your /etc/sudoers and set your user to be able to use sudo without password. if you check the file it will be explained in the comments there.

Change the timeout for sudo to something long enough to cover your usage, then run something like sudo echo to authenticate, then run your other commands. As long as the password timeout hasn't been reached, it should execute without asking again.

You can add that to your sudoers file

username ALL = NOPASSWD: /sbin/init

where username is the username you want to allow.

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