Install NetBox IPAM on Ubuntu: Step-by-Step Guide

This guide details installing and configuring NetBox IPAM on Ubuntu Linux. It covers system updates, dependency installations, PostgreSQL database setup, NetBox repository cloning, and initial configuration, including ALLOWED_HOSTS and SECRET_KEY settings. It guides through creating a superuser, setting up Gunicorn and Nginx for production, and accessing the server via a web browser. The open-source tool…

This article explains how to install and set up NetBox IPAM on Ubuntu Linux.

NetBox IPAM is a popular open-source IP address management (IPAM) and data center infrastructure management (DCIM) tool. It allows network administrators to centrally manage their IP addresses, devices, and racks.

Installing NetBox IPAM on Ubuntu Linux provides a secure and reliable platform for managing your IP addresses and data center infrastructure.

Ubuntu Linux is a widely used operating system with strong community support, and NetBox IPAM can be easily installed and configured.

Additionally, using an open-source tool like NetBox IPAM can help reduce costs and provide greater flexibility than proprietary IPAM solutions.

Prerequisites

Before you proceed with the installation, ensure that you have:

  • A server running Ubuntu Linux.
  • Sudo privileges on the server.
  • Basic knowledge of command-line operations and Linux environment.

Update and Upgrade the System

Begin by updating your package list and upgrading your system to ensure all existing packages are up to date.

sudo apt update
sudo apt upgrade

Restart your server if required.

Install Required Dependencies

NetBox has some dependencies you must install, including PostgreSQL, Redis, and other necessary software.

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 postgresql postgresql-contrib redis-server

Create PostgreSQL Database and User

Switch to the PostgreSQL prompt to create a NetBox database and a user.

sudo -u postgres psql

From the PostgreSQL prompt, execute:

CREATE DATABASE netbox;
CREATE USER netbox WITH PASSWORD 'netbox_user_password';
GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
ALTER DATABASE netbox OWNER TO netbox;
\q

Remember to replace netbox_user_password with a secure password.

Clone NetBox Repository

Clone the NetBox repository into the directory of your choice.

sudo mkdir -p /opt/netbox/
cd /opt/netbox/
sudo git clone -b master --depth 1 https://github.com/netbox-community/netbox.git .

Create a NetBox non-login account

First, execute the command below to create a new netbox user to run the NetBox installation.

sudo adduser --system --group netbox
sudo chown --recursive netbox /opt/netbox/netbox/media/

Configure NetBox

Next, navigate to the netbox directory and copy the example configuration file to create your own.

cd /opt/netbox/netbox/netbox/
sudo cp configuration_example.py configuration.py

Next, open configuration.py with your preferred editor to begin configuring NetBox.

sudo nano /opt/netbox/netbox/netbox/configuration.py

Set the ALLOWED_HOSTS, DATABASE, and SECRET_KEY settings to match your environment.

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

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

A simple Python script  generate_secret_key.py is provided in the parent directory to assist in generating a suitable key:

python3 ../generate_secret_key.py

Also, add the new secret to the SECRET_KEY section of the config file above.

SECRET_KEY = 'mnY(fx7RFfpV3yD#yEJ^Z8FOg&(hab7#KbYW+!u=J2HnL8ujk1'

Install Required Python Packages

Integration with the NAPALM automation library allows NetBox to fetch live data from devices and return it to a requester via its REST API. The NAPALM_USERNAME and NAPALM_PASSWORD configuration parameters define the credentials to be used when connecting to a device.

sudo sh -c "echo 'napalm' >> /opt/netbox/local_requirements.txt"

By default, NetBox will use the local filesystem to store uploaded files. To use a remote filesystem, install the django-storages library and configure your desired storage backend in configuration.py.

sudo sh -c "echo 'django-storages' >> /opt/netbox/local_requirements.txt"

Run the Upgrade Script

