Question

Setting a droplet password via API

I’m trying to create a Droplet via the API. Is there a way to set an SSH password? I would rather not use SSH keys.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
March 19, 2023
Accepted Answer

Hi there,

I don’t think that this is available out of the box, but what you could do is to pass a user_data parameter with a shell script or a ‘cloud-config’ configuration with

A string containing ‘user data’ which may be used to configure the Droplet on first boot, often a ‘cloud-config’ file or Bash script. It must be plain text and may not exceed 64 KiB in size.

Here is an example of such a Bash script:

#!/bin/bash

# Enable SSH password authentication and root login
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config

# Restart SSH service
systemctl restart sshd

# Set a root password
echo "root:<your-password-here>" | chpasswd

This script will run on the initial Droplet boot up.

For the API call, you might have to convert this into 1 liner, eg:

#/bin/bash ; sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g;s/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config && systemctl restart sshd && echo "root:<your-password-here>" | chpasswd

Hope that this helps!

Best,

Bobby

alexdo
Site Moderator
Site Moderator badge
March 21, 2023

Hello @trentseal

You can use Bobby’s suggestion to pass the user_data parameter to set the root password.

If you are creating a Droplet with the doctl command line tool, you can pass in the script using the --user-data-file option:

  1. doctl compute droplet create ... --user-data-file /path/to/script

If you do not want to use user data, you can also run the script manually over SSH once the server is booted up.

I’ll still recommend using ssh-keys as it’s more secure and you can also limit the access to the droplet to certain IP addresses as an additional step to tighten up the security.

Regards

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.