(Last Updated On: January 13, 2019)

Introduction

NetBox is an open source web application designed to help manage and document computer networks. Initially conceived by the network engineering team at DigitalOcean,

“But godliness with contentment is great gain”
–Bible

Updated Article for Ubuntu 18.04:

How to Install NetBox on Ubuntu 18.04 LTS

For CentOS users use:

How to Install NetBox on CentOS 7 with Apache and Supervisord

Step One

Initial set-up of Ubuntu server.

This is in case you have a new installation in your premise
Create a new sudo user

$ sudo useradd tech

Add superuser privileges to your new user to enable the user tun administrative tasks without using root
The best way to do this on Ubuntu is by adding the new user to the “sudo” group which has administrative privileges by default

$ sudo usermod -aG sudo tech

Step Two

Installation of PostgreSQL

Let us update the system and install PostgreSQL

$ sudo apt-get update
$ sudo apt-get install -y postgresql libpq-dev

Check if PostgreSQL is running

[email protected]:~$ systemctl status postgresql
WARNING: terminal is not fully functional
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor 
   Active: active (exited) since Wed 2018-06-27 04:39:32 EDT; 26min ago
 Main PID: 11811 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/postgresql.service

Jun 27 04:39:32 pri systemd[1]: Starting PostgreSQL RDBMS...
Jun 27 04:39:32 pri systemd[1]: Started PostgreSQL RDBMS.
Jun 27 04:39:42 pri systemd[1]: Started PostgreSQL RDBMS.
lines 1-9/9 (END)

Step Three

Creation of a database and user for NetBox and grant all privileges to the user. The user is netbox and the user is netbox as well. Use your own password:

[email protected]:~$ sudo -u postgres psql

[sudo]

password for tech: psql (9.5.13) Type “help” for help.postgres=# CREATE DATABASE netbox; CREATE DATABASE postgres=# CREATE USER netbox WITH PASSWORD ‘example12’; CREATE ROLE postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox; GRANT postgres=# \q

Try logging into the database t ensure that it is working. Just enter the following command and pass it the password you had defined. For me, it is example12. A netbox prompt will ensue if successful.

[email protected]:~$ psql -U netbox -W -h localhost netbox
Password for user netbox: 
psql (9.5.13)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

netbox=> 

Step Four

Installation of NetBox Application.
First, we have to install the dependencies and packages including python3

$ sudo apt-get install -y python3 python3-dev python3-setuptools build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libssl-dev zlib1g-dev
$ sudo easy_install3 pip

After this, let us download NetBox from git and install it. Just in case git is not already installed, please install it.
Install git

[email protected]:~$ sudo apt-get install -y git                                                                                                               
Reading package lists... Done
Building dependency tree       
Reading state information... Done

Create the base NetBox directory

$ sudo mkdir -p /opt/netbox/
$ cd /opt/netbox/

After this, just cloneNetBox GitHub repository into the base folder we have created. Do not forget the trailing dot.

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

Next, change the ownership of /opt/netbox/netbox/media/ directory to your user.

[email protected]:/opt/netbox$ sudo chown -R tech:tech /opt/netbox/netbox/media/
[email protected]:/opt/netbox$ ls                                                  
CONTRIBUTING.md  mkdocs.yml            README.md         upgrade.sh
docs             netbox                requirements.txt
LICENSE.txt      old_requirements.txt  scripts

Python package Installation
Let us next install python packages in the requiements.txt file using pip. The packages are

[email protected]:/opt/netbox$ cat requirements.txt 
Django>=1.11,<2.0 django-cors-headers>=2.1.0
django-debug-toolbar>=1.9.0
django-filter>=1.1.0
django-mptt>=0.9.0
django-tables2>=1.19.0
django-timezone-field>=2.0
djangorestframework>=3.7.7
drf-yasg[validation]>=1.4.4
graphviz>=0.8.2
Markdown>=2.6.11
natsort>=5.2.0
ncclient==0.5.3
netaddr==0.7.18
paramiko>=2.4.0
Pillow>=5.0.0
psycopg2-binary>=2.7.4
py-gfm>=0.1.3
pycryptodome>=3.4.11
xmltodict>=0.11.0
$ pip3 install -r requirements.txt
You will see messages like below. Let them finish..
Collecting Django<2.0,>=1.11 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/25/4d/c8228419346a0e84aec202a43e181afc6572b861d38f8a0306dbce6abef0/Django-1.11.13-py2.py3-none-any.whl (6.9MB)

