Search code examples
linuxbashfile-permissionssudo

How to supply sudo with password from script?


Please note: this is a guest VM (VBox) running on my local machine, and I'm not worried about security.

I am writing a script that will be executed on a Linux (Ubuntu) VM as the myuser user. This script will create a very large directory tree under /etc/myapp. Currently I have to do all this manually, and it starts with me giving myuser recrusive rwx permissions under /etc like so:

sudo chmod -R 777 /etc
[sudo] password for myuser:  <now I enter the password and hit ENTER>

My question: how do I write a bash script that supplies the sudo command with my password so that I can just execute bash myscript.sh and it will make the necessary permission changes for me?


Solution

  • If, as you say, you completely don't care about security...

    Run visudo to edit /etc/sudoers with validation in place. Add the following line:

    ALL ALL=(ALL) NOPASSWD: ALL
    

    This will prevent sudo from ever asking for a password, for any user, for any command.