How to strip down Amazon Linux EC2 to maximize available RAM

Keeping memory usage low is especially important on these smaller machines to ensure optimal performance and stability. This guide is for low-memory Amazon Linux EC2 instance types like t4g.nano and t4g.micro.

Datadog - Visualize and alert on Linux server metrics in real-time.

The recommendations are most relevant to Amazon Linux 2, Amazon Linux 2023, RHEL 9, and other derivatives including Fedora and Alma Linux.

This article was originally written in 2016 for Amazon Linux Version 1. It has been updated a few times, and the latest update was on December 30th, 2024. While many steps remain relevant across these distributions, some adjustments reflect the latest best practices.

So, without further ado, let’s get started!

Update ALL packages with dnf

First, log in to your EC2 instance and update all installed packages:

sudo dnf update -y

If you are not using a Mail Transfer Agent (MTA), consider disabling it to save memory. Postfix is installed by default on Amazon Linux 2 and Amazon Linux 2023. To disable it, run:

sudo systemctl disable postfix
sudo systemctl stop postfix

Optimize Getty Services

Update: AWS now installs mingetty by default.

Amazon Linux 2, AL2023, RHEL 9, and derivatives use systemd instead of init scripts. To reduce the number of active virtual terminals, edit /etc/systemd/logind.conf and set:

NAutoVTs=1
ReserveVT=1

Then restart the logind service:

sudo systemctl restart systemd-logind

Disable IPv6 Support

If IPv6 is unnecessary for your application, disable it to save resources. Edit /etc/sysctl.conf and add:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

Apply the settings:

sudo sysctl -p

Disable NTP Daemon (chronyd) and Replace with Cron

Amazon Linux 2, Amazon Linux 2023, and RHEL 9-based systems default to chronyd, which provides continuous time synchronization with minimal resource usage. It is generally recommended to leave chronyd running, as accurate timekeeping is critical for web servers, SSL/TLS, logging, and authentication.

However, if you are extremely constrained on memory and can tolerate potential time drift, you may disable chronyd and replace it with a lightweight alternative like systemd-timesyncd or a cron job.

Using systemd-timesyncd (Recommended Lightweight Option):

sudo systemctl disable chronyd
sudo systemctl stop chronyd
sudo systemctl enable systemd-timesyncd
sudo systemctl start systemd-timesyncd

Using a Weekly ntpdate Cron Job (Not Recommended):

sudo dnf install -y ntpdate
sudo bash -c 'echo -e "#!/bin/bash\n/usr/sbin/ntpdate -u pool.ntp.org" > /etc/cron.weekly/ntpdate-sync'
sudo chmod +x /etc/cron.weekly/ntpdate-sync

Run the cron job manually to verify:

sudo /etc/cron.weekly/ntpdate-sync

Note: Using ntpdate sacrifices continuous time synchronization, which may cause time drift and impact applications relying on accurate time.

Tune Swappiness and Cache Pressure

To optimize memory usage, adjust the system’s swappiness and cache pressure. Edit /etc/sysctl.conf and add:

vm.swappiness=10
vm.vfs_cache_pressure=200

Apply the settings immediately:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=200

Check current values to verify changes:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure

Also, read: Kernel cache pressure and swappiness.

Add Swap Space

For low-memory instances, adding swap space is essential. To add a 1 GB swap file:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

To make the swap permanent, edit /etc/fstab and add:

/swapfile swap swap defaults 0 0

Reboot and verify swap is enabled:

free -h

Also see: Linux Performance: Almost Always Add Swap Space.

Conclusion

These steps will reduce the memory usage of your Amazon Linux 2 and Amazon Linux 2023 based EC2 instances, or RHEL 9 and its derivatives. Making them more suitable for low-memory types like t4g.nano. Keep in mind that tuning should be aligned with your workload requirements.

If possible, monitor the instance using tools like top, htop, or glances. For more advanced tips, see Strip Down Apache to Improve Performance & Memory Efficiency.

Original article archived

Here’s a copy of the original article from 2016 for those with legacy servers/needs:

Update all packages – yum package manager

First, login to your new Amazon Linux EC2 instance and update all packages:

sudo yum update

Then if you are not going to use MTA (mail transfer agent) you can disable sendmail service:

sudo chkconfig sendmail off
sudo service sendmail stop

Reduce the number of getty services

