I want an environment variable that specifies the Droplet’s domain name to be set at boot even if the VM’s image is reinstalled. Do I need to destroy this droplet and create a new one, or can I modify my existing droplet?
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!
Heya,
I’ll recommend you to check the cloud-init stuff - https://docs.digitalocean.com/tutorials/droplet-cloudinit/
Cloud-init is an industry standard tool that allows you to automate the initialization of your Linux instances. This means that you can use cloud-init to inject a file into your Droplets at deployment that automatically sets up things like new users, firewall rules, app installations, and SSH keys. DigitalOcean’s User Data feature leverages cloud-init so you can deploy and automate the set up of several Droplets simultaneously. Learning to use cloud-init can save you a lot of set up time when deploying new Droplets.
Hi there,
Yes, you can modify an already deployed Droplet without destroying it or re-create it.
This could be done in a few different ways depending on the exact requirements, but here are a few examples:
Use /etc/environment: This is a system-wide configuration file to be used by all processes. You can append your environment variable here:
echo 'DOMAIN_NAME="yourdomain.com"' | sudo tee -a /etc/environment
Remember to load the environment after setting:
source /etc/environment
Startup Scripts with systemd: You can use systemd to create custom startup services that run your script at boot:
Create a script, for example, /usr/local/bin/set-domain.sh:
#!/bin/bash
export DOMAIN_NAME="yourdomain.com"
Make the script executable: chmod +x /usr/local/bin/set-domain.sh
Create a systemd service unit for this script, for example, /etc/systemd/system/set-domain.service:
[Unit]
Description=Set domain name environment variable
[Service] ExecStart=/usr/local/bin/set-domain.sh
[Install]
WantedBy=multi-user.target
Enable and start the service:
systemctl enable set-domain
systemctl start set-domain
Alternatively, if you’ve used cloud-init for your initial setup, you can modify the cloud-init configuration. However, changing cloud-init scripts on an already running Droplet can be tricky since cloud-init is primarily designed for the initial setup.
Hope that this helps!
Best,
Bobby
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.