Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

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!
#!/bin/bash set -euo pipefail
########################
########################
USERNAME=sammy
authorized_keys file to the new sudoCOPY_AUTHORIZED_KEYS_FROM_ROOT=true
OTHER_PUBLIC_KEYS_TO_ADD=( )
####################
####################
useradd --create-home --shell “/bin/bash” --groups sudo “${USERNAME}”
encrypted_root_pw=“$(grep root /etc/shadow | cut --delimiter=: --fields=2)”
if [ “${encrypted_root_pw}” != “*” ]; then # Transfer auto-generated root password to user if present # and lock the root account to password-based access echo “${USERNAME}:${encrypted_root_pw}” | chpasswd --encrypted passwd --lock root else # Delete invalid password for user if using keys so that a new password # can be set without providing a previous value passwd --delete “${USERNAME}” fi
chage --lastday 0 “${USERNAME}”
home_directory=“$(eval echo ~${USERNAME})” mkdir --parents “${home_directory}/.ssh”
authorized_keys file from root if requestedif [ “${COPY_AUTHORIZED_KEYS_FROM_ROOT}” = true ]; then cp /root/.ssh/authorized_keys “${home_directory}/.ssh” fi
for pub_key in “${OTHER_PUBLIC_KEYS_TO_ADD[@]}”; do echo “${pub_key}” >> “${home_directory}/.ssh/authorized_keys” done
chmod 0700 “${home_directory}/.ssh” chmod 0600 “${home_directory}/.ssh/authorized_keys” chown --recursive “${USERNAME}”:“${USERNAME}” “${home_directory}/.ssh”
sed --in-place ‘s/^PermitRootLogin.*/PermitRootLogin prohibit-password/g’ /etc/ssh/sshd_config if sshd -t -q; then systemctl restart sshd fi
ufw allow OpenSSH ufw --force enable
I have made a bash script to automate the setup process for a newly created droplet, hopefully this will be useful to someone else.
In the “Running the Script After Provisioning” section there is a curl call listed as “curl -L https://raw.githubusercontent.com/do-community/automated-setups/master/Ubuntu-18.04/initial_servers_setup.sh -o /tmp/initial_setup.sh” it should be “curl -L https://raw.githubusercontent.com/do-community/automated-setups/master/Ubuntu-18.04/initial_server_setup.sh -o /tmp/initial_setup.sh” You can see it listed correctly under “Script Contents” -> “click here to view the raw contents directly.”
As it currently is you get “404 Not Found” written to the new tmp file.
I would like to change the script to disable root SSH login and password authentication. Would the following change work?
# Disable root SSH login and password authentication
sed --in-place 's/^PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
sed --in-place 's/^PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config
if sshd -t -q; then
systemctl restart sshd
fi
Wouldn’t it be useful to also include updating to the setup script? Would the following lines – added at the end of the script – work?
apt-get update
apt-get -y upgrade
apt-get -y autoremove
Nice! I’ve made a script of my own with CentOS based on this. The only difference is this line:
useradd --create-home --shell "/bin/bash" --groups wheel "${USERNAME}"
Also, I omitted the firewall part.
There’s a bug in the script.
If OTHER_PUBLIC_KEYS_TO_ADD is left empty, then this line will fail:
chmod 0600 "${home_directory}/.ssh/authorized_keys"
--> chmod: cannot access '/home/sammy/.ssh/authorized_keys': No such file or directory
because .ssh/authorised_keys is created in the previous step ONLY if OTHER_PUBLIC_KEYS_TO_ADD has something:
# Add additional provided public keys
for pub_key in "${OTHER_PUBLIC_KEYS_TO_ADD[@]}"; do
echo "${pub_key}" >> "${home_directory}/.ssh/authorized_keys"
done
ssh root@165.227.208.217 ssh sammy@165.227.208.217 ssh jane_doe@165.227.208.217
after running the script as is, I don’t understand why I’m having trouble using ssh to log in?
I clicking the checkboxes for IPV6 and adding my pre-existing key, but I can’t imagine either of those making a difference
I keep getting Permission denied (publickey).
Permission denied (publickey).
When checking whether the root password has been set:
if [ "${encrypted_root_pw}" != "*" ]; then
should be
if [ "${encrypted_root_pw}" != "*" -a "${encrypted_root_pw}" != "!" ]; then
This is because if root used to have a password and was later locked it will have ! in the shadow file rather than *.
how does someone call this script using user_data attribute? Ive been searching the API docs and all it says is:
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.
I would like an example if that is possible. I’m trying to test this out in postman.