We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Install NetBox IPAM & DCIM Tool on Ubuntu 22.04|20.04

Learn how to – Install NetBox IPAM & DCIM Tool on Ubuntu 22.04|20.04.

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 22.04|20.04.

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

Install NetBox IPAM on Ubuntu 22.04 / Ubuntu 20.04

Follow the steps provided in this section to install Netbox tool on Ubuntu 22.04 / Ubuntu 20.04 system.

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 mkdir /opt/netbox
sudo useradd -d /opt/netbox netbox -s /bin/bash
sudo passwd netbox
sudo usermod -aG sudo netbox
sudo chown -R netbox:netbox /opt/netbox

Step 1: Install required dependencies

The best way to begin is to have every dependency required to run NetBox on Ubuntu LTS installed. Therefore, let us hit the road by installing all of them as shown below.

sudo apt update
sudo apt install -y git gcc nginx redis 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.

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: <Enter-Password>
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

netbox=> exit

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.

Switch to netbox user:

sudo su - netbox

Change to /opt/ directory and clone netbox from git

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

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

$ vim configuration.py

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

# 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)
}

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 ~/
python3 -m venv /opt/netbox/netbox/venv

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

$ cd /opt/netbox/netbox/
$ source venv/bin/activate
(venv) tech@ubuntu:/opt/netbox$

(venv) tech@ubuntu:/opt/netbox$ pip3 install -r requirements.txt
  Downloading .....
     |████████████████████████████████| 7.5 MB 1.7 MB/s

SECRET_KEY

Generate a random secret key of at least 50 alphanumeric characters.

$ cd /opt/netbox/netbox/netbox
$ ./generate_secret_key.py
-bUf2WxTdH^8%Kcl(_gnN1ym)CEBoDGXe56Lv4FJrYzs@SthQO

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

Example:

$ vim /opt/netbox/netbox/netbox/netbox/configuration.py
SECRET_KEY = '-bUf2WxTdH^8%Kcl(_gnN1ym)CEBoDGXe56Lv4FJrYzs@SthQO'

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) netbox@jammy:~/netbox/netbox$ cd /opt/netbox/netbox/netbox
(venv) netbox@jammy:~/netbox/netbox$ 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) netbox@jammy:~/netbox/netbox$ python3 manage.py createsuperuser

Username (leave blank to use 'root'): admin
Email address: admin@ownyourlife.com.ng
Password: StrongPassword
Password (again): 
Superuser created successfully.

Collect Static Files

(venv) netbox@jammy:~/netbox/netbox$ python3 manage.py collectstatic --no-input
976 static files copied to '/opt/netbox/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:

$ pip3 install gunicorn
Collecting gunicorn
  Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 79.5/79.5 KB 2.2 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=3.0 in /usr/lib/python3/dist-packages (from gunicorn) (59.6.0)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.1.0

Configure Gunicorn

Copy /opt/netbox/contrib/gunicorn.py to /opt/netbox/gunicorn.py then edit it to fit your needs.

$ cp /opt/netbox/netbox/contrib/gunicorn.py /opt/netbox/netbox/gunicorn.py
$ vim /opt/netbox/netbox/gunicorn.py
# The IP address (typically localhost) and port that the Netbox WSGI process should listen on
bind = '127.0.0.1:8001'
#bind = '0.0.0.0: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

# 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

