Nagios Core Setup Guide on Ubuntu/Debian

Nagios Core Setup Guide on Ubuntu/Debian

Nagios Core Setup Guide on Ubuntu/Debian

UbuntuMonitoringNagios

This guide walks through installing and configuring Nagios Core on Ubuntu or Debian to monitor servers, services, and network devices.


1. What Is Nagios Core?

Nagios Core is an open-source monitoring system used to monitor:

  • Servers
  • Network devices
  • Applications
  • Services
  • CPU and memory usage
  • Disk space
  • Network availability
  • Website availability
  • System processes

Nagios uses plugins to perform checks. For example:

Nagios Core → check_ping plugin → Tests whether a host responds
Nagios Core → check_http plugin → Tests whether a website works
Nagios Core → check_disk plugin → Checks available disk space

2. Important Nagios Terms

Term Definition
Host A device being monitored, such as a server or router
Service A specific feature or process monitored on a host
Plugin Program that performs a monitoring check
Command Definition describing how a plugin should run
Host group Group of related hosts
Service group Group of related services
Contact Person or team receiving notifications
Contact group Group of contacts
Check interval How often Nagios performs a check
Notification Alert sent when a problem occurs
OK The monitored item is working normally
WARNING The item requires attention but is not critical
CRITICAL The item has failed or is in a serious state
UNKNOWN Nagios cannot determine the status

3. Example Environment

This guide uses:

Nagios server hostname: nagios.example.local
Nagios server IP:       192.168.1.10
Monitored web server:   192.168.1.20
Monitored web service:  192.168.1.20
Nagios web user:        nagiosadmin

Replace these values with your own network information.


4. System Requirements

You need:

  • Ubuntu or Debian server
  • Static IP address
  • sudo access
  • Apache web server
  • Network access to monitored devices
  • Firewall access where required

For a small monitoring environment, a server with the following is usually sufficient:

2 CPU cores
2 GB RAM
20 GB disk space

5. Update the Server

sudo apt update
sudo apt upgrade -y

Set the hostname:

sudo hostnamectl set-hostname nagios.example.local

Confirm the hostname:

hostnamectl

6. Install Nagios Core and Plugins

Install Nagios, Apache, and standard plugins:

sudo apt install nagios4 nagios-plugins-contrib nagios-plugins-basic monitoring-plugins -y

Some distributions use different package names. If a package is unavailable, search for it:

apt search nagios

Enable Apache:

sudo systemctl enable apache2
sudo systemctl start apache2

Enable Nagios:

sudo systemctl enable nagios4
sudo systemctl start nagios4

Check the service:

sudo systemctl status nagios4

7. Create a Nagios Web Login

Create a web administrator account:

sudo htpasswd -c /etc/nagios4/htpasswd.users nagiosadmin

Enter a strong password when prompted.

The -c option creates the password file. Do not use -c when adding additional users, because it may overwrite the existing file.

Add another user:

sudo htpasswd /etc/nagios4/htpasswd.users operator

8. Open the Nagios Web Interface

Find the server’s IP address:

ip addr

Open a browser and visit:

http://192.168.1.10/nagios4

Log in using:

Username: nagiosadmin
Password: The password created earlier

You should see the Nagios dashboard.


9. Understand the Nagios Configuration Files

Important files are usually located in:

/etc/nagios4/

Common files include:

/etc/nagios4/nagios.cfg
/etc/nagios4/cgi.cfg
/etc/nagios4/objects/
/etc/nagios-plugins/config/

The main configuration file is:

/etc/nagios4/nagios.cfg

Object definitions are commonly stored in:

/etc/nagios4/objects/

Typical object files include:

commands.cfg
contacts.cfg
localhost.cfg
templates.cfg
timeperiods.cfg

A Nagios installation generally contains these configuration objects:

Host → Server, router, switch, or other device
Service → HTTP, SSH, ping, disk, CPU, and similar check
Command → Plugin command used by a service
Contact → Person receiving notifications

10. Check the Local Nagios Server

Nagios usually includes a configuration for monitoring the local machine.

Find the local host configuration:

sudo nano /etc/nagios4/objects/localhost.cfg

A basic host definition looks like this:

define host {
    use                     generic-host
    host_name               nagios-server
    alias                   Nagios Monitoring Server
    address                 127.0.0.1
    max_check_attempts      5
    check_period            24x7
    notification_interval   30
    notification_period     24x7
}

A basic service definition looks like this:

define service {
    use                     generic-service
    host_name               nagios-server
    service_description     PING
    check_command           check_ping!100.0,20%!500.0,60%
}

The check_ping command means:

WARNING: 100 ms latency or 20% packet loss
CRITICAL: 500 ms latency or 60% packet loss

