HomeHOWTOSHow To Install NetBox IPAM/DCIM Tool on Ubuntu 22.04|20.04

How To Install NetBox IPAM/DCIM Tool on Ubuntu 22.04|20.04

By following this guide to the end, you should be able to install NetBox IPAM/DCIM Tool on Ubuntu 22.04|20.04.

When an organization has a well-documented infrastructure, it demonstrates the Operations Team’s dedication, coordination, and professionalism. By providing a visual representation of every device in the data centre, a well-documented infrastructure using tools like NetBox not only simplifies the work of new staff but also serves as a useful training tool. Unlike traditional methods of documentation that involved drawing diagrams on paper and posting them on walls, NetBox makes the documentation process easy and enjoyable with its open-source features. This guide is focused on installing NetBox in the latest Ubuntu versions, 22.04 and 20.04. Before we begin, let’s familiarize ourselves with this tool.

NetBox is a web-based, open-source tool that enables the management and documentation of computer networks at no cost.

Built on the Django framework and powered by PostgreSQL, NetBox offers a range of impressive features for network documentation and IP address management, including:

  • Connection Management – Interfaces/Console/Power
  • Single Converged Database
  • Circuit Provider Management
  • Rack Elevation
  • DCIM – Data Center Infrastructure Management
  • IPAM – IP Address Management
  • VRF Management
  • Report Alerting
  • Customization Header For logos etc
  • Vlan Management
  • Multi-Site (tenancy)

#1. Install Required Dependency Packages

We will begin by installing all the required dependency packages for Netbox.

Update your system:

sudo apt update -y

Instal the dependency packages:

sudo apt install -y python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev git

Ensure that you have Python 3.8 and later:

$ python3 -V
Python 3.8.10

Once complete, proceed with the below step.

#2. Install PostgreSQL on Ubuntu 22.04|20.04

Netbox uses PostgreSQL as its database. So we need to ensure that it is installed on the system before we proceed. Luckily, this page offers a detailed demonstration of how to install PostgreSQL on Ubuntu 22.04|20.04.

Once installed, we will create a database for Netbox. First access the PostgreSQL shell:

sudo -u postgres psql

Create a database as shown below:

CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'Passw0rd!';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;

Use CTRL+D to quit then ensure password authentication is allowed;

sudo sed -i '/^host/s/ident/md5/' /etc/postgresql/14/main/pg_hba.conf
sudo sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf

Verify that you can access the created database.

