{"id":2764,"date":"2019-05-02T13:33:14","date_gmt":"2019-05-02T10:33:14","guid":{"rendered":"https:\/\/kifarunix.com\/?p=2764"},"modified":"2022-01-22T13:55:36","modified_gmt":"2022-01-22T10:55:36","slug":"install-and-configure-logstash-7-on-ubuntu-18-debian-9-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-configure-logstash-7-on-ubuntu-18-debian-9-8\/","title":{"rendered":"Install and Configure Logstash 7 on Ubuntu 18\/Debian 9.8"},"content":{"rendered":"\n<p>This guide will focus on how to install and configure Logstash 7 on Ubuntu 18.04\/Debian 9.8 as a continuation of our guide on how to setup Elastic Stack 7 on Ubuntu 18.04\/Debian 9.8. We have already covered the installation of Elasticsearch and Kibana.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-elasticsearch-7-x-on-ubuntu-18-04-debian-9-8\/\" target=\"_blank\">Install Elasticsearch 7.x on Ubuntu 18.04\/Debian 9.8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-elastic-stack-7-on-ubuntu-18-04-debian-9-8\/\" target=\"_blank\">Install Kibana 7 on Ubuntu 18.04\/Debian 9.8<\/a><\/p>\n\n\n\n<p>According to the installation order, Logstash is the third component in the line. Note that for Elastic Stack to function well, versions of all the components must match.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install and Configure Logstash 7 on Ubuntu 18\/Debian 9.8<\/h2>\n\n\n\n<p>You can install Logstash on Ubuntu\/Debian by either using DEB binary or right from APT repositories.<\/p>\n\n\n\n<p>We prefer the installation from APT repositories as this ensure a seamless upgrade incase of new package releases.<\/p>\n\n\n\n<p>Thus, to install Logstash on Ubuntu\/Debian via the APT repos;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Install Repository public signing key<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -qO - https:\/\/artifacts.elastic.co\/GPG-KEY-elasticsearch | sudo apt-key add -<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Install Elastic Stack APT repositories<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install apt-transport-https<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"deb https:\/\/artifacts.elastic.co\/packages\/7.x\/apt stable main\" | sudo tee \/etc\/apt\/sources.list.d\/elastic-7.x.list<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Install Logstash on Ubuntu\/Debian<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install logstash<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"configurelogstash\"><a href=\"#configurelogstash\">Configuring Logstash<\/a><\/h3>\n\n\n\n<p>Once the installation is done, proceed to configure Logstash. Logstash data processing pipeline has three sections;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>INPUT<\/strong>: input section is used to ingest data from different endpoints into Logstash.<\/li><li><strong>FILTERS<\/strong>: which processes and transform the data received.<\/li><li><strong>OUTPUT<\/strong>: which stashes processed data into a specified destination, which can be Elasticsearch.<\/li><\/ul>\n\n\n\n<p>You can read more about Logstash Pipeline <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/7.0\/pipeline.html\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>While configuring Logstash, you can have separate configuration files each for INPUT, FILTERS and OUTPUT. You can as well have single configuration file for all the sections. This guides uses separate configuration files.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Configure Logstash Input plugin<\/h4>\n\n\n\n<p>To kick off with, create a configuration file to define how data will be ingested into Logstash. For example, to configure Logstash to receive data from <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/downloads\/beats\" target=\"_blank\">Beats<\/a> on TCP port 5044, create an input configuration file say, <strong>\/etc\/logstash\/conf.d\/beats-input.conf<\/strong>, with the content below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/logstash\/conf.d\/beats-input.conf<br><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>input {\n  beats {\n    port =&gt; 5044\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_21\">Configure Logstash Filters<\/h4>\n\n\n\n<p>Now that we have defined an Logstash Input plugin as Beats. Hence, proceed to configure a filter plugin to process events received from the beats. This guide uses <strong><a rel=\"noreferrer noopener\" aria-label=\"grok (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/current\/plugins-filters-grok.html\" target=\"_blank\">grok<\/a><\/strong> filter plugin. You can read about other plugins <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/current\/filter-plugins.html\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>For demonstration purposes, we are going to configure beats to collect SSH authentication events from Ubuntu\/CentOS systems. Hence, we are going to create a filter to process such kind of events as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/logstash\/conf.d\/ssh-auth-filter.conf<\/code><\/pre>\n\n\n\n<p>The grok pattern used in this example matches the ssh authetication log lines below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>May  1 13:15:23 elk sshd[1387]: <strong>Failed password<\/strong> for testuser from 192.168.0.102 port 60004 ssh2<br>May  1 13:08:30 elk sshd[1338]: <strong>Accepted password<\/strong> for testuser from 192.168.0.102 port 59958 ssh2<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\nfilter {\n  grok {\n    match => { \"message\" => \"%{SYSLOGTIMESTAMP:timestamp}\\s+%{IPORHOST:dst_host}\\s+%{WORD:syslog_program}\\[\\d+\\]:\\s+(?&lt;status&gt;\\w+\\s+password)\\s+for\\s+%{USER:auth_user}\\s+from\\s+%{SYSLOGHOST:src_host}.*\" }\n    add_field => { \"activity\" => \"SSH Logins\" }\n    add_tag => \"linux_auth\"\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Kibana 7 comes bundled with Grok Debugger which is similar to <a rel=\"noreferrer noopener\" aria-label=\"herokuapp grokdebugger (opens in a new tab)\" href=\"http:\/\/grokdebug.herokuapp.com\/\" target=\"_blank\">herokuapp grokdebugger<\/a>.  You can access Kibana Grok debugger under <strong>Dev Tools &gt; Grok Debugger<\/strong>. You can utilize this to generate the correct grok patterns. You can as well check common logstash grok patterns <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/github.com\/logstash-plugins\/logstash-patterns-core\/blob\/master\/patterns\/grok-patterns\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>Also for the purposes of making demo a simple, we will add a filter to drop all the events that do not match our grok filter for SSH authentication events used above;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>if \"_grokparsefailure\" in [tags] { drop {} }<\/code><\/pre>\n\n\n\n<p>Such that our filter looks like;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>filter {\n  grok {\n    match =&gt; { \"message\" =&gt; \"%{SYSLOGTIMESTAMP:timestamp}\\s+%{IPORHOST:dst_host}\\s+%{WORD:syslog_program}\\[\\d+\\]:\\s+(?&lt;status&gt;\\w+\\s+password)\\s+for\\s+%{USER:auth_user}\\s+from\\s+%{SYSLOGHOST:src_host}.*\" }\n    add_field =&gt; { \"activity\" =&gt; \"SSH Logins\" }\n    add_tag =&gt; \"linux_auth\"\n    }\n  if \"_grokparsefailure\" in [tags] { drop {} }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_28\">Configure Logstash Output<\/h4>\n\n\n\n<p>There are different <a rel=\"noreferrer noopener\" aria-label=\"output plugins (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/current\/output-plugins.html\" target=\"_blank\">output plugins<\/a> that enables Logstash to sent event data to particular destinations. This guide uses <strong>elasticsearch<\/strong> that enables Logstash to sent data to Elasticsearch.<\/p>\n\n\n\n<p>Create Logstash output configuration file with the content below. This confguration sents data to Elasticsearch running on a localhost. The index defines the index to write events to, <strong>logstash-%{+YYYY.MM.dd}<\/strong> is the default index.<\/p>\n\n\n\n<p>If Elasticsearch is listening on non-loopback interface, replace localhost, <em>hosts =&gt; [&#8220;<strong>localhost<\/strong>:9200&#8243;]<\/em>  with an interface IP, for example; <em>hosts =&gt; [&#8220;<strong>192.168.0.101<\/strong>:9200&#8243;]<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/logstash\/conf.d\/elasticsearch-output.conf<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>output {\n   elasticsearch {\n     hosts =&gt; [\"192.168.0.101:9200\"]\n     manage_template =&gt; false\n     index =&gt; \"ssh_auth-%{+YYYY.MM}\"\n }\n}\n<\/code><\/pre>\n\n\n\n<p>So far we have used different configuration file for each Logstash section. If you need to put them in one file, then create a configuration file as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/logstash\/conf.d\/ssh-authentication.conf<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\ninput {\n  beats {\n    port => 5044\n  }\n}\nfilter {\n  grok {\n    match => { \"message\" => \"%{SYSLOGTIMESTAMP:timestamp}\\s+%{IPORHOST:dst_host}\\s+%{WORD:syslog_program}\\[\\d+\\]:\\s+(?&lt;status&gt;\\w+\\s+password)\\s+for\\s+%{USER:auth_user}\\s+from\\s+%{SYSLOGHOST:src_host}.*\" }\n    add_field => { \"activity\" => \"SSH Logins\" }\n    add_tag => \"linux_auth\"\n    }\n  if \"_grokparsefailure\" in [tags] { drop {} }\n}\noutput {\n  elasticsearch {\n    hosts => [\"localhost:9200\"]\n    index => \"ssh_auth-%{+YYYY.MM}\"\n}\n}\n<\/code><\/pre>\n\n\n\n<p>If you need to sent the event data to standard output as well for the purposes of debugging plugin configurations, then you would add the line, <strong>stdout { codec =&gt; rubydebug }<\/strong> to the output configuration section.<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>output {\n  elasticsearch {\n    hosts =&gt; [\"localhost:9200\"]\n    index =&gt; \"ssh_auth-%{+YYYY.MM}\"\n}\n  stdout { codec =&gt; rubydebug }\n}\n<\/code><\/pre>\n\n\n\n<p>You can also check sample Logstash pipelines <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/7.0\/logstash-config-for-filebeat-modules.html#parsing-system\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>If you need to debug Logstash Grok Filters to confirm that they can actually parse your logs into the required fields, see the link below on how to debug Logstash Grok filters.<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-debug-logstash-grok-filters\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">How to Debug Logstash Grok Filters<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Test Logstash Configuration<\/h4>\n\n\n\n<p>Once you are done with configurations, run the command below to verify the Logstash configuration before you can start it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo -u logstash \/usr\/share\/logstash\/bin\/logstash --path.settings \/etc\/logstash -t<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code> Sending Logstash logs to \/var\/log\/logstash which is now configured via log4j2.properties\n Configuration OK\n [2019-05-01T15:26:34,908][INFO ][logstash.runner          ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash\n<\/code><\/pre>\n\n\n\n<p>Well, if you get <strong>Configuration OK<\/strong> then you are good to go.<\/p>\n\n\n\n<p>To run Logstash and load a specific configuration file for debugging, you can execute the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo -u logstash \/usr\/share\/logstash\/bin\/logstash -f \/etc\/logstash\/conf.d\/<strong>config-file.conf<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running Logstash<\/h3>\n\n\n\n<p>You can now start and enable Logstash to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl start logstash<br>systemctl enable logstash<\/code><\/pre>\n\n\n\n<p>You can also check the logstash configuration file for any errors, <strong>\/var\/log\/logstash\/logstash-plain.log<\/strong>.<\/p>\n\n\n\n<p>Once you are done with configuration, proceed to install and configure Filebeat data shippers.<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-and-configure-filebeat-7-on-ubuntu-18-04-debian-9-8\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install and Configure Filebeat 7 on Ubuntu 18.04\/Debian 9.8<\/a><\/p>\n\n\n\n<p>That is all on how to install and configure Logstash 7 on Ubuntu 18.04\/Debian 9.8.<\/p>\n\n\n\n<p>Reference:<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\"Getting Started with Logstash (opens in a new tab)\" href=\"https:\/\/www.elastic.co\/guide\/en\/logstash\/current\/getting-started-with-logstash.html\" target=\"_blank\">Getting Started with Logstash<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Guides;<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-logstash-7-on-fedora-30-fedora-29-centos-7\/\" target=\"_blank\">Install Logstash 7 on Fedora 30\/Fedora 29\/CentOS 7<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-elastic-stack-7-on-fedora-30-fedora-29-centos-7\/\" target=\"_blank\">Install Elastic Stack 7 on Fedora 30\/Fedora 29\/CentOS 7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide will focus on how to install and configure Logstash 7 on Ubuntu 18.04\/Debian 9.8 as a continuation of our guide on how to<\/p>\n","protected":false},"author":1,"featured_media":8883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[72,910],"tags":[912,913,4471,1567,1568,921,4472],"class_list":["post-2764","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-monitoring","category-elastic-stack","tag-elastic-stack","tag-elk","tag-install-logstash-on-debian","tag-install-logstash-on-ubuntu-18-04","tag-install-logstash-ubuntu","tag-logstash","tag-logstash-apt-repositories","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2764"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=2764"}],"version-history":[{"count":19,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2764\/revisions"}],"predecessor-version":[{"id":11366,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2764\/revisions\/11366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/8883"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=2764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=2764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=2764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}