Install and configure NetBox IPAM and DCIM tools on Ubuntu 20.04

To
You can download this article in PDF format to support us through the following link.

Download the guide in PDF format

turn off
To

The
The

A well-documented infrastructure shows the investment, organization, and expressiveness of the operations team within the organization. It helps train new employees and make their work easy, because it helps to create a mind map of every device in the data center. Gone are the days of drawing them on paper, printing them and pasting them on the wall. The beauty of open source tools such as NetBox makes this type of documentation easy and interesting again. This guide focuses on how to install NetBox in the new Ubuntu 20.04 (Focal Fossa).

Before we break ground, let us get acquainted with this tool. NetBox is an open source IPAM | DCIM web application for managing and recording computer networks and managing IP addresses. It was originally conceived by DigitalOcean’s network engineering team. The following graphically shows an overview of its components.

Thanks to the Netbox documentation page

Netbox includes the following network management aspects:

  • IP Address Management (IPAM)-IP network and address, VRF and VLAN
  • Equipment racks-organized by group and site
  • Equipment-equipment type and its installation location
  • Connections-network, console and power connections between devices
  • Virtualization-virtual machines and clusters
  • Data circuit-telecommunications circuit and provider
  • Confidential – encrypted storage of sensitive credentials

“One of the biggest tragedies in life is watching unexplored potential deaths.” -Myles Munroe

Step 1: Install the required dependencies

The best way to start is to install all the dependencies needed to run NetBox on Ubuntu 20.04 LTS. So let’s install all the components as shown below.

sudo apt-get install -y git gcc nginx redis s python3-setuptools  graphviz python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev

By the way, we will proceed to the next step to set up the database and other databases.

Step 2: Install and configure PostgreSQL

NetBox developers must like to use PostgreSQL to store data. Therefore, we will install and configure the PostgreSQL server in this step. Fortunately, we have a detailed guide to set up PostgreSQL. Please check the guide below to complete this operation.

Install PostgreSQL database server on Ubuntu 20.04 (Focal Fossa)

After successfully installing PostgreSQL, create a database and user for NetBox as shown below.

$ sudo -u postgres psql

CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'StrongPassword';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
q

Confirm that you can log in to the database as a netbox user.

$ psql -U netbox -h localhost -W

