You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

A well-documented infrastructure shows how committed, organized, and presentable the Operations Team is within your organization. It serves as a wonderful tool to train new staff as well as make their work painless since it helps create a mind map of every device in the datacenter. Gone are the days where they were drawn on paper, printed and stuck on the wall. The beauty of Open Source tools such as NetBox has made such documentation painless and fun once more. This guide focuses on installing NetBox in the new Ubuntu 20.04 (Focal Fossa).

Before we break the ground, let us get acquainted with this tool. NetBox is an open-source IPAM | DCIM web application used for managing and documenting computer networks and managing IP addresses. It was initially conceived by the network engineering team at DigitalOcean. An overview of its components is diagrammatically represented below.

Credits to Netbox Documentation page

Netbox encompasses the following aspects of network management:

  • IP address management (IPAM) – IP networks and addresses, VRFs, and VLANs
  • Equipment racks – Organized by group and site
  • Devices – Types of devices and where they are installed
  • Connections – Network, console, and power connections among devices
  • Virtualization – Virtual machines and clusters
  • Data circuits – Long-haul communications circuits and providers
  • Secrets – Encrypted storage of sensitive credentials

“One of the greatest tragedies in life is to watch potential die untapped.”
Myles Munroe

Step 1: Install required dependencies

The best way to begin is to have every dependency required to run NetBox on Ubuntu 20.04 LTS installed. Therefore, let us hit the road by installing all of them 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

With that out of the way, we shall proceed to the next steps to setup the database and others.

Step 2: Install and configure PostgreSQL

NetBox developers must have fallen for PostgreSQL for storing data. Because of that, we are going to install and configure PostgreSQL server in this step. Fortunately, we have a detailed guide that sets up PostgreSQL. Kindly check the guide below to have it done.

Install PostgreSQL Database Server on Ubuntu 20.04 (Focal Fossa)

After you have PostgreSQL successfully installed, 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 login to database as 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 we are in the deep end in this guide where we get to clone Netbox from git and begin the process of configuring it for us. You can take a gulf of fresh air then submerge yourself in once again to proceed.

Change to /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 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’ll configure the WSGI and HTTP services to run under netbox user account. We’ll also assign this user ownership of the media directory. This ensures that NetBox will be able to save local files.

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

Set Up Python Environment

We will use a Python virtual environment to ensure NetBox’s required packages don’t conflict with anything in the base system. This will create a directory named venv in our NetBox root.

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

SECRET_KEY

Generate a random secret key of 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 Migrations

Before NetBox can run, we need to install the database schema. This is done by running python3 manage.py 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’ll need to create a super user to be able to log into 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

We’ll set up a simple WSGI front end using gunicorn in this guide.

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 then edit it to fit your needs.

$ 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 daemonization of NetBox services. First, copy contrib/netbox.service and contrib/netbox-rq.service to the /etc/systemd/system/ directory as below:

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

Then, start the netbox and netbox-rq services and enable them to initiate 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 Nginx web server to help us access Netbox via Domain name rather than specifying an IP address and a port.
Create 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 configs below to 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 /etc/hosts file and add the line below containing your FQDN if you do not have a DNS.
netbox.computingforgeeks.com 172.28.218.207

Step 7: Access Netbox Web UI

Open your default web browser and open the Netbox server hostname. You will land on the first page as a normal user. If you would wish to make administrative changes, you will have to log in with the admin user created earlier by clicking on “Login” as shown below.

That will redirect you to the login page. Enter the admin user you configured there.

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. Refer to official Netbox documentation for more details about the how to go about anything you will need to configure. Otherwise, thank you for visiting and staying till the end.

“Sadly enough, the most painful goodbyes are the ones that are left unsaid and never explained.”
Jonathan Harnisch, Freak

More interesting guides:

Install and Configure phpIPAM on Ubuntu / Debian Linux

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

How To Install NetBox IPAM on Debian Linux

How To Install NetBox on Ubuntu 18.04 LTS

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF