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.
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!
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
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:
- 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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.