#Add below lines
user = 'netbox'
command = '/opt/netbox/netbox/venv/bin/gunicorn'
pythonpath = '/opt/netbox/netbox/netbox'

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/netbox/contrib/*.service /etc/systemd/system/

Update systemd unit files.

  • netbox.service
sudo tee /etc/systemd/system/netbox.service<<EOF
[Unit]
Description=NetBox WSGI Service
Documentation=https://docs.netbox.dev/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple

User=netbox
Group=netbox
PIDFile=/var/tmp/netbox.pid
WorkingDirectory=/opt/netbox/netbox

ExecStart=/opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py netbox.wsgi

Restart=on-failure
RestartSec=30
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
  • netbox-rq.service
sudo tee /etc/systemd/system/netbox-rq.service<<EOF
[Unit]
Description=NetBox Request Queue Worker
Documentation=https://docs.netbox.dev/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple

User=netbox
Group=netbox
WorkingDirectory=/opt/netbox/netbox

ExecStart=/opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/netbox/manage.py rqworker high default low

Restart=on-failure
RestartSec=30
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

Set permissions

sudo chown -R netbox:netbox /opt/netbox

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

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

Check services status:

$ systemctl status netbox netbox-rq
● netbox.service - NetBox WSGI Service
     Loaded: loaded (/etc/systemd/system/netbox.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-10-22 10:37:20 EAT; 3s ago
       Docs: https://docs.netbox.dev/
   Main PID: 8335 (gunicorn)
      Tasks: 6 (limit: 9460)
     Memory: 198.7M
        CPU: 1.905s
     CGroup: /system.slice/netbox.service
             ├─8335 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>
             ├─8337 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>
             ├─8338 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>
             ├─8339 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>
             ├─8340 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>
             └─8341 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox/netbox --config /opt/netbox/netbox/gunicorn.py>

Okt 22 10:37:20 jammy gunicorn[8335]: [2022-10-22 10:37:20 +0300] [8335] [INFO] Starting gunicorn 20.1.0
Okt 22 10:37:20 jammy gunicorn[8335]: [2022-10-22 10:37:20 +0300] [8335] [INFO] Listening at: http://127.0.0.1:8001 (8335)
Okt 22 10:37:20 jammy gunicorn[8335]: [2022-10-22 10:37:20 +0300] [8335] [INFO] Using worker: gthread
Okt 22 10:37:20 jammy gunicorn[8337]: [2022-10-22 10:37:20 +0300] [8337] [INFO] Booting worker with pid: 8337
Okt 22 10:37:21 jammy gunicorn[8338]: [2022-10-22 10:37:21 +0300] [8338] [INFO] Booting worker with pid: 8338
Okt 22 10:37:21 jammy gunicorn[8339]: [2022-10-22 10:37:21 +0300] [8339] [INFO] Booting worker with pid: 8339
Okt 22 10:37:21 jammy gunicorn[8340]: [2022-10-22 10:37:21 +0300] [8340] [INFO] Booting worker with pid: 8340
Okt 22 10:37:21 jammy gunicorn[8341]: [2022-10-22 10:37:21 +0300] [8341] [INFO] Booting worker with pid: 8341

● netbox-rq.service - NetBox Request Queue Worker
     Loaded: loaded (/etc/systemd/system/netbox-rq.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-10-22 10:37:20 EAT; 3s ago
       Docs: https://docs.netbox.dev/
   Main PID: 8336 (python3)
      Tasks: 1 (limit: 9460)
     Memory: 91.8M
        CPU: 1.902s
     CGroup: /system.slice/netbox-rq.service
             └─8336 /opt/netbox/netbox/venv/bin/python3 /opt/netbox/netbox/netbox/manage.py rqworker high default low

The service should be listening on port 8001

$ ss -tunelp | grep 8001
tcp   LISTEN 0      2048         0.0.0.0:8001       0.0.0.0:*    users:(("gunicorn",pid=8608,fd=5),("gunicorn",pid=8607,fd=5),("gunicorn",pid=8606,fd=5),("gunicorn",pid=8605,fd=5),("gunicorn",pid=8604,fd=5),("gunicorn",pid=8602,fd=5)) uid:1001 ino:66123 sk:11 cgroup:/system.slice/netbox.service <->

Step 6: Configure Nginx Web Server (Proxy)

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.ownyourlife.com.ng;
    client_max_body_size 25m;

    location /static/ {
        alias /opt/netbox/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;
    }
}

Restart nginx service:

sudo systemctl restart nginx

Open /etc/hosts file and add the line below containing your FQDN if you do not have a DNS.

172.28.218.207 netbox.ownyourlife.com.ng

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:

Cart

Your Cart is Empty

Back To Shop