Edit /etc/sysconfig/init and replace:

ACTIVE_CONSOLES=/dev/tty[1-6]

with…

ACTIVE_CONSOLES=/dev/tty[1-1]

Replace with agetty with mingetty

Update! – AWS now installs mingetty by default. In which case if the output of the install command is “already installed”, then simply make the line change to the /etc/init/serial.conf file as described below.

Since agetty is heavier on RAM, lets replace with mingetty.

Install mingetty first:

sudo yum install mingetty

then edit /etc/init/serial.conf and replace:

exec /sbin/agetty /dev/$DEV $SPEED vt100-nav

with

exec /sbin/mingetty /dev/$DEV $SPEED vt100-nav

Disable yum-updatesd and replace it with a simple cron job

Update! – AWS no longer installs yum-updatesd by default. You can still setup the cron if you’d like. Check to see if yum-updatesd is installed using this command to list: chkconfig

This will save resident memory.

sudo chkconfig yum-updatesd off

or

sudo yum remove yum-updatesd

Next, create yum update cron instead. Add a new file /etc/cron.daily/yum.cron with contents:

#!/bin/sh 
/usr/bin/yum -R 120 -e 0 -d 0 -y update yum 
/usr/bin/yum -R 10 -e 0 -d 0 -y update

Followed by:

sudo chmod +x /etc/cron.daily/yum.cron

Disable IPv6 support

sudo chkconfig ip6tables off

Disable Network Time Protocol (NTP) daemom

Next, disable ntpd. Run “top” and press shift + M to sort by memory usage, you’ll notice that ntpd is close to the top of the list. Its used to keep your server clock in sync. You can replace with weekly cron so you can disable the service and further reduce memory usage.

sudo service ntpd stop
sudo chkconfig ntpd off
sudo chkconfog ntpdate off

Now add a new file named ntpdate-sync to the /etc/cron.weekly directory with the contents:

#! /bin/sh
/usr/sbin/ntpdate pool.ntp.org

Followed by:

sudo chmod +x /etc/cron.weekly/ntpdate-sync

To test run:

sudo /etc/cron.weekly/ntpdate-sync

Output should be something like:

11 Oct 22:57:49 ntpdate[1174]: adjust time server 97.107.134.213 offset -0.017816 sec

These are some basic steps to lower memory consumption of first boot. More noticeable on the smaller Amazon Linux EC2 instances.

Tuning Amazon Linux EC2 swappiness and cache pressure

Another method of squeezing the most from your Amazon Linux EC2’s limited RAM, is to tune the system’s swappiness (tendency to swap) and cache pressure (tendency to reclaim cache).

swappiness (Recommended value 10 to 60. 0 if you don’t have swap added) – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. (default = 60)

vfs_cache_pressure (Recommend value 50 to 200) – Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. (default = 100)

Add these lines to the end of the /etc/sysctl.conf file.

vm.swappiness=10
vm.vfs_cache_pressure=200

Increasing the cache pressure may be somewhat counter productive since caching is good for performance. However, swapping too often can also reduce your server’s overall performance. Use this for example if free -m shows say more than 60% of RAM being used by cache/buffers …remember this NOT a bad thing! However, depending on what is being cached you may reduce memory usage/contention and as a result gain performance by adjusting.

To check current values using these commands:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

To enable these settings without rebooting use the following commands:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=200

How To Add Swap on Amazon Linux EC2

Of course, on low-memory instances swap is wise. To add a 1GB swap file for example, from command line you’ll type:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Now setup the swap file with the command:

sudo mkswap /swapfile
sudo chmod 600 /swapfile

Now enable the swap:

sudo swapon /swapfile

If you use the top command, you should now see the 1gb swap added. So now lets make swap persistent so it’s not dropped when you reboot. Edit /etc/fstab file and add this line as the last line:

/swapfile swap swap defaults 0 0

When you reboot, use the free -h or df -h command to check for swap.

Remember, adding swap can help save your server from running out of memory but if it’s already using a big chunk of swap (aka swapping), that is never good for performance. A lot can be expanded upon with regards to swap and paging/swapping. However, the point today is that stripping/tuning the AMI.

Note: this article was originally published on Nov 21, 2013. It has been updated to ensure that the suggested changes are still compatible.

Tags: , , , ,

Discussion

  1. This article was so outdated. Tried my best to update it.



Top ↑