sudo wihout password in your shell script

In the shell script programing, in the cases of running command must be with the root role. but if you su to root, then must input password for root or you may write the root password in your script--it is not good solution.

How to do that?

1. login as root user:

2. type command:

visudo

3. uncomment the following lines:

# Uncomment to allow people in group wheel to run all commands
# %wheel  ALL=(ALL)       ALL

# Same thing without a password
# %wheel  ALL=(ALL)       NOPASSWD: ALL

 To:

# Uncomment to allow people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

# Same thing without a password
%wheel  ALL=(ALL)       NOPASSWD: ALL

 4. run the command:

usermod -G wheel <script user name>

 5. then you can use the <script user name> to run the sudo, it will NOT prompt the password again.

eg. 

sudo chmod +x test.sh
   

猜你喜欢

转载自fengyonghui.iteye.com/blog/570335