Install and Configure NetBox on Ubuntu 20.04
Introduction
NetBox is an open-source, web-based Infrastructure Resource Modeling (IRM) application used for automating most networking operations. IP Address Management (IPAM) for managing IP addresses and Datacenter Infrastructure Management (DCIM) for managing and documenting computer networks are its major tools. In this article, well learn how to install and configure NetBox on Ubuntu 20.04.
Prerequisites
- Deploy a fully updated Ubuntu 20.04 LTS at Vultr with at least 2GB of RAM and 1 vCPU cores.
- Create a non-root user with sudo access.
1. Install and configure PostgreSQL
Install PostgreSQL.
$ sudo apt install postgresql libpq-dev -y
Start the database server.
$ sudo systemctl start postgresql
Enable the database server to start automatically on reboot.
$ sudo systemctl enable postgresql
Change the default PostgreSQL password.
$ sudo passwd postgres
Switch to the
postgres
user.$ su - postgres
Log in to PostgreSQL.
$ psql
Create database
netbox
.CREATE DATABASE netbox;
Create user
netbox
with passwordmy_strong_password
. Use a strong password in place ofmy_strong_password
.CREATE USER netbox WITH ENCRYPTED password 'my_strong_password';
Grant all the privileges on the
netbox
database to thenetbox
user.GRANT ALL PRIVILEGES ON DATABASE netbox to netbox;
Exit PostgreSQL.
\q
Return to your non-root sudo user account.
$ exit
2. Install Redis®
Redis® is an in-memory key-value store. NetBox uses it for caching and queuing.
Install Redis®.
$ sudo apt install -y redis-server
3. Install and configure NetBox
It's recommended to install NetBox from the official git repository to allows for seamless upgrades by re-pulling the master branch.
Install all the required packages.
$ sudo apt install python3 python3-pip python3-venv python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev libpq-dev libssl-dev zlib1g-dev git -y
Update
pip
to the latest version.$ sudo pip3 install --upgrade pip
We'll use
/opt/netbox/
as the installation directory. Create directory/opt/netbox/
and change to/opt/netbox/
directory.$ sudo mkdir -p /opt/netbox/ && cd /opt/netbox/
Clone NetBox from official git repository to the current directory.
$ sudo git clone -b master https://github.com/netbox-community/netbox.git .
Create a system user named
netbox
.$ sudo adduser --system --group netbox
Grant user
netbox
ownership of/opt/netbox/netbox/media/
.$ sudo chown --recursive netbox /opt/netbox/netbox/media/
Browse to the
/opt/netbox/netbox/netbox/
directory.$ cd /opt/netbox/netbox/netbox/
Copy example configuration file
configuration.example.py
to a configuration fileconfiguration.py
that we will use to configure the project.$ sudo cp configuration.example.py configuration.py
Create a symbolic link of Python binary.
$ sudo ln -s /usr/bin/python3 /usr/bin/python
Generate a random
SECRET_KEY
of at least 50 alphanumeric characters.$ sudo /opt/netbox/netbox/generate_secret_key.py
You will get a random secret similar to the bellow example. Copy it and save it somewhere. You will need it in the configuration file.
-^%YEl*Q2etCR6$kNG70H=&sM(45XvJaBWdf3O)inZ@L9j8_w1
Open and edit the configuration file
configuration.py
.$ sudo nano /opt/netbox/netbox/netbox/configuration.py
The final file should have the following configurations.
ALLOWED_HOSTS = ['*'] DATABASE = { 'NAME': 'netbox', # Database name you created 'USER': 'netbox', # PostgreSQL username you created 'PASSWORD': 'my_strong_password', # PostgreSQL password you set 'HOST': 'localhost', # Database server 'PORT': '', # Database port (leave blank for default) } SECRET_KEY = '-^%YEl*Q2etCR6$kNG70H=&sM(45XvJaBWdf3O)inZ@L9j8_w1'
Run the upgrade script.
$ sudo /opt/netbox/upgrade.sh
Enter the Python virtual environment.
$ source /opt/netbox/venv/bin/activate
Go to
/opt/netbox/netbox
directory.$ cd /opt/netbox/netbox
Create a superuser account.
$ python3 manage.py createsuperuser
Reboot the system to apply the changes.
$ sudo reboot
4. Configure Gunicorn
Copy
/opt/netbox/contrib/gunicorn.py
to/opt/netbox/gunicorn.py
.$ sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
5. Configure Systemd
Copy
contrib/netbox.service
andcontrib/netbox-rq.service
to the/etc/systemd/system/
directory.$ sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/
Reload daemon to enable the Systemd changes.
$ sudo systemctl daemon-reload
Start the
netbox
andnetbox-rq
services.$ sudo systemctl start netbox netbox-rq
Enable the services to initiate at boot time.
$ sudo systemctl enable netbox netbox-rq
6. Configure Nginx Web Server
Install Nginx web server.
$ sudo apt install -y nginx
Copy NetBox Nginx configuration file
nginx.conf
to/etc/nginx/sites-available/netbox
.$ sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netbox
Edit file
netbox
.$ sudo nano /etc/nginx/sites-available/netbox
Replace all the files content with the bellow code. Modify the
server_name
value with your server IP address:server { listen 80; # CHANGE THIS TO YOUR SERVER'S NAME server_name 192.0.2.10; client_max_body_size 25m; location /static/ { alias /opt/netbox/netbox/static/; } location / { proxy_pass http://127.0.0.1:8001; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; } }
Delete
/etc/nginx/sites-enabled/default
.$ sudo rm /etc/nginx/sites-enabled/default
Create a symlink in the sites-enabled directory to the
netbox
configuration file.$ sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox
Restart nginx service to enable the new configurations.
$ sudo systemctl restart nginx
Conclusion
You have successfully installed NetBox. You can now log in with the username and password you set while creating the superuser account. You can now begin configuring and managing your network components.