Step Five

Configure NetBox
After the completion of the python packages, the next step is to configure NetBox. The example configuration file is in /opt/netbox/netbox/netbox directory so change into the directory and make a copy of it

$ cd /opt/netbox/netbox/netbox
$ cp configuration.example.py configuration.py

Inside the configuration.py file, edit the following parts as shwn below:

ALLOWED_HOSTS= [‘ip or hostname of the server’]
DATABASE
SECRET_KEY

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

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

# This key is used for secure generation of random numbers and strings. It must never be exposed outside of this file.
# For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and
# symbols. NetBox will not run without this defined. For more information, see
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SECRET_KEY
SECRET_KEY = '[email protected]=%62B1-sCexf*secret*rdGzH^lQbcMNVo4Apu9Jvy7Y_F*w)3TthSP+'

For the secret key, just navigate to /opt/netbox/netbox and use the generate_secret_key.py script to generate it for you in the following way.

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

The secret key will be generated and should be kept secret. Paste it in the SECRET_KEY part of the conf file.
In case the database is on a different server, please place its IP Address in the HOST part of the DATABASE code block. The username and password are the nes you provided when installing the postgreSQL.
Because Netbox uses a datbase to run, a schema needs to be installed. To have that accomplished, just do the following:

$ cd /opt/netbox/netbox/
$ python3 manage.py migrate
[email protected]:/opt/netbox/netbox$ sudo python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, circuits, contenttypes, dcim, extras, ipam, secrets, sessions, tenancy, users, virtualization
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying tenancy.0001_initial... OK
  Applying dcim.0001_initial... OK
  

Step Six

User Creation and collection of static files for the webgui.
Since NetBox does not come with default users, we will have to create one user for the purposes of logging into the system when we launch it.

[email protected]:/opt/netbox/netbox$ sudo python3 manage.py create superuser
Unknown command: 'create'
Type 'manage.py help' for usage.
[email protected]:/opt/netbox/netbox$ sudo python3 manage.py createsuperuser 
Username (leave blank to use 'root'): tech
Email address: 
Password: 
Password (again): 
Superuser created successfully.
[email protected]:/opt/netbox/netbox$ sudo python3 manage.py collectstatic --no-input
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.woff'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.eot'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.ttf'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.svg'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/fonts/glyphicons-halflings-regular.woff2'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/js/bootstrap.min.js'
Copying '/opt/netbox/netbox/project-static/bootstrap-3.3.7-dist/js/npm.js
....................
.....................
279 static files copied to '/opt/netbox/netbox/static'.

NetBox provides initial data for you if you would wish to begin using it right away. In case you would wish to start everything on your own, you can skip this step

[email protected]:/opt/netbox/netbox$ sudo python3 manage.py loaddata initial_data
Installed 53 object(s) from 5 fixture(s)

Step Seven

Testing our fresh NetBox
This installtion is a test environment and I will be covering the entire set up later. Cruise around the test environment and see if it befits your needs. The test environment runs within a virtual environment as follows.

[email protected]:/opt/netbox/netbox$ sudo python3 manage.py runserver 0.0.0.0:8000 --insecure
Performing system checks...

System check identified no issues (0 silenced).
June 28, 2018 - 07:51:54
Django version 1.11.13, using settings 'netbox.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[28/Jun/2018 07:52:04] "GET /login/?next=/ HTTP/1.1" 200 14516
[28/Jun/2018 07:53:25] "POST /login/ HTTP/1.1" 302 0
[28/Jun/2018 07:53:26] "GET / HTTP/1.1" 200 37460

Now navigate to your browser and type in the IP address of your server plus the port number for example:
192.168.122.114:8000

Cheers if it worked and thank you for visiting the site.
Reference: http://netbox.readthedocs.io