Changing ROOT Password with Bash Scripting

Posted April 20, 2015 7.7k views
UbuntuApacheGetting Started

So you know when you spin up a new droplet and you first SSH into it and it asks you for the current password, then to change the password [twice]? Could this be done automatically say via a Bash/Shell script?

I’m looking to automate the process to where nobody has to SSH into a droplet and change it manually.

Surely possible, I just don’t know much about bash/shell scripting.


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

The quickest solution would be to set up SSH keys on your account.

With this in place any client with the ssh private key will be able to make an ssh connection to your droplet without the need for a password and this password change prompt is never shown.

Alternately you could use the user-data section on the create screen to pass bash commands to your droplet on creation to set the root password. A few methods to accomplish this can be found here.

  • Thanks for that. I do plan on using SSH keys for ‘Managed’ customers, as they won’t be even touching the shell, just me.

    That link is helpful, I just need to wrap my head around the way it works. As I say, I don’t know how to code bash.

    Thanks for the advise!

  • At it’s simplest, writing a bash script is simply placing the commands you want to run in a file. For example, the script below could be dropped into user-data when creating a droplet and would create a file called “hello” in the /root directory with the content “Hello World”:

    echo "Hello World" > /root/hello;

    The couple differences between this and just typing into a command prompt are:


    This line tells the shell that the code below is to be interpreted by the program at /bin/bash (the bash shell).

    echo "Hello World" > /root/hello;

    Two things to note here. The ’>’ tells bash to run the command before it and write the content to the file specified after. The ’>>’ operator is also available which would instead append the content to the file (rather than clearing any existing content).

    Second, each line ends with a semi-colon. This simply tells bash that this is the end of this particular command.

    You can find more basics, and get into the details of what you can do with bash scripts here.

I’m still confused here. How can I change the root pw via Bash so i don’t need to SSH in and do it manually or use SSH keys

Thank you, I appreciate the write up!