Tutorials  /  Ubuntu

Install and Secure Memcached with SASL on Ubuntu 20.04

Ccentron Redaktion · October 2025 ·6 min read ·Ubuntu, Tutorial

Memcached is a free, distributed memory caching system that stores frequently accessed data and objects—such as database queries, API responses, session information, or computation results—in RAM. By reducing repetitive operations like database lookups, it helps improve application performance and lower server load.

This guide explains how to install Memcached on Ubuntu 20.04 and configure it to use SASL (Simple Authentication and Security Layer) for secure client connections.

Prerequisites

  • An Ubuntu 20.04 server
  • SSH access as a non-root user with sudo privileges
  • Ensure the server is updated
VM

Matching infrastructure at centron

Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Install Memcached

Memcached is available in Ubuntu 20.04’s default package repositories and can be installed using APT. You can also compile a specific version from source if needed. The following steps describe installing Memcached and enabling it to run automatically on your server.

Update the package index

Console
$ sudo apt update

Install Memcached and required tools

Console
$ sudo apt install memcached libmemcached-tools -y

Check the installed Memcached version

Console
$ memcached --version

Example output:

Code
memcached 1.5.22

Enable Memcached to start at boot

Console
$ sudo systemctl enable memcached

Start the Memcached service

Console
$ sudo systemctl start memcached

Configure Memcached

The configuration file for Memcached is /etc/memcached.conf. Here you can define options such as port number, memory size, connection limits, and the IP address that Memcached listens on. Follow these steps to adjust the settings and confirm that Memcached is running properly.

Edit the configuration file

Console
$ sudo nano /etc/memcached.conf

Enable SASL authentication

Add -S at the end of the file after -P /var/run/memcached/memcached.pid to enable SASL:

Code
...
-P /var/run/memcached/memcached.pid
-S

Enable verbose logging

Code
-v

Limit simultaneous connections

Code
-c 1024

Check the Memcached port

Console
# Default connection port is 11211
-p 11211

Restrict or allow remote access

Ensure the -l directive is set to 127.0.0.1 to limit access to local connections. To allow remote access, update it to your server’s public IP:

Code
-l 127.0.0.1
-l ::1

Apply configuration changes

Console
$ sudo systemctl restart memcached

Secure Memcached with SASL

By default, Memcached does not enforce authentication. Enabling SASL ensures that only authenticated users can access it. Follow these steps to add this security layer.

Install the SASL package

Console
$ sudo apt install sasl2-bin -y

Create a directory for SASL credentials

Console
$ sudo mkdir /etc/sasl2

Create a SASL configuration file

Console
$ sudo nano /etc/sasl2/memcached.conf

Add the following content:

Code
log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2

This configuration:

  • log_level: Enables detailed logging at level 5.
  • mech_list: Specifies authentication method; plain allows simple username and password authentication.
  • sasldb_path: Indicates the location of the Memcached SASL database for authentication.

Create a SASL user

Replace example-user with your desired username:

Console
$ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example-user

Set a strong password when prompted.

Assign correct ownership to the SASL database

Console
$ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2

Restart Memcached to apply changes

Console
$ sudo systemctl restart memcached

Check the Memcached service status

Console
$ sudo systemctl status memcached

List all SASL users

Console
$ sudo sasldblistusers2 -f /etc/sasl2/memcached-sasldb2

Example output:

Code
example-user@ubuntu20: userPassword

Test the new user credentials

Replace example-user@ubuntu20 and strong-password with your actual details:

Console
$ memcstat --binary --servers="127.0.0.1" --username='example-user@ubuntu20' --password=strong-password

If the configuration is correct, you should see output similar to this:

YAML
Server: 127.0.0.1 (11211)
    pid: 5090
    uptime: 570
    time: 1743878810
    version: 1.5.22
    libevent: 2.1.11-stable
    pointer_size: 64
    rusage_user: 0.052240
    rusage_system: 0.043533
    max_connections: 1024
    curr_connections: 2
    total_connections: 10
    rejected_connections: 8

Connect to Memcached

Memcached works with many application frameworks such as PHP, Perl, Python, Ruby, and Java. To test connectivity with PHP, follow these steps.

Install PHP and the Memcached module

Console
$ sudo apt install php php-memcached -y

Create a sample PHP script

Console
$ nano memcached.php

Add the following PHP code. Replace example-user@ubuntu20 and strong-password with your own credentials:

Code
<!--?php $memcached = new Memcached(); $memcached--->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->addServer('127.0.0.1', 11211);
$memcached->setSaslAuthData('example-user@ubuntu20', 'strong-password');

// Set and retrieve a value to test the connection
$memcached->set('example', 'Greetings from centron!');
echo $memcached->get('example');
?>

This script connects to Memcached using the specified user credentials and binary protocol. It creates a new key named example with the value “Greetings from centron!” and then retrieves it from memory.

Run the PHP script

Console
$ php memcached.php

Example output:

Code
Greetings from centron!

The output confirms that the PHP script successfully connected to Memcached, stored the key example, and retrieved the value “Greetings from centron!”.

Conclusion

You have installed Memcached on Ubuntu 20.04 and configured it to securely integrate with application frameworks such as PHP. By caching frequently used data like database queries in memory, Memcached helps improve server performance and reduce load.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Ubuntu
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren