How do I assign a static hostname to a private Amazon EC2 instance running RHEL 7 or Centos 7?

Amazon EC2 instance hostnames are derived from the IP address that is dynamically assigned to the instance at startup. Although you can change the hostname of a private instance of EC2 Linux by using the hostname command, if you reboot or stop/start the instance it will revert to using a hostname derived from the IP address assigned to the instance

For a hostname to be static on RHEL 7 or CentOS 7, you have to perform the below steps.

  1. Edit /etc/hostname and replace the value with the hostname that you want.
  2. Update /etc/hosts file. Change the entry beginning with to include your hostname
  3. Edit /etc/sysconfig/network and append HOSTNAME=xxxxxxx with your hostname
  4. Edit /etc/cloud/cloud.cfg and append the following line at the end of the file without the quotes: “preserve_hostname: true”
  5. Change hostname using hostnamectl command

Your hostname change will be persistent across reboots now.

For more information:


Estimating Web Hosting Needs

To have a website on the Internet, the most basic needs you require are disk space and bandwidth.

Disk space is the amount of data you can store on the web server. Obviously, the amount of space needed depends on the size of your website. Most websites are composed of HTML (text), images, Flash, or a combination of all.

Text is very economical, it occupies very little space. Images and flash are more expensive, as they require more disk space.

Ideally, your web page should stay under 50-60KB, including images and flash. This is because there are some people using slow dial-up connections of under 56Kbps. For these people, a 60KB page will take more than 8 seconds to load. 8 seconds seems like a small number, but imagine you have to sit there and stare at a blank white page for 8 seconds. That’s a lot of time. Use the slow dial-up connection as your reference. If your website loads fast using slow dial-up, it will be even faster for broadband, high-speed connection.

If your average page size is 50KB, you can put approximately 20 pages on 1Mb of space. If you have 100MB of disk space, you can host 200 pages.

Bandwidth is the amount of data you are allowed to transfer to and from your web server per month. This includes all uploads and downloads, both HTTP and FTP. Bandwidth depends on your web page size, as well as the number of visitors to your website and the number of pages they visit.

For an average page size of 50KB, 20,000 visitors per month, and 5 pages per visitor your website will need about 5000MB, or 5GB, of bandwidth per month. Note that 20,000 visitors per month equates to approximately 667 visitors per day. This is a number even most large websites cannot achieve.

If you are just launching your website, chances are you will not need a huge amount of space, nor a huge amount of bandwidth. Unless you have reasons to believe, and evidentiary numbers to support, that your website will have lots of visitors and/or you are offering movie and music downloads, there is little reason for many of us to worry about space and bandwidth. However, in choosing a web host, choose one that allows you to upgrade your disk space and bandwidth if you should need it later on.

You can use the following formula if you need to do a load test, you can determine the number of concurrent users to test if you know daily visits to your site (you may want to use Google Analytics report to find visits per hour):

Concurrent_users = (peak hourly_visits * visit_duration in seconds) / 3600


Adding Swap from Instance Store (Amazon Linux)

Below is the procedure to add a swap from instance-store volumes to Amazon Linux instance.
In this example the instance-store device is /dev/xvdb. Feel free to change it according to your instance-store device.

a. Ensure the Instance includes instance-store volumes or add one instance-store volume to your instance during launch time.

b. SSH into the instance and query for the instance-store:
# df -ah | grep ephemeral
The above command should return output like the following (note this may be different based on your AMI and you have to manually test to see which device is mounted as ephemeral storage):
/dev/xvdb 37G 177M 35G 1% /media/ephemeral0

c. Unmount the mounted instance-store:
# umount /media/ephemeral0

d. Create swap partition:
# mkswap /dev/xvdb

e. Comment out the line for /media/ephemeral0 within /etc/fstab:
#/dev/sdb /media/ephemeral0 auto defaults,nofail,comment=cloudconfig 0 2

f. Activate the swap space:
# swapon /dev/xvdb

g. Check that the swap space is active:
# cat /proc/meminfo | grep Swap
The above command should return output like the following:
SwapCached: 0 kB
SwapTotal: 39313404 kB
SwapFree: 39313404 kB

Should you stop start the instance to move underlying hosts, you will need to re-do the above procedure in order to ensure that you have instance-store configured as swap. You can bypass this by creating a script to prepare swap space so that if the instance is shutdown and then moved to a different underlying host, a pristine instance-store will be converted to swap space again. Adding it inside /etc/fstab will not work as the volume will not be prepared for swap when an instance is stopped and started. Invoking the script within /etc/rc.local would be ideal to perform this. For example:

cat << EOF >> /etc/rc.d/rc.local

# Create swap space – /dev/xvdb
if file /dev/xvdb | grep -q block; then
umount /dev/xvdb;
mkswap /dev/xvdb;
swapon -a /dev/xvdb;