$ psql -U netbox -h localhost -W
psql (14.7 (Ubuntu 14.7-1.pgdg22.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
netbox=> exit

#3. Install Redis on Ubuntu 22.04|20.04

Redis will be used as an in-memory key-value store for NetBox. This is so vital for caching and queuing. To install Redi on Ubuntu 22.04|20.04 use the command:

sudo apt install -y redis-server

Check the installed version:

$ redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=66bd629f924ac924

Verify if Redis is working:

$ redis-cli ping
PONG

#4. Install and Configure NetBox on Ubuntu 22.04|20.04

Once all the above steps have been executed, we are now set to install NetBox on Ubuntu 22.04|20.04. We will clone the Netbox Git repo and begin configuring it as required.

Create a directory for Netbox:

sudo mkdir /opt/netbox && cd /opt/netbox

Now navigate to the /opt/ directory and clone Netbox:

sudo git clone -b master --depth 1 https://github.com/netbox-community/netbox.git .

Add a user for Netbox on Ubuntu 22.04|20.04

sudo useradd -d /opt/netbox netbox -s /bin/bash
sudo passwd netbox

Add the user to the sudo and group:

sudo usermod -aG sudo netbox

Give the user access to the created directory:

sudo chown -R netbox:netbox /opt/netbox

Now switch to the created user:

sudo su - netbox

Create a configuration file from the sample one:

cd /opt/netbox/netbox/netbox/
cp configuration_example.py configuration.py

Now we will modify the configuration file:

vim configuration.py

In the file, set the allowed host(‘*‘ if unsure or netbox.example.com‘ if you have a domain name)and database login details:

# Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local']
ALLOWED_HOSTS = ['*','127.0.0.1']
#ALLOWED_HOSTS = ['*']

# PostgreSQL database configuration.
DATABASE = {
    'NAME': 'netbox',                           # Database name you created
    'USER': 'netbox',                           # PostgreSQL username you created
    'PASSWORD': 'Passw0rd!',               # PostgreSQL password you set
    'HOST': 'localhost',                        # Database server
    'PORT': '',                                 # Database port (leave blank for default)
}

Also, make Redis cache configurations:

REDIS = {
    'tasks': {
        'HOST': 'localhost',      # Redis server
        'PORT': 6379,             # Redis port
        'USERNAME': '',        #Redis pusername(optional)
        'PASSWORD': '',           # Redis password (optional)
        'DATABASE': 0,            # Database ID
        'SSL': False,             # Use SSL (optional)
    },
    'caching': {
        'HOST': 'localhost',
        'PORT': 6379,
        'USERNAME': '',
        'PASSWORD': '',
        'DATABASE': 1,            # Unique ID for second database
        'SSL': False,
    }
}

Save the config and proceed.

Generate the Django Secret Key

Now generate a random secret key using the command:

$ python3 ../generate_secret_key.py
jD9-KWIT(4QMwF=j2yWePnl-!&)V%b7N)RH+CDus!e3&bx(C7F

This secret key needs to be provided in the configuration file. So open it and modify the value as shown:

$ vim configuration.py
SECRET_KEY = 'jD9-KWIT(4QMwF=j2yWePnl-!&)V%b7N)RH+CDus!e3&bx(C7F'

Run Nextbox Upgrade Script

The script performs several tasks that include:

  • Creating a Python virtual environment
  • Installing all required Python packages
  • Running database schema migrations
  • Building the documentation locally (for offline use)
  • Aggregating static resource files on disk

To run the script, use the command:

sudo /opt/netbox/upgrade.sh

Sample Output:

How To Install NetBox IPAM/DCIM Tool on Ubuntu 22.04|20.04

Create Netbox User Account.

Since Netbox doesn’t come with predefined user accounts, we need to create a superuser account. First, we need to active the Python environment:

source /opt/netbox/venv/bin/activate

From the Netbox directory execute the command:

cd /opt/netbox/netbox
python3 manage.py createsuperuser

Proceed as shown:

Username (leave blank to use 'netbox'): admin
Email address: [email protected]
Password: ***********
Password (again): ***********
Superuser created successfully.

Schedule Housekeeping Task

This task is used to handle the repetitive Netbox cleanup tasks. Create the corn job using the command:

sudo ln -s /opt/netbox/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping

Test Netbox Application

Now you can test if the installation is okay by starting Netbox locally with the command:

python3 manage.py runserver 0.0.0.0:8000 --insecure

Sample Output:

Performing system checks...

System check identified no issues (0 silenced).
May 01, 2023 - 10:48:55
Django version 4.1.8, using settings 'netbox.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Now you can verify if you are able to access Netbox using the URL http://localhost:8000

To stop the server press Ctrl+c

Configure the Gunicorn module

Netbox comes with a default Gunicorn config file. This can be copied to the Netbox path as shown:

sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
sudo cat /opt/netbox/gunicorn.py

Make sure that the file looks as below:

# The IP address (typically localhost) and port that the Netbox WSGI process should listen on
bind = '127.0.0.1:8001'

# Number of gunicorn workers to spawn. This should typically be 2n+1, where
workers = 5

# Number of threads per worker process
threads = 3

# Timeout (in seconds) for a request to complete
timeout = 120

# The maximum number of requests a worker can handle before being respawned
max_requests = 5000
max_requests_jitter = 500

#5. Create a systemd service file For Netbox

To be able to manage the Netbox service, we need to create a service file. We will copy the available config as shown:

sudo cp -v /opt/netbox/contrib/*.service /etc/systemd/system/

Reload the system daemon:

sudo systemctl daemon-reload

Now start and enable the Netbox service on Ubuntu 22.04|20.04

sudo systemctl start netbox netbox-rq
sudo systemctl enable netbox netbox-rq

Verify if the service is running:

$ systemctl status netbox.service
● netbox.service - NetBox WSGI Service
     Loaded: loaded (/etc/systemd/system/netbox.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-05-01 13:55:31 EAT; 12s ago
       Docs: https://docs.netbox.dev/
   Main PID: 25740 (gunicorn)
      Tasks: 6 (limit: 4575)
     Memory: 408.4M
     CGroup: /system.slice/netbox.service
             ├─25740 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─25743 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─25744 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─25745 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─25747 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             └─25748 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi

Cam 01 13:55:31 tutornix gunicorn[25740]: [2023-05-01 13:55:31 +0300] [25740] [INFO] Starting gunicorn 20.1.0
Cam 01 13:55:31 tutornix gunicorn[25740]: [2023-05-01 13:55:31 +0300] [25740] [INFO] Listening at: http://127.0.0.1:8001 (25740)

Check if the service is listening on port 8001 localhost:

$ sudo ss -plunt|grep 8001
tcp   LISTEN 0      2048        127.0.0.1:8001       0.0.0.0:*    users:(("gunicorn",pid=21744,fd=5),("gunicorn",pid=21743,fd=5),("gunicorn",pid=21742,fd=5),("gunicorn",pid=21741,fd=5),("gunicorn",pid=21740,fd=5),("gunicorn",pid=21739,fd=5))

#6. Install and Configure Reverse Proxy for Netbox

For this guide, we will use Nginx as the proxy server to allow us to access Netbox on the IP address/domain name anywhere on the network and not localhost.

Install Nginx with the command:

sudo apt install nginx -y

We need to create certificates to access Netbox using HTTPS. For this guide, we will generate self-signed SSL certs. Create a config:

$ vim netbox_ssl.conf
[req]
default_bits       = 2048
default_keyfile    = netbox_ssl.key
distinguished_name = req_distinguished_name
req_extensions     = req_ext
x509_extensions    = v3_ca

[req_distinguished_name]
countryName                 = Country Name (2 letter code)
countryName_default         = KE
stateOrProvinceName         = State or Province Name (full name)
stateOrProvinceName_default = Nairobi
localityName                = Locality Name (eg, city)
localityName_default        = Nairobi
organizationName            = Organization Name (eg, company)
organizationName_default    = netbox
organizationalUnitName      = organizationalunit
organizationalUnitName_default = Development
commonName                  =  Common Name (e.g. server FQDN or YOUR name)
commonName_default          = Your_IP-Address
commonName_max              = 64

[req_ext]
subjectAltName = @alt_names

[v3_ca]
subjectAltName = @alt_names

[alt_names]
DNS.1   = localhost
DNS.2   = Your_IP-Address

Now generate certs using the config:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout netbox_ssl.key -out netbox_ssl.crt -config netbox_ssl.conf

Move the certs to the required directory:

sudo mv netbox_ssl.crt /etc/ssl/certs/netbox.crt
sudo mkdir -p /etc/ssl/private/
sudo mv netbox_ssl.key /etc/ssl/private/netbox.key

Now create a virtual host for Netbox on Ubuntu 22.04|20.04 by copying the default config available:

sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox

Remove the default Nginx page and enable the site:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox

Open the file for editing:

sudo vim /etc/nginx/sites-available/netbox

Modify the file to accommodate your domain name:

server {
    listen [::]:443 ssl ipv6only=off;

    # CHANGE THIS TO YOUR SERVER'S NAME
    server_name netbox.tutornix.com;

    ssl_certificate /etc/ssl/certs/netbox.crt;
    ssl_certificate_key /etc/ssl/private/netbox.key;

    client_max_body_size 25m;

    location /static/ {
        alias /opt/netbox/netbox/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    # Redirect HTTP traffic to HTTPS
    listen :80 ipv6only=off;
    server_name _;
    return 301 https://$host$request_uri;
}

Save the file and restart the service:

sudo systemctl restart nginx

#7. Access NetBox IPAM/DCIM Tool Web Interface

Now we can access the NetBox IPAM/DCIM Tool web interface using the URL https://domain_name

Login by clicking on the button on the top right corner and provide the creds for the user we created earlier.

Once authenticated, you will see this page:

Now you are set to use Netbox as desired. You can now add the connections, devices, clusters, power supply etc.

For example, adding the first site.

Once the site is added, it will appear as shown.

Proceed and add a rack:

In the rack, you can then add, device roles, types etc, and add the devices on Netbox.

The Netbox UI is easy to navigate, now proceed and explore the dashboards.

Verdict

Today, we have learned how to install NetBox IPAM/DCIM Tool on Ubuntu 22.04|20.04. With Netbox, you can fully manage a data centre with ease. I hope this was informative.

See more on this page:

Klinsmann Öteyo
Klinsmann Öteyo
Self proclaimed Geek
- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here