Password: 
psql (12.3 (Ubuntu 12.3-1.pgdg20.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

netbox=>

Step 3: Install and configure Netbox

Now that we dive into this guide, we can clone Netbox from git and start the process of configuring it for us. You can take a breath of fresh air and then immerse in the water again to continue.

Go to the /opt/ directory and clone netbox from git

cd /opt/
sudo git clone -b master https://github.com/digitalocean/netbox.git
cd netbox/netbox/netbox/
sudo cp configuration.example.py configuration.py

Edit the configuration file and set the allowed host and database login details

$ sudo vim configuration.py

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

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

Create NetBox user

We configured WSGI and HTTP services to run under the netbox user account. We will also assign ownership of the media catalog to this user. This ensures that NetBox can save local files.

sudo groupadd --system netbox
sudo adduser --system netbox
sudo chown --recursive netbox /opt/netbox/netbox/media/

Set up the Python environment

We will use the Python virtual environment to ensure that the packages required by NetBox will not conflict with any programs in the base system. This will create a directory called venv in our NetBox root directory.

cd /opt/netbox/
sudo python3 -m venv /opt/netbox/venv

Next, activate the virtual environment and install the required Python packages.

$ cd /opt/netbox/
$ source venv/bin/activate
(venv) [email protected]:/opt/netbox$

(venv) [email protected]:/opt/netbox$ sudo pip3 install -r requirements.txt
Collecting Django<3.1,>=3.0
  Downloading Django-3.0.8-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 1.7 MB/s

Key

Generate a random key containing at least 50 alphanumeric characters.

$ cd /opt/netbox/netbox
$ sudo ./generate_secret_key.py

-bUf2WxTdH^8%Kcl(_gnN1ym)[email protected]

Then set the key on the file /opt/netbox/netbox/netbox/configuration.py

example:

$ sudo vim /opt/netbox/netbox/netbox/configuration.py
SECRET_KEY = '-bUf2WxTdH^8%Kcl(_gnN1ym)[email protected]'

Run database migration

Before NetBox can run, we need to install the database architecture. This is done by running python3 manage.py to migrate from the netbox directory (/opt/netbox/netbox/ in this guide):

(venv) [email protected]:/opt/netbox/netbox$ cd /opt/netbox/netbox/netbox/
(venv) [email protected]:/opt/netbox/netbox$ sudo python3 manage.py migrate

Create a super user

NetBox does not come with any predefined user accounts. You need to create a super user to log in to NetBox:

(venv) [email protected]:/opt/netbox/netbox$ sudo python3 manage.py createsuperuser

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

Collect static files

(venv) [email protected]:/opt/netbox/netbox$ sudo python3 manage.py collectstatic --no-input

976 static files copied to '/opt/netbox/netbox/static'

Step 4: Install and configure gunicorn

In this guide, we will set up a simple WSGI front end using gunicorn.

Install gunicorn using pip3:

$ sudo pip3 install gunicorn
Collecting gunicorn
  Using cached gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
Requirement already satisfied: setuptools>=3.0 in /usr/lib/python3/dist-packages (from gunicorn) (45.2.0)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4

Configure Gunicorn

Copy /opt/netbox/contrib/gunicorn.py to /opt/netbox/gunicorn.py, and edit as needed.

$ sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
$ sudo vim /opt/netbox/gunicorn.py

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

# Number of gunicorn workers to spawn. This should typically be 2n+1, where
# n is the number of CPU cores present.
workers = 5

# Number of threads per worker process
threads = 3
command = '/usr/local/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
# Timeout (in seconds) for a request to complete
timeout = 120
#User
user = 'netbox'
# The maximum number of requests a worker can handle before being respawned
max_requests = 5000
max_requests_jitter = 500

Step 5: Configure Systemd

We will use systemd to control the daemon of the NetBox service. First, copy contrib/netbox.service and contrib/netbox-rq.service to the /etc/systemd/system/ directory as shown below:

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

Then, start the netbox and netbox-rq services and enable them to start at boot time:

sudo systemctl daemon-reload
sudo systemctl start netbox netbox-rq
sudo systemctl enable netbox netbox-rq

Step 6: Configure Nginx web server

Let’s configure the Nginx web server to help us access Netbox through a domain name instead of specifying an IP address and port. Create a new Nginx configuration file for Netbox.

## Back up the default file
$ cd /etc/nginx/sites-enabled/
$ sudo mv default /tmp

## Create a new file for Netbox
$ sudo vim /etc/nginx/sites-enabled/netbox.conf

Add the following configuration to the new Netbox file.

server {
    listen 80;
    server_name netbox.computingforgeeks.com;
    client_max_body_size 25m;

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

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

Open the /etc/hosts file, if there is no DNS.netbox.computingforgeeks.com 172.28.218.207, add a line containing the FQDN below.

Step 7: Access Netbox Web UI

Open the default web browser, and then open the Netbox server hostname. You will enter the homepage as a normal user. If you wish to make administrative changes, you must log in versus By clicking “log in” As follows.

Install and configure NetBox IPAM and DCIM tools on Ubuntu 20.04

This will redirect you to the login page. Enter the admin user you configured here.

Install and configure NetBox IPAM and DCIM tools on Ubuntu 20.04

Install and configure NetBox IPAM and DCIM tools on Ubuntu 20.04

in conclusion

We finally have a working Netbox instance for you to use. Enjoy using this tool to document your network infrastructure and improve your organization. For details on how to configure, please refer to the official Netbox documentation. Otherwise, thank you for coming and staying.

“Sad enough, the most painful farewells are those that have not been said, have never been explained.”-Freak Jonathan Hanish

More interesting guides:

Install and configure phpIPAM on Ubuntu/Debian Linux

Install Neat IP Address Planner (NIPAP) on Ubuntu and Debian

How to install NetBox IPAM on Debian Linux

How to install NetBox on Ubuntu 18.04 LTS

To
You can download this article in PDF format to support us through the following link.

Download the guide in PDF format

turn off
To

The
The

Sidebar