Adsar Logo

Cloud Server Scripts

I like to automate the build of cloud servers using good-old shell scripts.

Here's a collection of commands I've found useful of the years...

Set the root password

echo -e "PASSWORD\nPASSWORD" | passwd

Set the hostname (helps to stop sendmail hanging)

hostnamectl set-hostname DOMAINNAME

Move over to CentOS Stream

yum -y install centos-release-stream
yum -y distro-sync

Install MySQL and run mysql_secure_installation

yum -y install mysql-server
service mysqld start
chkconfig mysqld on

mysql_secure_installation <<EOF


Create some swap

fallocate --length 4GiB /swapfile
chown root:root /swapfile
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile

echo '
/swapfile swap swap defaults 0 0
' >> /etc/fstab

Open up HTTP and HTTPS on firewalld

firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload

Hide PHP from webserver headers

sed -i 's/expose_php = On/expose_php = Off/g' /etc/php.ini

Connecting via SSH to other systems - use sshpass

yum -y install sshpass


Get Server external IP

curl -s

Build a MySQL replica

See Percona XtraBackup

Give the MySQL server a unique ID (base it upon the IP)

echo -n 'server_id = ' >> /etc/my.cnf.d/mysql-server.cnf
echo $(curl -s | tr . '\n' | awk '{s = s*256 + $1} END{print s}' >> /etc/my.cnf.d/mysql-server.cnf

Trees for life

Want to get in touch?