Install and Configure NetBox on Ubuntu 20.04

Updated on June 28, 2021
Install and Configure NetBox on Ubuntu 20.04 header image

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

1. Install and configure PostgreSQL

  1. Install PostgreSQL.

     $ sudo apt install postgresql libpq-dev -y
  2. Start the database server.

     $ sudo systemctl start postgresql
  3. Enable the database server to start automatically on reboot.

     $ sudo systemctl enable postgresql
  4. Change the default PostgreSQL password.

     $ sudo passwd postgres
  5. Switch to the postgres user.

     $ su - postgres
  6. Log in to PostgreSQL.

     $ psql
  7. Create database netbox.

     CREATE DATABASE netbox;
  8. Create user netbox with password my_strong_password. Use a strong password in place of my_strong_password.

     CREATE USER netbox WITH ENCRYPTED password 'my_strong_password';
  9. Grant all the privileges on the netbox database to the netbox user.

     GRANT ALL PRIVILEGES ON DATABASE netbox to netbox;
  10. Exit PostgreSQL.

    \q
  11. 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.

  1. 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.

  1. 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
  2. Update pip to the latest version.

     $ sudo pip3 install --upgrade pip
  3. 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/
  4. Clone NetBox from official git repository to the current directory.

     $ sudo git clone -b master https://github.com/netbox-community/netbox.git .
  5. Create a system user named netbox.

     $ sudo adduser --system --group netbox
  6. Grant user netbox ownership of /opt/netbox/netbox/media/.

     $ sudo chown --recursive netbox /opt/netbox/netbox/media/
  7. Browse to the /opt/netbox/netbox/netbox/ directory.

     $ cd /opt/netbox/netbox/netbox/
  8. Copy example configuration file configuration.example.py to a configuration file configuration.py that we will use to configure the project.

     $ sudo cp configuration.example.py configuration.py
  9. Create a symbolic link of Python binary.

     $ sudo ln -s /usr/bin/python3 /usr/bin/python
  10. 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
  11. 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'
  12. Run the upgrade script.

    $ sudo /opt/netbox/upgrade.sh
  13. Enter the Python virtual environment.

    $ source /opt/netbox/venv/bin/activate
  14. Go to /opt/netbox/netbox directory.

    $ cd /opt/netbox/netbox
  15. Create a superuser account.

    $ python3 manage.py createsuperuser
  16. Reboot the system to apply the changes.

    $ sudo reboot

4. Configure Gunicorn

  1. 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

  1. Copy contrib/netbox.service and contrib/netbox-rq.service to the /etc/systemd/system/ directory.

     $ sudo cp /opt/netbox/contrib/*.service /etc/systemd/system/
  2. Reload daemon to enable the Systemd changes.

     $ sudo systemctl daemon-reload
  3. Start the netbox and netbox-rq services.

     $ sudo systemctl start netbox netbox-rq
  4. Enable the services to initiate at boot time.

     $ sudo systemctl enable netbox netbox-rq

6. Configure Nginx Web Server

  1. Install Nginx web server.

     $ sudo apt install -y nginx
  2. 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
  3. 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;
         }
     }
  4. Delete /etc/nginx/sites-enabled/default.

     $ sudo rm /etc/nginx/sites-enabled/default
  5. 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
  6. 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.