
Introduction
NetBox is an open-source, web-based Infrastructure Resource Modeling (IRM) application designed to automate various networking operations. When you install NetBox, you gain access to powerful tools like IP Address Management (IPAM) for managing IP addresses and Data Center Infrastructure Management (DCIM) for documenting and overseeing computer networks effectively.
In this article, we’ll explore how to install NetBox and configure it on Ubuntu 20.04 for effective network management and documentation.
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
-
$ sudo apt install postgresql libpq-dev -y Start the database server.
$ sudo systemctl start postgresqlEnable the database server to start automatically on reboot.
$ sudo systemctl enable postgresqlChange the default PostgreSQL password.
$ sudo passwd postgresSwitch to the
postgresuser.$ su - postgresLog in to PostgreSQL.
$ psqlCreate database
netbox.CREATE DATABASE netbox;Create user
netboxwith 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
netboxdatabase to thenetboxuser.GRANT ALL PRIVILEGES ON DATABASE netbox to netbox;Exit PostgreSQL.
\qReturn 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 -yUpdate
pipto the latest version.$ sudo pip3 install --upgrade pipWe'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 netboxGrant user
netboxownership 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.pyto a configuration fileconfiguration.pythat we will use to configure the project.$ sudo cp configuration.example.py configuration.pyCreate a symbolic link of Python binary.
$ sudo ln -s /usr/bin/python3 /usr/bin/pythonGenerate a random
SECRET_KEYof at least 50 alphanumeric characters.$ sudo /opt/netbox/netbox/generate_secret_key.pyYou 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_w1Open and edit the configuration file
configuration.py.$ sudo nano /opt/netbox/netbox/netbox/configuration.pyThe 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.shEnter the Python virtual environment.
$ source /opt/netbox/venv/bin/activateGo to
/opt/netbox/netboxdirectory.$ cd /opt/netbox/netboxCreate a superuser account.
$ python3 manage.py createsuperuserReboot the system to apply the changes.
$ sudo reboot
4. Configure Gunicorn
Copy
/opt/netbox/contrib/gunicorn.pyto/opt/netbox/gunicorn.py.$ sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
5. Configure Systemd
Copy
contrib/netbox.serviceandcontrib/netbox-rq.serviceto the/etc/systemd/system/directory.$ sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/Reload daemon to enable the Systemd changes.
$ sudo systemctl daemon-reloadStart the
netboxandnetbox-rqservices.$ sudo systemctl start netbox netbox-rqEnable the services to initiate at boot time.
$ sudo systemctl enable netbox netbox-rq
6. Configure Nginx Web Server
-
$ sudo apt install -y nginx Copy NetBox Nginx configuration file
nginx.confto/etc/nginx/sites-available/netbox.$ sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/sites-available/netboxEdit file
netbox.$ sudo nano /etc/nginx/sites-available/netboxReplace all the files content with the bellow code. Modify the
server_namevalue 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/defaultCreate a symlink in the sites-enabled directory to the
netboxconfiguration file.$ sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netboxRestart 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.