Difference between revisions of "Startup scripts"

From Luna Node
Jump to: navigation, search
(Created page with "Startup scripts can be defined from the [https://dynamic.lunanode.com/panel/scripts.php Startup Scripts] sidebar item. These scripts can then be selecting when provisioning ne...")
 
Line 14: Line 14:
 
* [http://cloudinit.readthedocs.org/en/latest/topics/format.html cloud-config script format]
 
* [http://cloudinit.readthedocs.org/en/latest/topics/format.html cloud-config script format]
 
* [http://cloudinit.readthedocs.org/en/latest/topics/examples.html Example cloud-config scripts]
 
* [http://cloudinit.readthedocs.org/en/latest/topics/examples.html Example cloud-config scripts]
 +
 +
== Example scripts ==
 +
 +
Set the root password:
 +
 +
<nowiki>#!/bin/bash
 +
password=$(cut -d':' -f2 /etc/shadow | tail -n 1 | sed -e 's/[\/&]/\\&/g')
 +
sed -i 's/root:[^:]*:/root:'"$password"':/' /etc/shadow
 +
username=$(ls /home/)
 +
deluser "$username"
 +
rm -rf /home/"$username"
 +
sed -i 's/PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
 +
service ssh restart
 +
service sshd restart</nowiki>

Revision as of 03:19, 25 February 2015

Startup scripts can be defined from the Startup Scripts sidebar item. These scripts can then be selecting when provisioning new virtual machines, and selected scripts will execute when the new instance boots for the first time.

Shell scripts

Shell scripts (such as scripts for bash) start with "#!" on the first line, generally followed by a path to the shell binary. For example, this bash script will update packages on Debian-based operating systems:

#!/bin/bash
apt-get update; apt-get upgrade -y

cloud-config scripts

cloud-config scripts can be used on images that have cloud-init installed (this includes the default template images). These can more reliably configure operating system parameters than shell scripts, and are generally easier to write.

Example scripts

Set the root password:

#!/bin/bash
password=$(cut -d':' -f2 /etc/shadow | tail -n 1 | sed -e 's/[\/&]/\\&/g')
sed -i 's/root:[^:]*:/root:'"$password"':/' /etc/shadow
username=$(ls /home/)
deluser "$username"
rm -rf /home/"$username"
sed -i 's/PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
service ssh restart
service sshd restart