{"id":2462,"date":"2018-09-20T14:10:18","date_gmt":"2018-09-20T18:10:18","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=2462"},"modified":"2018-09-20T22:52:32","modified_gmt":"2018-09-21T02:52:32","slug":"installing-memcached-on-centos-7","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2018\/09\/20\/installing-memcached-on-centos-7\/","title":{"rendered":"Installing Memcached on CentOS"},"content":{"rendered":"<h3 id=\"introduction\">Introduction<\/h3>\n<p>Memory object caching systems like Memcached can optimize backend database performance by temporarily storing information in memory, retaining frequently or recently requested records. In this way, they reduce the number of direct requests to your databases.<\/p>\n<p>Because systems like Memcached can contribute to denial of service attacks if improperly configured, it is important to secure your Memcached servers. In this guide, we will cover how to protect your Memcached server by binding your installation to a local or private network interface and creating an authorized user for your Memcached instance.<\/p>\n<h2 id=\"installing-memcached-from-official-repositories\">Installing Memcached from Official Repositories<\/h2>\n<p>If you don&#8217;t already have Memcached installed on your server, you can install it from the official CentOS repositories. First, make sure that your local package index is updated:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum update<\/code><\/pre>\n<p>Next, install the official package as follows:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install memcached<\/code><\/pre>\n<p>We can also install <code>libmemcached<\/code>, a library that provides several tools to work with your Memcached server:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install libmemcached<\/code><\/pre>\n<p>Memcached should now be installed as a service on your server, along with tools that will allow you to test its connectivity. We can now move on to securing its configuration settings.<\/p>\n<h2 id=\"securing-memcached-configuration-settings\">Securing Memcached Configuration Settings<\/h2>\n<p>To ensure that our Memcached instance is listening on the local interface <code>127.0.0.1<\/code>, we will modify the <code>OPTIONS<\/code> variable in the configuration file located at <code>\/etc\/sysconfig\/memcached<\/code>. We will also disable the UDP listener. Both of these actions will protect our server from denial of service attacks.<\/p>\n<p>You can open <code>\/etc\/sysconfig\/memcached<\/code> with <code>nano<\/code>:<\/p>\n<pre class=\"code-pre command\"><code>sudo nano \/etc\/sysconfig\/memcached<\/code><\/pre>\n<p>Locate the <code>OPTIONS<\/code> variable, which will initially look like this:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sysconfig\/memcached\">\/etc\/sysconfig\/memcached<\/div>\n<pre class=\"code-pre \"><code>. . .\r\nOPTIONS=\"\"\r\n<\/code><\/pre>\n<p>Binding to our local network interface will restrict traffic to clients on the same machine. We will do this by adding <code>-l 127.0.0.1<\/code> to our <code>OPTIONS<\/code> variable. This may be too restrictive for certain environments, but it can make a good starting point as a security measure.<\/p>\n<p>Because UDP protocol is much more effective for denial of service attacks than TCP, we can also disable the UDP listener. To do this, we will add the <code>-U 0<\/code> parameter to our <code>OPTIONS<\/code> variable. The file in full should look like this:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sysconfig\/memcached\">\/etc\/sysconfig\/memcached<\/div>\n<pre class=\"code-pre \"><code>\r\nPORT=\"11211\"\r\nUSER=\"memcached\"\r\nMAXCONN=\"1024\"\r\nCACHESIZE=\"64\"\r\nOPTIONS=\"<span class=\"highlight\">-l 127.0.0.1 -U 0<\/span>\" \r\n<\/code><\/pre>\n<p>Save and close the file when you are done.<\/p>\n<p>Restart your Memcached service to apply your changes:<\/p>\n<pre class=\"code-pre command\"><code>sudo systemctl restart memcached<\/code><\/pre>\n<p>Verify that Memcached is currently bound to the local interface and listening only for TCP connections by typing:<\/p>\n<pre class=\"code-pre command\"><code>sudo netstat -plunt<\/code><\/pre>\n<p>You should see the following output:<\/p>\n<p><code><\/code>Output<\/p>\n<pre class=\"code-pre \"><code>Active Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID\/Program name\r\n. . .\r\n<span class=\"highlight\">tcp<\/span>        0      0 <span class=\"highlight\">127.0.0.1<\/span>:11211         0.0.0.0:*               LISTEN      2383\/<span class=\"highlight\">memcached<\/span>\r\n. . .<\/code><\/pre>\n<p>This confirms that <code>memcached<\/code> is bound to the <code>127.0.0.1<\/code> address using only TCP.<\/p>\n<h2 id=\"adding-authorized-users\">Adding Authorized Users<\/h2>\n<p>To add authenticated users to your Memcached service, it is possible to use Simple Authentication and Security Layer (SASL), a framework that de-couples authentication procedures from application protocols. We will enable SASL within our Memcached configuration file and then move on to adding a user with authentication credentials.<\/p>\n<h3 id=\"configuring-sasl-support\">Configuring SASL Support<\/h3>\n<p>We can first test the connectivity of our Memcached instance with the <code>memstat<\/code> command. This will help us establish that SASL and user authentication are enabled after we make changes to our configuration files.<\/p>\n<p>To check that Memcached is up and running, type the following:<\/p>\n<pre class=\"code-pre command\"><code>memstat --servers=\"127.0.0.1\"<\/code><\/pre>\n<p>You should see output like the following:<\/p>\n<p><code><\/code>Output<\/p>\n<pre class=\"code-pre \"><code>Server: 127.0.0.1 (11211)\r\n     pid: 3831\r\n     uptime: 9\r\n     time: 1520028517\r\n     version: 1.4.25\r\n     . . .<\/code><\/pre>\n<p>Now we can move on to enabling SASL. First, we can add the <code>-S<\/code> parameter to our <code>OPTIONS<\/code> variable in <code>\/etc\/sysconfig\/memcached<\/code>, which will enable SASL. Open the file again:<\/p>\n<pre class=\"code-pre command\"><code>sudo nano \/etc\/sysconfig\/memcached<\/code><\/pre>\n<p>We will add both the <code>-S<\/code> and <code>-vv<\/code> parameters to our <code>OPTIONS<\/code> variable. The <code>-vv<\/code> option will provide verbose output to <code>\/var\/log\/memcached<\/code>, which will help us as we debug. Add these options to the <code>OPTIONS<\/code> variable as follows:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sysconfig\/memcached\">\/etc\/sysconfig\/memcached<\/div>\n<pre class=\"code-pre \"><code>. . .\r\nOPTIONS=\"<span class=\"highlight\">-l 127.0.0.1 -U 0 -S -vv<\/span>\" \r\n<\/code><\/pre>\n<p>Save and close the file.<\/p>\n<p>Restart the Memcached service:<\/p>\n<pre class=\"code-pre command\"><code>sudo systemctl restart memcached<\/code><\/pre>\n<p>Next, we can take a look at the logs to be sure that SASL support has been enabled:<\/p>\n<pre class=\"code-pre command\"><code>sudo journalctl -u memcached<\/code><\/pre>\n<p>You should see the following line, indicating that SASL support has been initialized:<\/p>\n<p><code><\/code>Output<\/p>\n<pre class=\"code-pre \"><code>. . .\r\nMar 05 18:16:11 memcached-server memcached[3846]: Initialized SASL.\r\n. . .<\/code><\/pre>\n<p>We can check the connectivity again, but because SASL has been initialized, this command should fail without authentication:<\/p>\n<pre class=\"code-pre command\"><code>memstat --servers=\"127.0.0.1\"<\/code><\/pre>\n<p>This command should not produce output. We can type the following to check its status:<\/p>\n<pre class=\"code-pre command\"><code>echo $?<\/code><\/pre>\n<p><code>$?<\/code> will always return the exit code of the last command that exited. Typically, anything besides <code>0<\/code> indicates process failure. In this case, we should see an exit status of <code>1<\/code>, which tells us that the <code>memstat<\/code> command failed.<\/p>\n<h3 id=\"adding-an-authenticated-user\">Adding an Authenticated User<\/h3>\n<p>Now we can download two packages that will allow us to work with the Cyrus SASL Library and its authentication mechanisms, including plugins that support <em>PLAIN<\/em> authentication schemes. These packages, <code>cyrus-sasl-devel<\/code> and <code>cyrus-sasl-plain<\/code>, will allow us to create and authenticate our user. Install the packages by typing:<\/p>\n<pre class=\"code-pre command\"><code>sudo yum install cyrus-sasl-devel cyrus-sasl-plain<\/code><\/pre>\n<p>Next, we will create the directory and file that Memcached will check for its SASL configuration settings:<\/p>\n<pre class=\"code-pre command\"><code>sudo mkdir -p \/etc\/sasl2<\/code><\/pre>\n<pre class=\"code-pre command\"><code>sudo nano \/etc\/sasl2\/memcached.conf <\/code><\/pre>\n<p>Add the following to the SASL configuration file:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sasl2\/memcached.conf\">\/etc\/sasl2\/memcached.conf<\/div>\n<pre class=\"code-pre \"><code>mech_list: plain\r\nlog_level: 5\r\nsasldb_path: \/etc\/sasl2\/memcached-sasldb2\r\n<\/code><\/pre>\n<p>In addition to specifying our logging level, we will set <code>mech_list<\/code> to <code>plain<\/code>, which tells Memcached that it should use its own password file and verify a plaintext password. We will also specify the path to the user database file that we will create next. Save and close the file when you are finished.<\/p>\n<p>Now we will create a SASL database with our user credentials. We will use the <code>saslpasswd2<\/code> command to make a new entry for our user in our database using the <code>-c<\/code> option. Our user will be <strong>sammy<\/strong> here, but you can replace this name with your own user. Using the <code>-f<\/code> option, we will specify the path to our database, which will be the path we set in <code>\/etc\/sasl2\/memcached.conf<\/code>:<\/p>\n<pre class=\"code-pre command\"><code>sudo saslpasswd2 -a memcached -c -f \/etc\/sasl2\/memcached-sasldb2 <span class=\"highlight\">sammy<\/span><\/code><\/pre>\n<p>Finally, we want to give the <code>memcached<\/code> user ownership over the SASL database:<\/p>\n<pre class=\"code-pre command\"><code>sudo chown memcached:memcached \/etc\/sasl2\/memcached-sasldb2<\/code><\/pre>\n<p>Restart the Memcached service:<\/p>\n<pre class=\"code-pre command\"><code>sudo systemctl restart memcached<\/code><\/pre>\n<p>Running <code>memstat<\/code> again will confirm whether or not our authentication process worked. This time we will run it with our authentication credentials:<\/p>\n<pre class=\"code-pre command\"><code>memstat --servers=\"127.0.0.1\" --username=<span class=\"highlight\">sammy<\/span> --password=<span class=\"highlight\">your_password<\/span><\/code><\/pre>\n<p>You should see output like the following:<\/p>\n<p><code><\/code>Output<\/p>\n<pre class=\"code-pre \"><code>Server: 127.0.0.1 (11211)\r\n     pid: 3831\r\n     uptime: 9\r\n     time: 1520028517\r\n     version: 1.4.25\r\n     . . .<\/code><\/pre>\n<p>Our Memcached service is now successfully running with SASL support and user authentication.<\/p>\n<h2 id=\"allowing-access-over-the-private-network\">Allowing Access Over the Private Network<\/h2>\n<p>We have covered how to configure Memcached to listen on the local interface, which can prevent denial of service attacks by protecting the Memcached interface from exposure to outside parties. There may be instances where you will need to allow access from other servers, however. In this case, you can adjust your configuration settings to bind Memcached to the private network interface.<\/p>\n<h3 id=\"limiting-ip-access-with-firewalls\">Limiting IP Access With Firewalls<\/h3>\n<p>Before you adjust your configuration settings, it is a good idea to set up firewall rules to limit the machines that can connect to your Memcached server. If you followed the prerequisites and installed FirewallD on your server and do <strong>not<\/strong> plan on connecting to Memcached from another host, then you do not need to adjust your firewall rules. Your standalone Memcached instance should be listening on <code>127.0.0.1<\/code>, thanks to the <code>OPTIONS<\/code> variable we defined earlier, and there should therefore be no concerns about incoming traffic. If you plan to allow access to your Memcached server from other hosts, however, then you will need to make changes to your firewall settings using the <code>firewall-cmd<\/code> command.<\/p>\n<p>Begin by adding a dedicated Memcached zone to your <code>firewalld<\/code> policy:<\/p>\n<pre class=\"code-pre command\"><code>sudo firewall-cmd --permanent --new-zone=memcached\r\n<\/code><\/pre>\n<p>Then, specify which port you would like to keep open. Memcached uses port <code>11211<\/code> by default:<\/p>\n<pre class=\"code-pre command\"><code>sudo firewall-cmd --permanent --zone=memcached --add-port=11211\/tcp<\/code><\/pre>\n<p>Next, specify the private IP addresses that should be allowed to access Memcached. For this, you will need to know your <strong>client server&#8217;s private IP address<\/strong>:<\/p>\n<pre class=\"code-pre command\"><code>sudo firewall-cmd --permanent --zone=memcached --add-source=<span class=\"highlight\">client_server_private_IP<\/span><\/code><\/pre>\n<p>Reload the firewall to ensure that the new rules take effect:<\/p>\n<pre class=\"code-pre command\"><code>sudo firewall-cmd --reload<\/code><\/pre>\n<p>Packets from your client&#8217;s IP address should now be processed according to the rules in the dedicated Memcached zone. All other connections will be processed by the default <code>public<\/code> zone.<\/p>\n<p>With these changes in place, we can move on to making the necessary configuration changes to our Memcached service, binding it to our server&#8217;s private networking interface.<\/p>\n<h3 id=\"binding-memcached-to-the-private-network-interface\">Binding Memcached to the Private Network Interface<\/h3>\n<p>The first step in binding to our server&#8217;s private networking interface will be modifying the <code>OPTIONS<\/code> variable we set earlier.<\/p>\n<p>We can open <code>\/etc\/sysconfig\/memcached<\/code> again by typing:<\/p>\n<pre class=\"code-pre command\"><code>sudo nano \/etc\/sysconfig\/memcached<\/code><\/pre>\n<p>Inside, locate the <code>OPTIONS<\/code> variable. We can now modify <code>-l 127.0.0.1<\/code> to reflect our Memcached server&#8217;s private IP:<\/p>\n<div class=\"code-label \" title=\"\/etc\/sysconfig\/memcached\">\/etc\/sysconfig\/memcached<\/div>\n<pre class=\"code-pre \"><code>. . .\r\nOPTIONS=\"-l <span class=\"highlight\">memcached_servers_private_IP<\/span> -U 0 -S -vv\"\r\n<\/code><\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<p>Restart the Memcached service again:<\/p>\n<pre class=\"code-pre command\"><code>sudo systemctl restart memcached<\/code><\/pre>\n<p>Check your new settings with <code>netstat<\/code> to confirm the change:<\/p>\n<pre class=\"code-pre command\"><code>sudo netstat -plunt<\/code><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Output\">Output<\/div>\n<pre class=\"code-pre \"><code>Active Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID\/Program name\r\n. . .\r\n<span class=\"highlight\">tcp<\/span>        0      0 <span class=\"highlight\">memcached_servers_private_IP<\/span>:11211         0.0.0.0:*               LISTEN      2383\/<span class=\"highlight\">memcached<\/span><\/code><\/pre>\n<p>Test connectivity from your external client to ensure that you can still reach the service. It is a good idea to also check access from a non-authorized client to ensure that your firewall rules are effective.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Memory object caching systems like Memcached can optimize backend database performance by temporarily storing information in memory, retaining frequently or recently requested records. In this way, they reduce the number of direct requests to your databases. Because systems like Memcached can contribute to denial of service attacks if improperly configured, it is important to &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2018\/09\/20\/installing-memcached-on-centos-7\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Installing Memcached on CentOS<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2464,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Installing Memcached on CentOS - HP Server","description":"Introduction Memory object caching systems like Memcached can optimize backend database performance by temporarily storing information in memory, retaining freq"},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=2462"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/2462\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media\/2464"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}