How to install NetBox Network Documentation and Management Tool on Ubuntu 20.04 LTS

Netbox is a free and powerful IP address (IPAM) and datacentre infrastructure management (DCIM) tool. It is used for storing information about your networks, VMs, inventories, and many more. It was originally developed by the network engineering team at DigitalOcean. This tool is written in Django Python framework and relies on PostgreSQL database. Its aim is to function as a domain-specific source of truth for network operations.

In this tutorial, we will explain how to install Netbox with Nginx as a reverse proxy on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

Getting Started

Before starting, you will need to install some dependencies required by Netbox. You can install all of them by running the following command:

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

Once all the packages are installed, you can proceed to the next step.

Install and Configure PostgreSQL Database

Netbox relies on PostgreSQL database for data storage. You can install it with the following command:

apt-get install postgresql postgresql-contrib -y

Once the PostgreSQL is installed, login to the PostgreSQL with the following command:

su - postgres
[email protected]:~$ psql

You should get the following output:

psql (12.2 (Ubuntu 12.2-4))
Type "help" for help.

Next, create a database and user for Netbox with the following command:

postgres=# CREATE DATABASE netbox;
postgres=# CREATE USER netbox WITH PASSWORD 'password';

Next, grant all the privileges to the Netbox database with the following command:

postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;

Next, exit from the PostgreSQL shell with the following command:

postgres=# exit
[email protected]:~$ exit

Install and Configure NetBox

First, change the directory to the /opt and download the latest version of Netbox from the Git Hub repository using the following command:

cd /opt/
git clone -b master https://github.com/digitalocean/netbox.git

Next, create a symbolic link of Python binary with the following command:

ln -s /usr/bin/python3 /usr/bin/python

Next, change the directory to /opt/netbox/netbox/ and generate Django SECRET Key by running the following command:

cd /opt/netbox/netbox/
./generate_secret_key.py

You should get the following output:

[email protected])eTDpo(k^f4Sm9bariUnK0syCPMGEIjW6XV_8l5xhB7z

Next, change the directory to netbox and rename the example configuration file:

cd netbox
mv configuration.example.py configuration.py

Next, edit the Netbox configuration file and define your database, secret key and allowed hosts:

nano configuration.py

Make the following changes:

ALLOWED_HOSTS = ['your-server-ip']

# PostgreSQL database configuration. See the Django documentation for a complete list of available parameters:
#   https://docs.djangoproject.com/en/stable/ref/settings/#databases
DATABASE = {
    'NAME': 'netbox',         # Database name
    'USER': 'netbox',               # PostgreSQL username
    'PASSWORD': 'password',           # PostgreSQL password
    'HOST': 'localhost',      # Database server
    'PORT': '',               # Database port (leave blank for default)
    'CONN_MAX_AGE': 300,      # Max database connection age
}

SECRET_KEY = '[email protected])eTDpo(k^f4Sm9bariUnK0syCPMGEIjW6XV_8l5xhB7z'

Save and close the file then install all Python dependencies with the following command:

pip3 install -r /opt/netbox/requirements.txt

Next, migrate the database with the following command:

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

Next, create a Netbox administrative user with the following command:

python3 manage.py createsuperuser

You will be asked to provide username and password as shown below:

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

Next, collect static file with the following command:

python3 manage.py collectstatic

You should see the following output:

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

Install and Configure Gunicorn

Netbox is a Django based application. So you will need to install Gunicorn in your system. You can install it by running the following command:

pip3 install gunicorn

After installing Gunicorn, create a new Gunicorn configuration file for Netbox with the following command:

nano /opt/netbox/gunicorn_config.py

Add the following lines:

command = '/usr/local/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = 'your-server-ip:8001'
workers = 3
user = 'www-data'

Save and close the file when you are finished.

Install and Configure Supervisor

Supervisor is a client/server system that allows you to monitor and control the NetBox service. You can create a new Supervisor configuration file for Netbox with the following command:

nano /etc/supervisor/conf.d/netbox.conf

Add the following lines:

[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = www-data

Save and close the file when you are done. Then, restart Supervisor service with the following command:

systemctl restart supervisor

You can also verify the status of Supervisor service using the following command:

systemctl status supervisor

You should get the following output:

? supervisor.service - Supervisor process control system for UNIX
     Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-05-30 05:49:08 UTC; 14s ago
       Docs: http://supervisord.org
   Main PID: 550606 (supervisord)
      Tasks: 5 (limit: 4691)
     Memory: 184.3M
     CGroup: /system.slice/supervisor.service
             ??550606 /usr/bin/python3 /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
             ??550626 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
             ??550628 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
             ??550629 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
             ??550630 /usr/bin/python3 /usr/local/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi

May 30 05:49:08 ubuntu2004 systemd[1]: Started Supervisor process control system for UNIX.
May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,664 CRIT Supervisor is running as root.  Privileges were not dropped becau>
May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,664 INFO Included extra file "/etc/supervisor/conf.d/netbox.conf" during p>
May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,671 INFO RPC interface 'supervisor' initialized
May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,671 CRIT Server 'unix_http_server' running without any HTTP authentication>
May 30 05:49:08 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:08,672 INFO supervisord started with pid 550606
May 30 05:49:09 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:09,676 INFO spawned: 'netbox' with pid 550626
May 30 05:49:11 ubuntu2004 supervisord[550606]: 2020-05-30 05:49:11,060 INFO success: netbox entered RUNNING state, process has stayed up for

Configure Nginx for NetBox

It is a good idea to configure Nginx as a reverse proxy to access the Netbox at port 80. You can create a new Nginx virtual host configuration with using the following command:

nano /etc/nginx/sites-available/netbox.conf

Add the following lines:

server {
    listen 80;
    server_name your-server-ip;
    client_max_body_size 25m;

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

    location / {
        proxy_pass http://your-server-ip:8001;
    }
}

Save and close the file. Then, create a symbolic link to the /etc/nginx/sites-enabled/ directory:

ln -s /etc/nginx/sites-available/netbox.conf /etc/nginx/sites-enabled/

Next, verify Nginx for any syntax error with the following command:

nginx -t

If everything is fine, you should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to implement the changes.

systemctl restart nginx

You can also verify the Nginx with the following command:

systemctl status nginx

You should get the following output:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-05-30 22:28:13 EST; 4min 14s ago
  Process: 984 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 982 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 980 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 985 (nginx)
    Tasks: 3 (limit: 25028)
   Memory: 5.5M
   CGroup: /system.slice/nginx.service
           ??985 nginx: master process /usr/sbin/nginx
           ??986 nginx: worker process
           ??987 nginx: worker process

May 30 21:28:12 ubuntu2004 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 30 21:28:12 ubuntu2004 nginx[982]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 30 21:28:12 ubuntu2004 nginx[982]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 30 21:28:13 ubuntu2004 systemd[1]: Started The nginx HTTP and reverse proxy server.

At this point, the Nginx web server is configured to serve Netbox on port 80. You can now proceed to access the Netbox web interface.

Access Netbox Web Interface

Open your web browser and visit the URL http://your-server-ip. You will be redirected to the following page:

Netbox interface

Click on the Log in button. You should see the Netbox login page in the following screen:

Netbox Login

Provide your Netbox admin username, password, and click on the login button. You should see the Netbox default dashboard in the following page:

Netbox admin dashboard

Conclusion

In this guide, you learned how to install Netbox on Ubuntu 20.04 with Nginx. You can now start documenting your network infrastructure. For more information, visit the Netbox official documentation. Fee free to ask me if you have any questions.

Share this page:

Suggested articles

2 Comment(s)

Add comment

Comments

By: Asau at: 2020-07-07 06:06:30

Is it same as NetDot?

By: Gijs at: 2020-07-09 16:27:47

Hi Hitesh,Nice article, one comment though. I'd recommend to replace this line:

update-alternatives --install /usr/bin/python python /usr/bin/python3 10

which is the way it should be set on ubuntu.

update-alternatives --install /usr/bin/python python /usr/bin/python3 10