11. Create a Configuration File for a Remote Host

Create a file for the monitored server:

sudo nano /etc/nagios4/objects/web-server.cfg

Add:

define host {
    use                     generic-host
    host_name               web-server
    alias                   Web Server
    address                 192.168.1.20
    max_check_attempts      5
    check_period            24x7
    notification_interval   30
    notification_period     24x7
}

Host definition explanation

use generic-host

Uses default settings from the generic host template.

host_name web-server

Internal name used by Nagios.

alias Web Server

Friendly name shown in the web interface.

address 192.168.1.20

IP address or resolvable hostname of the monitored device.

max_check_attempts 5

Nagios tries the check five times before declaring a problem.

check_period 24x7

The host is checked all day, every day.


12. Monitor Ping

Add this service to the same file:

define service {
    use                     generic-service
    host_name               web-server
    service_description     PING
    check_command           check_ping!100.0,20%!500.0,60%
    normal_check_interval   5
    retry_check_interval    1
}

This checks whether the server responds to ICMP ping.


13. Monitor SSH

Add:

define service {
    use                     generic-service
    host_name               web-server
    service_description     SSH
    check_command           check_ssh
    normal_check_interval   5
    retry_check_interval    1
}

This checks whether TCP port 22 is available.

Test connectivity manually:

nc -vz 192.168.1.20 22

14. Monitor a Website

Add:

define service {
    use                     generic-service
    host_name               web-server
    service_description     HTTP
    check_command           check_http
    normal_check_interval   5
    retry_check_interval    1
}

For HTTPS:

define service {
    use                     generic-service
    host_name               web-server
    service_description     HTTPS
    check_command           check_http!-S
    normal_check_interval   5
    retry_check_interval    1
}

To check a specific URL path:

define service {
    use                     generic-service
    host_name               web-server
    service_description     Website Login Page
    check_command           check_http!-u /login
    normal_check_interval   5
    retry_check_interval    1
}

15. Monitor DNS

If your DNS server is at 192.168.1.10, create a DNS check:

define service {
    use                     generic-service
    host_name               nagios-server
    service_description     DNS
    check_command           check_dns!-s 192.168.1.10 -H example.com
    normal_check_interval   5
    retry_check_interval    1
}

This checks whether the DNS server can answer for example.com.


16. Monitor Disk Space, Load, and Users with NRPE

To monitor internal resources such as disk space, CPU load, and logged-in users, install the Nagios Remote Plugin Executor, known as NRPE.

On the monitored Linux server:

sudo apt update
sudo apt install nagios-nrpe-server nagios-plugins -y

Edit the NRPE configuration:

sudo nano /etc/nagios/nrpe.cfg

Find:

allowed_hosts=127.0.0.1

Change it to include the Nagios server:

allowed_hosts=127.0.0.1,192.168.1.10

Restart NRPE:

sudo systemctl enable nagios-nrpe-server
sudo systemctl restart nagios-nrpe-server

Open TCP port 5666 on the monitored server:

sudo ufw allow from 192.168.1.10 to any port 5666 proto tcp

From the Nagios server, test NRPE:

/usr/lib/nagios/plugins/check_nrpe -H 192.168.1.20

Expected output:

NRPE v4.x

17. Configure NRPE Services

On the monitored server, edit:

sudo nano /etc/nagios/nrpe.cfg

Add or verify these command definitions:

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 5,4,3 -c 10,8,6
command[check_disk]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_procs]=/usr/lib/nagios/plugins/check_procs -w 250 -c 400

Definitions

-w

Warning threshold.

-c

Critical threshold.

For disk space:

-w 20%

Warning when less than 20% free space remains.

-c 10%

Critical when less than 10% free space remains.

Restart NRPE:

sudo systemctl restart nagios-nrpe-server

18. Add NRPE Checks to Nagios

On the Nagios server, edit:

sudo nano /etc/nagios4/objects/web-server.cfg

Add:

define service {
    use                     generic-service
    host_name               web-server
    service_description     Users
    check_command           check_nrpe!check_users
}

define service {
    use                     generic-service
    host_name               web-server
    service_description     System Load
    check_command           check_nrpe!check_load
}

define service {
    use                     generic-service
    host_name               web-server
    service_description     Root Disk
    check_command           check_nrpe!check_disk
}

define service {
    use                     generic-service
    host_name               web-server
    service_description     Processes
    check_command           check_nrpe!check_procs
}

Verify that the check_nrpe command exists:

grep -R "check_nrpe" /etc/nagios4 /etc/nagios-plugins

If the command is missing, add it to the Nagios command configuration:

sudo nano /etc/nagios4/objects/commands.cfg

Add:

define command {
    command_name    check_nrpe
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

The variable $USER1$ normally points to the Nagios plugins directory.


19. Configure Notifications

Open the contacts configuration:

sudo nano /etc/nagios4/objects/contacts.cfg

Example:

define contact {
    contact_name                    nagiosadmin
    use                             generic-contact
    alias                           Nagios Administrator
    email                           admin@example.com
    service_notification_commands   notify-service-by-email
    host_notification_commands      notify-host-by-email
}

A contact group can be defined as:

define contactgroup {
    contactgroup_name   admins
    alias               Nagios Administrators
    members             nagiosadmin
}

Then attach the group to a service:

define service {
    use                     generic-service
    host_name               web-server
    service_description     HTTP
    check_command           check_http
    contact_groups          admins
}

Email notifications require a working mail transfer agent, such as Postfix:

sudo apt install postfix mailutils -y

For a basic local setup, choose:

Local only

Check your distribution’s notification commands:

grep -R "notify-service-by-email" /etc/nagios4

20. Validate All Nagios Configuration

Always validate before restarting Nagios:

sudo nagios4 -v /etc/nagios4/nagios.cfg

Look for:

Total Errors: 0
Total Warnings: 0

Do not restart Nagios while configuration errors remain.


21. Restart Nagios and Apache

sudo systemctl restart nagios4
sudo systemctl restart apache2

Check both services:

sudo systemctl status nagios4
sudo systemctl status apache2

If Nagios fails to start, inspect the logs:

sudo journalctl -u nagios4 --no-pager -n 100

22. Access the Monitoring Dashboard

Open:

http://192.168.1.10/nagios4

Useful pages include:

Hosts
Services
Host Groups
Service Groups
Problems
Tactical Monitoring Overview

You should see:

nagios-server
web-server
PING status
HTTP status
SSH status
NRPE checks, if configured

23. Test a Failure

To test monitoring safely:

  1. Confirm the web server is currently OK.
  2. Stop its web service:
    sudo systemctl stop apache2
    
  3. Wait for the next check.
  4. Confirm that Nagios changes the HTTP service to CRITICAL.
  5. Start Apache again:
    sudo systemctl start apache2
    
  6. Nagios should eventually return the service to OK.

On the Nagios server:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 5666/tcp
sudo ufw enable

For better security, restrict NRPE access to only the Nagios server:

sudo ufw allow from 192.168.1.10 to any port 5666 proto tcp

On monitored Linux servers:

sudo ufw allow from 192.168.1.10 to any port 5666 proto tcp

Avoid exposing NRPE port 5666 to the public Internet.


25. Common Problems

Nagios does not start

Validate the configuration:

sudo nagios4 -v /etc/nagios4

Check logs:

sudo journalctl -u nagios4 -n 100

Host shows as DOWN

Check connectivity:

ping -c 4 192.168.1.20

Check firewall rules and routing.

HTTP check is CRITICAL

Test the web server manually:

curl -I http://192.168.1.20

Check whether Apache or Nginx is running:

sudo systemctl status apache2

NRPE connection refused

On the monitored server:

sudo systemctl status nagios-nrpe-server
sudo ss -tulpn | grep 5666

Check the allowed hosts setting:

grep allowed_hosts /etc/nagios/nrpe.cfg

It should include the Nagios server IP:

allowed_hosts=127.0.0.1,192.168.1.10

Plugin not found

Locate a plugin:

find /usr -name check_ping 2>/dev/null

Check the configured plugin path:

grep resource_file /etc/nagios4/nagios.cfg

26. Useful Nagios Commands

Command Purpose
sudo nagios4 -v /etc/nagios4/nagios.cfg Check configuration
sudo systemctl restart nagios4 Restart Nagios
sudo systemctl reload nagios4 Reload Nagios configuration
sudo tail -f /var/log/nagios4/nagios.log View Nagios logs
sudo systemctl status nagios4 View service status

Test a plugin manually:

/usr/lib/nagios/plugins/check_ping -H 192.168.1.20 -w 100,20% -c 500,60%

Test HTTP:

/usr/lib/nagios/plugins/check_http -H 192.168.1.20

Test SSH:

/usr/lib/nagios/plugins/check_ssh -H 192.168.1.20

Conclusion

After completing these steps, Nagios Core will monitor the server, website, SSH service, DNS service, and internal Linux resources such as disk usage, system load, users, and processes. You now have a full monitoring setup with alert notifications when anything goes wrong.

UbuntuMonitoringNagios

Share this post:

Table of Contents

NeeRoz M
NeeRoz M DevOps Engineer and Linux enthusiast sharing hands-on tutorials on server administration, Docker, Kubernetes, and cloud infrastructure.
comments powered by Disqus