Reputation: 2123
I want to run the following sample bash script which needs sudo password for a command
#!/bin/bash
kinit #needs sudo password
vi hello.txt
while running the above script it is asking for password.
How can i pass the username and password in the command itself or is there any better way i can skip passing my password in the script ?
Upvotes: 5
Views: 24476
Reputation: 1482
So if you have access to your full system, you can change your sudoers file to allow certain sudo commands to be run w/o a password.
On the command line run visudo
Find your user and change the line to look something like this:
pi ALL=(ALL) NOPASSWD: /path/to/kinit, /path/to/another/command
That should do it. Give it another shot!
Hope that helps
Upvotes: 8
Reputation: 10673
You shouldn't pass username and password. This is not secure and it is not going to work if the password is changed.
You can use this:
gksudo kinit # This is going to open a dialog asking for the password.
#sudo kinit # or this if you want to type your password in the terminal
vi hello.txt
Or you can run your script under root. But note that vi is going to be ran as root as well, which means that it will probably create files that belong to root, that might be not what you want.
Upvotes: 4
Reputation: 84453
You can't—at least, not the way you think.
You have a couple of options:
sudo -v
. The credentials will be temporarily cached, giving you time to run your script.One or more of these will definitely get you where you want to go—just not the way you wanted to get there.
Upvotes: 9