Once NetBox has been configured, run the packaged upgrade script (upgrade.sh) to perform the following actions:

  • Create a Python virtual environment
  • Installs all required Python packages
  • Run database schema migrations
  • Builds the documentation locally (for offline use)
  • Aggregate static resource files on disk
sudo /opt/netbox/upgrade.sh

Create Super User

NetBox does not come with any predefined user accounts. You must create a super user (administrative account) to log into NetBox.

First, enter the Python virtual environment created by the upgrade script:

source /opt/netbox/venv/bin/activate

Once the virtual environment has been activated, you should notice the string (venv) prepended to your console prompt.

Next, create a superuser account using the createsuperuser Django management command (via manage.py).

Specifying an email address for the user is not required, but be sure to use a strong password.

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

Follow the prompts to set up your superuser login.

Once done, run the command below to test your configuration.

python3 manage.py runserver 0.0.0.0:8000 --insecure

Open your browser and browse to the server hostname or IP address followed by port number 8000.

http://localhost:8000

The server is up and running. Howerver, when you type Ctrl+c , you will stop the server.

You will want to set up a reverse proxy to run it in a production environment.

Continue with the steps below to do that.

Collect Static Files

Collect the static files required for the web interface to function correctly.

python3 manage.py collectstatic --no-input

Configuration of Gunicorn

Install Gunicorn to serve the NetBox application.

pip3 install gunicorn

Then, create a Gunicorn systemd service file:

sudo nano /etc/systemd/system/netbox.service

Add the following configuration to the netbox.service file:

[Unit]
Description=NetBox WSGI Service
After=network.target

[Service]
Type=simple
User=netbox
Group=netbox
Environment="PATH=/opt/netbox/venv/bin"
ExecStart=/opt/netbox/venv/bin/gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi

[Install]
WantedBy=multi-user.target

Make sure to adjust paths if you installed NetBox in a different directory.

Configure Gunicorn

Next, you’ll need to create a Gunicorn configuration file at /opt/netbox/gunicorn_config.py.

sudo nano /opt/netbox/gunicorn_config.py

Add your Gunicorn config, which may look something like this:

command = '/opt/netbox/venv/bin/gunicorn'
pythonpath = '/opt/netbox/netbox'
bind = '0.0.0.0:8000'
workers = 3

Configure Nginx

Install Nginx and forward requests to Gunicorn.

sudo apt install nginx
sudo nano /etc/nginx/sites-available/netbox

Set up the server block:

server {
    listen 80;

    server_name netbox.example.com;

    client_max_body_size 25m;

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

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Remember to replace netbox.example.com with your domain name or server IP address.

Enable the NetBox site by linking the configuration file to the sites-enabled directory and restarting Nginx.

sudo ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-enabled/netbox
sudo systemctl restart nginx

Start NetBox and Gunicorn

Enable and start the NetBox service.

sudo systemctl daemon-reload
sudo systemctl enable netbox
sudo systemctl start netbox

Access NetBox

Now, you can access NetBox via your web browser by navigating to your server’s IP address or domain name. Log in with the superuser account you created to manage your instance.

http://netbox.example.com

That should do it!

Conclusion:

  • Successfully installing and setting up NetBox IPAM on Ubuntu Linux provides a secure and reliable platform for centrally managing IP addresses, devices, and racks within a data center infrastructure.
  • Utilizing the strong community support and flexibility of the widely used Ubuntu Linux operating system ensures a smooth installation and configuration process.
  • Embracing the open-source nature of NetBox IPAM offers cost reduction and greater flexibility than proprietary IPAM solutions.
  • The comprehensive guide has detailed the necessary prerequisites, installation of required dependencies, database and user creation, repository cloning, configuration, Python package installation, upgrade script execution, super user creation, static file collection, Gunicorn and Nginx configuration, and starting NetBox and Gunicorn services.
  • Upon completion, accessing NetBox via a web browser allows the management of the instance with the created superuser account.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *