One place for hosting & domains

      Redis

      Cara Menginstal dan Mengamankan Redis pada Ubuntu 20.04


      Versi sebelumnya dari tutorial ini ditulis oleh Justin Ellingwood

      Pengantar

      Redis adalah penyimpanan nilai kunci dalam memori yang dikenal karena fleksibilitas, kinerja, dan dukungan bahasanya yang luas. Tutorial ini mendemonstrasikan cara menginstal, mengonfigurasi, dan mengamankan Redis pada server Ubuntu 20.04.

      Prasyarat

      Untuk menyelesaikan panduan ini, Anda akan memerlukan akses ke server Ubuntu 20.04 yang memiliki pengguna non-root dengan privilese sudo dan firewall yang terkonfigurasi dengan ufw. Anda dapat menyiapkan ini dengan mengikuti Panduan Penyiapan Server Awal untuk Ubuntu 20.04 dari kami.

      Langkah 1 — Menginstal dan Mengonfigurasi Redis

      Kita akan menggunakan manajer paket APT untuk menginstal redis dari repositori Ubuntu resmi. Pada saat penulisan panduan ini, versi yang tersedia dalam repositori asali adalah 5.0.7.

      Mulailah dengan memperbarui cache paket apt lokal Anda:

      Lalu instal Redis dengan mengetik:

      • sudo apt install redis-server

      Ini akan mengunduh dan menginstal Redis serta dependensinya. Setelah ini, ada satu perubahan konfigurasi penting yang harus dibuat di dalam berkas konfigurasi Redis, yang dihasilkan secara otomatis selama instalasi.

      Buka berkas ini dengan editor teks pilihan Anda:

      • sudo nano /etc/redis/redis.conf

      Di dalam berkas itu, temukan arahan supervised. Arahan ini memungkinkan Anda untuk mendeklarasikan sistem init untuk mengelola Redis sebagai suatu layanan, yang memberi Anda kontrol lebih atas operasinya. Arahan supervised diatur sebagai no secara asali. Karena Anda menjalankan Ubuntu, yang menggunakan sistem init systemd, ubah ini menjadi systemd:

      /etc/redis/redis.conf

      . . .
      
      # If you run Redis from upstart or systemd, Redis can interact with your
      # supervision tree. Options:
      #   supervised no      - no supervision interaction
      #   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
      #   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
      #   supervised auto    - detect upstart or systemd method based on
      #                        UPSTART_JOB or NOTIFY_SOCKET environment variables
      # Note: these supervision methods only signal "process is ready."
      #       They do not enable continuous liveness pings back to your supervisor.
      supervised systemd
      
      . . .
      

      Itu adalah satu-satunya perubahan yang Anda perlu buat terhadap berkas konfigurasi Redis saat ini, jadi simpan dan tutup berkasnya ketika Anda selesai. Jika Anda menggunakan nano untuk mengedit berkas, lakukan itu dengan menekan CTRL + X, Y, lalu ENTER.

      Lalu, mulai ulang layanan Redis untuk mencerminkan perubahan yang Anda buat ke berkas konfigurasi:

      • sudo systemctl restart redis.service

      Dengan itu, Anda telah menginstal dan mengonfigurasi Redis dan Redis berjalan pada mesin Anda. Sebelum Anda mulai menggunakannya, merupakan hal bijaksana untuk memeriksa terlebih dahulu apakah Redis berfungsi dengan benar.

      Langkah 2 — Menguji Redis

      Seperti perangkat lunak yang baru terinstal lainnya, sebaiknya pastikan bahwa Redis berfungsi seperti yang diharapkan sebelum membuat perubahan apa pun terhadap konfigurasinya. Kita akan membahas beberapa cara untuk memeriksa bahwa Redis bekerja dengan benar dalam langkah ini.

      Mulai dengan memeriksa bahwa layanan Redis sedang berjalan:

      • sudo systemctl status redis

      Jika Redis berjalan tanpa kesalahan apa pun, perintah ini akan menghasilkan keluaran yang mirip dengan berikut ini:

      Output

      ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-04-30 23:26:54 UTC; 4s ago Docs: http://redis.io/documentation, man:redis-server(1) Process: 36552 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS) Main PID: 36561 (redis-server) Tasks: 4 (limit: 2345) Memory: 1.8M CGroup: /system.slice/redis-server.service └─36561 /usr/bin/redis-server 127.0.0.1:6379 . . .

      Di sini, Anda dapat melihat bahwa Redis sedang berjalan dan sudah diaktifkan, yang berarti bahwa sistem sudah disiapkan untuk memulai setiap kali server melakukan boot.

      Catatan: Pengaturan ini diinginkan untuk banyak kasus penggunaan umum dari Redis. Namun, jika Anda lebih memilih untuk memulai Redis secara manual setiap kali server melakukan boot, Anda dapat mengonfigurasi ini dengan perintah berikut:

      • sudo systemctl disable redis

      Untuk menguji bahwa Redis berfungsi dengan benar, hubungkan ke server menggunakan redis-cli, yang merupakan klien baris perintah Redis:

      Dalam prompt selanjutnya, uji konektivitas dengan perintah ping:

      Output

      PONG

      Keluaran ini mengonfirmasi bahwa koneksi server masih hidup. Selanjutnya, periksa apakah Anda dapat mengatur kunci dengan menjalankan:

      Output

      OK

      Dapatkan nilainya dengan mengetik:

      Dengan asumsi semuanya berjalan lancar, Anda akan dapat mendapatkan nilai yang Anda simpan:

      Output

      "It's working!"

      Setelah mengonfirmasi bahwa Anda dapat mengambil nilai, keluarlah dari prompt Redis untuk kembali ke shell:

      Sebagai uji coba terakhir, kita akan memeriksa apakah Redis dapat mempertahankan data bahkan setelah sistem dihentikan atau dimulai kembali. Untuk melakukan ini, pertama-tama mulai ulang instans Redis:

      • sudo systemctl restart redis

      Lalu, hubungkan dengan klien baris perintah lagi:

      Dan konfirmasikan bahwa nilai uji coba Anda masih ada

      Nilai dari kunci Anda harus masih dapat diakses:

      Output

      "It's working!"

      Keluarlah dari shell lagi ketika Anda selesai:

      Dengan itu, instalasi Redis Anda sudah beroperasi sepenuhnya dan siap untuk digunakan. Namun, beberapa pengaturan konfigurasi asalinya tidak aman dan memberi peluang kepada aktor jahat untuk menyerang dan memperoleh akses ke server dan data Anda. Langkah-langkah selanjutnya dalam tutorial ini mencakup metode untuk mengurangi kerentanan ini, seperti yang disarankan oleh situs web Redis resmi. Meskipun langkah-langkah ini bersifat opsional dan Redis tetap akan berfungsi jika Anda memilih untuk tidak mengikutinya, tetapi Anda sangat disarankan untuk menyelesaikannya untuk memperkuat keamanan sistem Anda.

      Langkah 3 — Mengikat ke localhost

      Secara asali, Redis hanya dapat diakses dari localhost. Namun, jika Anda menginstal dan mengonfigurasi Redis dengan mengikuti tutorial yang berbeda dari tutorial ini, Anda mungkin telah memperbarui berkas konfigurasi untuk mengizinkan koneksi dari mana saja. Ini tidak seaman terikat ke localhost.

      Untuk memperbaiki ini, buka berkas konfigurasi Redis untuk mengedit:

      • sudo nano /etc/redis/redis.conf

      Temukan baris ini dan pastikan bahwa baris ini tidak dijadikan komentar (hapus # jika ada):

      /etc/redis/redis.conf

      bind 127.0.0.1 ::1
      

      Simpan dan tutup berkas saat selesai (tekan CTRL + X, Y, lalu ENTER).

      Lalu, mulai ulang layanan untuk memastikan bahwa systemd membaca perubahan Anda:

      • sudo systemctl restart redis

      Untuk memeriksa bahwa perubahan ini telah diterapkan, jalankan perintah netstat berikut:

      • sudo netstat -lnp | grep redis

      Output

      tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 14222/redis-server tcp6 0 0 ::1:6379 :::* LISTEN 14222/redis-server

      Catatan: Perintah netstat mungkin tidak tersedia di sistem Anda secara asali. Jika kasusnya seperti ini, Anda dapat menginstalnya (bersama dengan beberapa peralatan jejaring berguna lainnya) dengan perintah berikut :

      • sudo apt install net-tools

      Keluaran ini menunjukkan bahwa program redis-server terikat ke localhost (127.0.0.1), yang mencerminkan perubahan yang Anda baru saja buat pada berkas konfigurasi. Jika Anda melihat alamat IP lain di dalam kolom itu (sebagai contoh: 0.0.0.0), maka Anda harus memeriksa ulang bahwa Anda telah menghapus tanda komentar pada baris yang benar dan memulai ulang layanan Redis lagi.

      Kini setelah instalasi Redis Anda hanya mendengarkan localhost, maka akan lebih sulit bagi aktor jahat untuk membuat permintaan atau memperoleh akses ke server Anda. Namun, Redis saat ini tidak diatur untuk meminta pengguna melakukan autentikasi sendiri sebelum membuat perubahan ke konfigurasi atau data yang dimilikinya. Untuk memperbaiki ini, Redis mengizinkan Anda untuk meminta pengguna melakukan autentikasi dengan kata sandi sebelum membuat perubahan melalui klien Redis (redis-cli).

      Langkah 4 — Mengonfigurasi Kata sandi Redis

      Mengonfigurasi kata sandi Redis akan mengaktifkan salah satu dari dua fitur keamanan bawaan — perintah auth, yang mengharuskan klien melakukan autentikasi untuk mengakses basis data. Kata sandi dikonfigurasi secara langsung di dalam berkas konfigurasi Redis, /etc/redis/redis.conf, jadi buka berkas itu lagi dengan editor pilihan Anda:

      • sudo nano /etc/redis/redis.conf

      Gulir ke bagian SECURITY dan cari arahan dengan komentar yang berbunyi:

      /etc/redis/redis.conf

      . . .
      # requirepass foobared
      . . .
      

      Hapus status komentar dengan menghapus #, dan ubah foobared menjadi kata sandi yang aman.

      Catatan: Di atas arahan requirepass di dalam berkas redis.conf, ada peringatan dengan komentar:

      /etc/redis/redis.conf

      . . .
      # Warning: since Redis is pretty fast an outside user can try up to
      # 150k passwords per second against a good box. This means that you should
      # use a very strong password otherwise it will be very easy to break.
      #
      . . .
      

      Maka dari itu, merupakan hal penting bagi Anda untuk menentukan nilai yang sangat kuat dan sangat panjang sebagai kata sandi Anda. Alih-alih membuat kata sandi sendiri, Anda dapat menggunakan perintah openssl untuk menghasilkan kata sandi acak, seperti dalam contoh berikut. Dengan memasukkan keluaran dari perintah pertama ke perintah openssl kedua, seperti yang ditunjukkan di sini, hal tersebut akan menghapus jeda baris apa pun yang dihasilkan oleh perintah pertama:

      • openssl rand 60 | openssl base64 -A

      Keluaran Anda akan terlihat seperti:

      Output

      RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE

      Setelah menyalin dan menempel keluaran dari perintah itu sebagai nilai baru untuk requirepass, maka keluarannya akan berbunyi:

      /etc/redis/redis.conf

      requirepass RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE

      Setelah menetapkan kata sandi, simpan dan tutup berkas, lalu mulai ulang Redis:

      • sudo systemctl restart redis.service

      Untuk menguji bahwa kata sandi itu bekerja, buka klien Redis:

      Yang berikut ini menunjukkan urutan perintah yang digunakan untuk menguji apakah kata sandi Redis berfungsi. Perintah pertama mencoba untuk mengatur kunci atas suatu nilai sebelum autentikasi:

      Itu tidak akan berhasil karena Anda tidak melakukan autentikasi, jadi Redis merespons dengan pesan kesalahan:

      Output

      (error) NOAUTH Authentication required.

      Perintah selanjutnya mengautentikasi dengan kata sandi yang ditentukan di dalam berkas konfigurasi Redis:

      Redis mengakui:

      Output

      OK

      Setelah itu, menjalankan perintah sebelumnya lagi akan menjadi berhasil:

      Output

      OK

      get key1 melakukan kueri terhadap Redis untuk nilai dari kunci yang baru.

      Output

      "10"

      Setelah mengonfirmasi bahwa Anda dapat menjalankan perintah di dalam klien Redis setelah melakukan autentikasi, Anda dapat keluar dari redis-cli:

      Selanjutnya, kita akan membahas tentang mengganti nama perintah Redis yang, jika dimasukkan secara tidak sengaja atau oleh aktor jahat, dapat menyebabkan kerusakan serius pada mesin Anda.

      Langkah 5 — Mengganti Nama Perintah Berbahaya

      Fitur keamanan lain yang ditanam di dalam Redis melibatkan penggantian nama atau menonaktifkan secara penuh perintah-perintah tertentu yang dianggap berbahaya.

      Ketika dijalankan oleh pengguna yang tidak sah, perintah seperti itu dapat digunakan untuk mengonfigurasi ulang, menghancurkan, atau menghapus data Anda. Seperti kata sandi autentikasi, mengganti nama atau menonaktifkan perintah dikonfigurasi pada bagian SECURITY yang sama dari berkas /etc/redis/redis.conf.

      Beberapa perintah yang dianggap berbahaya termasuk: FLUSHDB, FLUSHALL, KEYS, PEXPIRE, DEL, CONFIG, SHUTDOWN, BGREWRITEAOF, BGSAVE, SAVE, SPOP, SREM, RENAME, dan DEBUG. Ini bukanlah daftar yang lengkap, tetap mengganti nama atau menonaktifkan semua perintah di dalam daftar itu adalah titik awal yang baik untuk menambah keamanan server Redis Anda.

      Apakah Anda harus menonaktifkan atau mengganti nama perintah bergantung pada kebutuhan spesifik Anda atau situs Anda. Jika Anda tahu bahwa Anda tidak akan pernah menggunakan perintah yang dapat disalahgunakan, maka Anda dapat menonaktifkan perintah itu. Jika tidak, mungkin akan lebih baik untuk mengganti nama.

      Untuk mengganti nama atau menonaktifkan perintah Redis, buka berkas konfigurasi sekali lagi:

      • sudo nano /etc/redis/redis.conf

      Peringatan: Langkah berikut yang menunjukkan cara menonaktifkan dan mengganti nama perintah adalah contoh. Anda harus hanya memilih untuk menonaktifkan atau mengganti nama perintah yang masuk akal bagi Anda. Anda dapat meninjau daftar lengkap perintah untuk Anda dan menentukan bagaimana perintah itu mungkin dapat disalahgunakan di redis.io/commands.

      Untuk menonaktifkan perintah, cukup ganti namanya menjadi string kosong (yang ditandai oleh sepasang tanda kutip tanpa karakter di dalamnya), seperti yang ditunjukkan di bawah ini:

      /etc/redis/redis.conf

      . . .
      # It is also possible to completely kill a command by renaming it into
      # an empty string:
      #
      rename-command FLUSHDB ""
      rename-command FLUSHALL ""
      rename-command DEBUG ""
      . . .
      

      Untuk mengganti nama perintah, beri nama lain seperti yang ditunjukkan dalam contoh di bawah ini. Perintah yang diganti namanya harus sulit ditebak oleh orang lain, tetapi mudah untuk Anda ingat:

      /etc/redis/redis.conf

      . . .
      # rename-command CONFIG ""
      rename-command SHUTDOWN SHUTDOWN_MENOT
      rename-command CONFIG ASC12_CONFIG
      . . .
      

      Simpan perubahan Anda dan tutup berkas.

      Setelah mengganti nama perintah, terapkan perubahan dengan memulai ulang Redis:

      • sudo systemctl restart redis.service

      Untuk menguji perintah baru, masuk ke baris perintah Redis:

      Lalu, lakukan autentikasi:

      Output

      OK

      Mari kita berasumsi bahwa Anda telah mengganti nama perintah CONFIG menjadi ASC12_CONFIG, seperti dalam contoh sebelumnya. Pertama-tama, coba gunakan perintah CONFIG yang asli. Ini akan gagal, karena Anda telah mengubah namanya:

      Output

      (error) ERR unknown command `config`, with args beginning with:

      Namun, menggunakan perintah yang telah diubah namanya akan berhasil. Ini tidak peka huruf kapital:

      • asc12_config get requirepass

      Output

      1) "requirepass" 2) "your_redis_password"

      Terakhir, Anda dapat keluar dari redis-cli:

      Perhatikan bahwa jika Anda sudah menggunakan baris perintah Redis lalu memulai ulang Redis, Anda akan perlu melakukan autentikasi ulang. Jika tidak, Anda akan mendapat pesan kesalahan ini jika Anda mengetik perintah:

      Output

      NOAUTH Authentication required.

      Mengenai praktik mengganti nama perintah, ada pernyataan peringatan di bagian akhir dari SECURITY di dalam /etc/redis/redis.conf yang berbunyi:

      /etc/redis/redis.conf

      . . .
      # Please note that changing the name of commands that are logged into the
      # AOF file or transmitted to replicas may cause problems.
      . . .
      

      Catatan: Proyek Redis memilih untuk menggunakan istilah “master” dan “slave”, sedangkan DigitalOcean secara umum lebih menyukai istilah alternatif “primary” dan “secondary”. Untuk menghindari kebingungan, kami memilih untuk menggunakan istilah yang digunakan dalam dokumentasi Redis di sini.

      Artinya, jika penggantian nama perintah tidak ada dalam berkas AOF, atau jika itu ada tetapi berkas AOF belum dikirim ke slave, maka seharusnya tidak ada masalah.

      Maka, ingatlah hal itu ketika Anda mencoba mengganti nama perintah. Waktu terbaik untuk mengganti nama perintah adalah saat Anda tidak menggunakan persistensi AOF, atau segera setelah instalasi, yang artinya, sebelum aplikasi yang menggunakan Redis disebarkan.

      Ketika Anda menggunakan AOF dan menangani instalasi master-slave, pertimbangkan jawaban dari laman masalah proyek Github ini. Berikut ini adalah jawaban dari pertanyaan penulis:

      Perintah-perintah dicatat ke AOF dan direplikasi ke slave dengan cara pengiriman yang sama, maka jika Anda mencoba untuk memainkan AOF kembali pada suatu instans yang tidak memiliki penggantian nama yang sama, Anda mungkin akan menghadapi inkonsistensi karena perintah tidak dapat dieksekusi (sama untuk slave).

      Dengan demikian, cara terbaik untuk menangani penggantian nama dalam kasus seperti itu adalah dengan memastikan bahwa mengganti nama perintah diterapkan pada semua instans dalam instalasi master-slave.

      Kesimpulan

      Dalam tutorial ini, Anda telah menginstal dan mengonfigurasi Redis, memvalidasi bahwa instalasi Redis Anda berfungsi dengan benar, dan menggunakan fitur keamanan bawaannya untuk membuatnya lebih tidak rentan terhadap serangan dari aktor-aktor jahat.

      Harap diingat bahwa setelah seseorang log masuk ke server Anda, maka sangat mudah untuk mengakali fitur keamanan spesifik Redis yang kita sudah atur. Oleh karena itu, fitur keamanan yang paling penting pada server Redis adalah firewall (yang Anda konfigurasikan jika Anda mengikuti tutorial prasyarat Penyiapan Server Awal ini, karena ini membuat aktor jahat sangat kesulitan untuk melompati pagar keamanan.



      Source link

      How To Install and Secure Redis on CentOS 8


      Not using CentOS 8?


      Choose a different version or distribution.

      Introduction

      Redis is an open-source, in-memory key-value data store which excels at caching. A non-relational database, Redis is known for its flexibility, performance, scalability, and wide language support.

      Redis was designed for use by trusted clients in a trusted environment, and has no robust security features of its own. Redis does, however, have a few security features like a basic unencrypted password as well as command renaming and disabling. This tutorial provides instructions on how to install Redis and configure these security features. It also covers a few other settings that can boost the security of a standalone Redis installation on CentOS 8.

      Note that this guide does not address situations where the Redis server and the client applications are on different hosts or in different data centers. Installations where Redis traffic has to traverse an insecure or untrusted network will require a different set of configurations, such as setting up an SSL proxy or a VPN between the Redis machines.

      Prerequisites

      To complete this tutorial, you will need a server running CentOS 8. This server should have a non-root user with administrative privileges and a firewall configured with firewalld. To set this up, follow our Initial Server Setup guide for CentOS 8.

      Step 1 — Installing and Starting Redis

      You can install Redis with the DNF package manager. The following command will install Redis, its dependencies, and nano, a user-friendly text editor. You don’t have to install nano, but we’ll use it in examples throughout this guide:

      • sudo dnf install redis nano

      This command will prompt you to confirm that you want to install the selected packages. Press y then ENTER to do so:

      Output

      . . . Total download size: 1.5 M Installed size: 5.4 M Is this ok [y/N]: y

      Following this, there is one important configuration change to make in the Redis configuration file, which was generated automatically during the installation.

      Open this file with your preferred text editor. Here we’ll use nano:

      • sudo nano /etc/redis/redis.conf

      Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised directive is set to no by default. Since you are running CentOS, which uses the systemd init system, change this to systemd:

      /etc/redis/redis.conf

      . . .
      
      # If you run Redis from upstart or systemd, Redis can interact with your
      # supervision tree. Options:
      #   supervised no      - no supervision interaction
      #   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
      #   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
      #   supervised auto    - detect upstart or systemd method based on
      #                        UPSTART_JOB or NOTIFY_SOCKET environment variables
      # Note: these supervision methods only signal "process is ready."
      #       They do not enable continuous liveness pings back to your supervisor.
      supervised systemd
      
      . . .
      

      That’s the only change you need to make to the Redis configuration file at this point, so save and close it when you are finished. If you used nano to edit the file, do so by pressing CTRL + X, Y, then ENTER.

      After editing the file, start the Redis service:

      • sudo systemctl start redis.service

      If you’d like Redis to start on boot, you can enable it with the enable command:

      • sudo systemctl enable redis

      Notice that this command doesn’t include the .service suffix after the unit file name. You can usually leave this suffix off of systemctl commands, as it’s typically implied when interacting with systemd.

      You can check Redis’s status by running the following:

      • sudo systemctl status redis

      Output

      ● redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Wed 2020-09-30 20:05:24 UTC; 13s ago Main PID: 13734 (redis-server) Tasks: 4 (limit: 11489) Memory: 6.6M CGroup: /system.slice/redis.service └─13734 /usr/bin/redis-server 127.0.0.1:6379

      Once you’ve confirmed that Redis is indeed running, you can test its functionality with this command:

      This should print PONG as the response:

      Output

      PONG

      If this is the case, it means you now have Redis running on your server and you can begin configuring it to enhance its security.

      Step 2 — Configuring Redis and Securing it with a Firewall

      An effective way to safeguard Redis is to secure the server it’s running on. You can do this by ensuring that Redis is bound only to either localhost or to a private IP address and also that the server has a firewall up and running.

      However, if you chose to set up Redis using another tutorial, then you may have updated the configuration file to allow connections from anywhere. This is not as secure as binding to localhost or a private IP.

      To remedy this, open the Redis configuration file again with your preferred text editor:

      • sudo nano /etc/redis.conf

      Locate the line beginning with bind and make sure it’s uncommented:

      /etc/redis.conf

      . . .
      bind 127.0.0.1
      

      If you need to bind Redis to another IP address (as in cases where you will be accessing Redis from a separate host) we strongly encourage you to bind it to a private IP address. Binding to a public IP address increases the exposure of your Redis interface to outside parties:

      /etc/redis.conf

      . . .
      bind your_private_ip
      

      After confirming that the bind directive isn’t commented out, you can save and close the file.

      If you’ve followed the prerequisite Initial Server Setup tutorial and installed firewalld on your server, and you do not plan to connect to Redis from another host, then you do not need to add any extra firewall rules for Redis. After all, any incoming traffic will be dropped by default unless explicitly allowed by the firewall rules. Since a default standalone installation of Redis server is listening only on the loopback interface (127.0.0.1 or localhost), there should be no concern for incoming traffic on its default port.

      If, however, you do plan to access Redis from another host, you will need to make some changes to your firewalld configuration using the firewall-cmd command. Again, you should only allow access to your Redis server from your hosts by using their private IP addresses in order to limit the number of hosts your service is exposed to.

      To begin, add a dedicated Redis zone to your firewalld policy:

      • sudo firewall-cmd --permanent --new-zone=redis

      Then specify which port you’d like to have open. Redis uses port 6397 by default:

      • sudo firewall-cmd --permanent --zone=redis --add-port=6379/tcp

      Next, specify any private IP addresses which should be allowed to pass through the firewall and access Redis:

      • sudo firewall-cmd --permanent --zone=redis --add-source=client_server_private_IP

      After running those commands, reload the firewall to implement the new rules:

      • sudo firewall-cmd --reload

      Under this configuration, when the firewall encounters a packet from your client’s IP address, it will apply the rules in the dedicated Redis zone to that connection. All other connections will be processed by the default public zone. The services in the default zone apply to every connection, not just those that don’t match explicitly, so you don’t need to add other services (e.g. SSH) to the Redis zone because those rules will be applied to that connection automatically.

      Keep in mind that using any firewall tool will work, whether you use firewalld, ufw, or iptables. What’s important is that the firewall is up and running so that unknown individuals cannot access your server. In the next step, you will configure Redis to only be accessible with a strong password.

      Step 3 — Configuring a Redis Password

      Configuring a Redis password enables one of its built-in security features — the auth command — which requires clients to authenticate before being allowed access to the database. Like the bind setting, the password is configured directly in Redis’s configuration file, /etc/redis.conf. Reopen that file:

      • sudo nano /etc/redis.conf

      Scroll to the SECURITY section and look for a commented directive that reads:

      /etc/redis.conf

      . . .
      # requirepass foobared
      

      Uncomment it by removing the #, and change foobared to a very strong password of your choosing.

      Note: Rather than make up a password yourself, you may use a tool like apg or pwgen to generate one. If you don’t want to install an application just to generate a password, though, you may use the command below. This command echoes a string value and pipes it into the following sha256sum command, which will display the string’s SHA256 checksum.

      Be aware that entering this command as written will generate the same password every time. To create a unique password, change the string in quotes to any other word or phrase:

      • echo "digital-ocean" | sha256sum

      Though the generated password will not be pronounceable, it will be very strong and long, which is exactly the type of password required for Redis. After copying and pasting the output of that command as the new value for requirepass, it should read:

      /etc/redis.conf

      . . .
      requirepass password_copied_from_output
      

      Alternatively, if you prefer a shorter password, you could instead use the output of the following command. Again, change the word in quotes so it will not generate the same password as this command:

      • echo "digital-ocean" | sha1sum

      After setting the password, save and close the file then restart Redis:

      • sudo systemctl restart redis

      To test that the password works, open the Redis client:

      The following is a sequence of commands used to test whether the Redis password works. The first command tries to set a key to a value before authentication:

      That won’t work as you have not yet authenticated, so Redis returns an error:

      Output

      (error) NOAUTH Authentication required.

      The following command authenticates with the password specified in the Redis configuration file:

      Redis will acknowledge that you have been authenticated:

      Output

      OK

      After that, running the previous command again should be successful:

      Output

      OK

      The get key1 command queries Redis for the value of the new key:

      Output

      "10"

      This last command exits redis-cli. You may also use exit:

      It should now be very difficult for unauthorized users to access your Redis installation. Be aware that if you’re already using the Redis client and then restart Redis, you’ll need to re-authenticate. Also, please note that without SSL or a VPN, the unencrypted password will still be visible to outside parties if you’re connecting to Redis remotely.

      Next, this guide will go over renaming Redis commands to further protect Redis from malicious actors.

      Step 4 — Renaming Dangerous Commands

      The other security feature built into Redis allows you to rename or completely disable certain commands that are considered dangerous. When run by unauthorized users, such commands can be used to reconfigure, destroy, or otherwise wipe your data. Some of the commands that are considered to be dangerous include:

      • FLUSHDB
      • FLUSHALL
      • KEYS
      • PEXPIRE
      • DEL
      • CONFIG
      • SHUTDOWN
      • BGREWRITEAOF
      • BGSAVE
      • SAVE
      • SPOP
      • SREM
      • RENAME
      • DEBUG

      This is not a comprehensive list, but renaming or disabling all of the commands in this list can help to improve your data store’s security. Whether you should disable or rename a given command will depend on your specific needs. If you know you will never use a command that can be abused, then you may disable it. Otherwise, you should rename it instead.

      Like the authentication password, renaming or disabling commands is configured in the SECURITY section of the /etc/redis.conf file. To enable or disable Redis commands, open the configuration file for editing one more time:

      • sudo nano /etc/redis.conf

      NOTE: These are examples. You should choose to disable or rename the commands that make sense for you. You can learn more about Redis’s commands and determine how they might be misused at redis.io/commands.

      To disable or kill a command, rename it to an empty string, like this:

      /etc/redis.conf

      # It is also possible to completely kill a command by renaming it into
      # an empty string:
      #
      rename-command FLUSHDB ""
      rename-command FLUSHALL ""
      rename-command DEBUG ""
      

      To rename a command, give it another name like in the examples below. Renamed commands should be difficult for others to guess, but easy for you to remember:

      /etc/redis.conf

      # It is also possible to completely kill a command by renaming it into
      # an empty string:
      #
      rename-command FLUSHDB ""
      rename-command FLUSHALL ""
      rename-command DEBUG ""
      rename-command SHUTDOWN SHUTDOWN_MENOT
      rename-command CONFIG ASC12_CONFIG
      

      Save your changes and close the file. Then apply the changes by restarting Redis:

      • sudo systemctl restart redis.service

      To test your new commands, enter the Redis command line:

      Authenticate yourself using the password you defined earlier:

      Output

      OK

      Assuming that you renamed the CONFIG command to ASC12_CONFIG, attempting to use the config command will fail:

      Output

      (error) ERR unknown command 'config'

      Calling the renamed command instead will be successful. Note that Redis commands are not case-sensitive:

      • asc12_config get requirepass

      Output

      1) "requirepass" 2) "your_redis_password"

      Finally, you can exit from redis-cli:

      Warning: Regarding renaming commands, there’s a cautionary statement at the end of the SECURITY section in the /etc/redis.conf file, which reads:

      /etc/redis.conf

      . . .
      
      # Please note that changing the name of commands that are logged into the
      # AOF file or transmitted to slaves may cause problems.
      
      . . .
      

      This means if the renamed command is not in the AOF file, or if it is but the AOF file has not been transmitted to replicas, then there should be no problem. Keep that in mind as you’re renaming commands. The best time to rename a command is when you’re not using AOF persistence or right after installation (that is, before your Redis-using application has been deployed).

      When you’re using AOF and dealing with Redis replication, consider this answer from the project’s GitHub issue page. The following is a reply to the author’s question:

      The commands are logged to the AOF and replicated to the slave the same way they are sent, so if you try to replay the AOF on an instance that doesn’t have the same renaming, you may face inconsistencies as the command cannot be executed (same for slaves).

      The best way to handle renaming in cases like that is to make sure that renamed commands are applied to the primary instance as well as every secondary instance in a Redis installation.

      Step 5 — Setting Data Directory Ownership and File Permissions

      This step will go through a couple of ownership and permissions changes you may need to make to improve the security profile of your Redis installation. This involves making sure that only the user that needs to access Redis has permission to read its data. That user is, by default, the redis user.

      You can verify this by grep-ing for the Redis data directory in a long listing of its parent directory. This command and its output are given below:

      • ls -l /var/lib | grep redis

      Output

      drwxr-x---. 2 redis redis 22 Sep 30 20:15 redis

      This output indicates that the Redis data directory is owned by the redis user, with secondary access granted to the redis group. This ownership setting is secure as are the folder’s permissions which, using octal notation, are set to 750.

      If your Redis data directory has insecure permissions (for example, it’s world-readable) you can ensure that only the Redis user and group have access to the folder and its contents by running the chmod command. The following example changes this folder’s the permissions setting to 770:

      • sudo chmod 770 /var/lib/redis

      The other permission you may need to change is that of the Redis configuration file. By default, it has a file permission of 640 and is owned by root, with secondary ownership by the root group:

      Output

      -rw-r-----. 1 redis root 62344 Sep 30 20:14 /etc/redis.conf

      That permission (640) means the Redis configuration file is readable only by the redis user and the root group. Because the configuration file contains the unencrypted password you configured in Step 4, redis.conf should be owned by the redis user, with secondary ownership by the redis group. To set this, run the following command:

      • sudo chown redis:redis /etc/redis.conf

      Then change the permissions so that only the owner of the file can read and write to it:

      • sudo chmod 600 /etc/redis.conf

      You may verify the new ownership and permissions by running the previous ls commands again:

      • ls -l /var/lib | grep redis

      Output

      total 40 drwxrwx---. 2 redis redis 22 Sep 30 20:15 redis

      Output

      total 40 -rw-------. 1 redis redis 62344 Sep 30 20:14 /etc/redis.conf

      Finally, restart Redis to reflect these changes:

      • sudo systemctl restart redis

      With that, your Redis installation has been secured.

      Conclusion

      Keep in mind that once someone is logged in to your server, it’s very easy to circumvent the Redis-specific security features you’ve put in place. This is why the most important security feature covered in this tutorial is the firewall, as that prevents unknown users from logging into your server in the first place.

      If you’re attempting to secure Redis communication across an untrusted network you’ll have to employ an SSL proxy, as recommended by Redis developers in the official Redis security guide.



      Source link

      How To Manage a Redis Database eBook


      Download the Complete eBook!

      How To Manage a Redis Database eBook in EPUB format

      How To Manage a Redis Database eBook in PDF format

      Introduction to the eBook

      This book aims to provide an approachable introduction to Redis concepts by outlining many of the key-value store’s commands so readers can learn their patterns and syntax, thus building up readers’ understanding gradually. The goal for this book is to serve as an introduction to Redis for those interested in getting started with it, or key-value stores in general. For more experienced users, this book can function as a collection of helpful cheat sheets and in-depth reference.

      This book is based on the How To Manage a Redis Database tutorial series found on DigitalOcean Community. The topics that it covers include how to:

      1. Connect to a Redis database

      2. Create and use a variety of Redis data types, including strings, sets, hashes, and lists

      3. Manage Redis clients and replicas

      4. Run transactions in Redis

      5. Troubleshoot issues in a Redis installation

      Each chapter is self-contained and can be followed independently of the others. By reading through this book, you’ll become acquainted with many of Redis’s most widely used commands, which will help you as you begin to build applications that take advantage of its power and speed.

      Download the eBook

      You can download the eBook in either the EPUB or PDF format by following the links below.

      Download the Complete eBook!

      How To Manage a Redis Database eBook in EPUB format

      How To Manage a Redis Database eBook in PDF format

      If you’d like to learn more about how to use Redis, visit the DigitalOcean Community’s Redis section. Alternatively, if you want to learn about other open-source database management systems, like MySQL, PostgreSQL, MariaDB, or MongoDB, we encourage you to check out our full library of database-related content.



      Source link