Installing NetBox on Rocky Linux 9.3

Rocky Linux 9.3
NetBox v4.1.7

Installation

  1. Make a system upgrade.

    sudo dnf check-update
    sudo dnf -y upgrade
  2. Disable SElinux.

    sudo setenforce 0
    sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
  3. Install python3.12 packages.

    sudo dnf -y install python3.12 python3.12-devel

    python3.12 -V
  4. Install and configure PostgreSQL database.

    Install postgresql-server package.

    sudo dnf -y install postgresql-server
    sudo postgresql-setup --initdb

    Modify /var/lib/pgsql/data/pg_hba.conf to support MD5 authentication.

    sudo sed -i '/^host    all/s/ident/md5/' /var/lib/pgsql/data/pg_hba.conf

    Start the service and enable it to run at boot.

    sudo systemctl enable --now postgresql

    psql -V

    Loign to PostgreSQL.

    sudo --login -u postgres psql

    Create netbox database.

    CREATE DATABASE netbox;
    CREATE USER netbox WITH PASSWORD 'J5brHrAXFLQSif0K';
    ALTER DATABASE netbox OWNER TO netbox;
    \q

    Verify service status.

    ~]$ psql --username netbox --password --host localhost netbox
    Password: J5brHrAXFLQSif0K
    psql (13.18)
    Type "help" for help.

    netbox=> \conninfo
    You are connected to database "netbox" as user "netbox" on host "localhost" (address "::1") at port "5432".
    netbox=> \q
  5. Install Redis.

    sudo dnf -y install redis
    sudo systemctl enable --now redis

    Verify service status.

    ~]$ redis-cli ping
    PONG
  6. Install NetBox.

    Install system packages.

    sudo dnf -y install gcc libxml2-devel libxslt-devel libffi-devel libpq-devel openssl-devel redhat-rpm-config git

    Download NetBox.

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

    Create the NetBox system user.

    sudo groupadd --system netbox
    sudo adduser --system -g netbox netbox
    sudo chown --recursive netbox /opt/netbox/netbox/media/
    sudo chown --recursive netbox /opt/netbox/netbox/reports/
    sudo chown --recursive netbox /opt/netbox/netbox/scripts/

    Modify configuration parameters.

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

    # ALLOWED_HOSTS
    sudo sed -i "/^ALLOWED_HOSTS/s/\[.*]/\['*']/" configuration.py

    # DATABASE
    sudo sed -i "/PostgreSQL username/s/'USER': '.*'/'USER': 'netbox'/" configuration.py
    sudo sed -i "/PostgreSQL password/s/'PASSWORD': '.*'/'PASSWORD': 'J5brHrAXFLQSif0K'/" configuration.py

    # SECRET_KEY
    SECRET_KEY=`python3.12 ../generate_secret_key.py`
    sudo sed -i "s/^SECRET_KEY.*/SECRET_KEY = '$SECRET_KEY'/" configuration.py

    Verify configuration.

    netbox]$ grep -Ev '^#|^$|^ *#' configuration.py
    ALLOWED_HOSTS = ['*']
    DATABASE = {
    'ENGINE': 'django.db.backends.postgresql', # Database engine
    'NAME': 'netbox', # Database name
    'USER': 'netbox',
    'PASSWORD': 'J5brHrAXFLQSif0K',
    'HOST': 'localhost', # Database server
    'PORT': '', # Database port (leave blank for default)
    'CONN_MAX_AGE': 300, # Max database connection age
    }
    REDIS = {
    'tasks': {
    'HOST': 'localhost',
    'PORT': 6379,
    'USERNAME': '',
    'PASSWORD': '',
    'DATABASE': 0,
    'SSL': False,
    },
    'caching': {
    'HOST': 'localhost',
    'PORT': 6379,
    'USERNAME': '',
    'PASSWORD': '',
    'DATABASE': 1,
    'SSL': False,
    }
    }
    SECRET_KEY = 'KSCyDtsb-X2pQNe3EJGrgH_%zThEv4(m7eHXEq*uhGQ#wtg6d*'

    Run the upgrade script.

    sudo PYTHON=/usr/bin/python3.12 /opt/netbox/upgrade.sh

    Create a super user.

    source /opt/netbox/venv/bin/activate
    cd /opt/netbox/netbox
    python3.12 manage.py createsuperuser

    Schedule the housekeeping task.

    sudo ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping

    (Option) Test the application.

    sudo firewall-cmd --zone=public --add-port=8000/tcp

    source /opt/netbox/venv/bin/activate
    cd /opt/netbox/netbox
    python3.12 manage.py runserver 0.0.0.0:8000 --insecure
  7. Gunicorn and systemd setup.

    Copy default configuration file for gunicorn.

    sudo cp /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py

    systemd setup.

    sudo cp -v /opt/netbox/contrib/*.service /etc/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable --now netbox netbox-rq
    systemctl status netbox netbox-rq
  8. HTTP server setup.

    Obtain an SSL certificate.

    sudo mkdir -p /etc/ssl/private/

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout /etc/ssl/private/netbox.key \
    -out /etc/ssl/certs/netbox.crt

    Install and configure nginx.

    sudo dnf -y install nginx
    sudo cp /etc/nginx/nginx.conf{,.bak}
    sudo sed -i '/^ server {/,/^ }/s/^/#/' /etc/nginx/nginx.conf
    sudo cp /opt/netbox/contrib/nginx.conf /etc/nginx/conf.d/netbox.conf
    sudo systemctl enable nginx --now
    systemctl status nginx

    sudo firewall-cmd --zone=public --add-service=https
    sudo firewall-cmd --zone=public --add-service=https --per

Plugins

netbox-attachments

source /opt/netbox/venv/bin/activate
sudo /opt/netbox/venv/bin/pip install netbox-attachments

sudo bash -c "cat >>/opt/netbox/netbox/netbox/configuration.py <<EOF

# Plugins
PLUGINS = ['netbox_attachments']
PLUGINS_CONFIG = {
'netbox_attachments': {
'apps': ['dcim', 'ipam', 'circuits', 'tenancy', 'virtualization', 'wireless', 'inventory_monitor'],
'display_default': \"right_page\",
'display_setting': {
'ipam.vlan': \"left_page\",
'dcim.device': \"full_width_page\",
'dcim.devicerole': \"full_width_page\",
'inventory_monitor.probe': \"additional_tab\"
}
}
}
EOF"

sudo mkdir -p /opt/netbox/netbox/media/netbox-attachments
sudo chown netbox /opt/netbox/netbox/media/netbox-attachments
cd /opt/netbox/netbox
python3.12 manage.py migrate netbox_attachments
sudo systemctl restart netbox netbox-rq

Troubleshooting

Has an fatal error: Python.h: No such file or directory while running the upgrade script.

4-cpython-312/psycopg_c/_psycopg.o
psycopg_c/_psycopg.c:42:10: fatal error: Python.h: No such file or directory
42 | #include "Python.h"
| ^~~~~~~~~~
compilation terminated.
error: command '/bin/gcc' failed with exit code 1
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for psycopg-c

Install python3.12-devel package.

sudo dnf install -y python3.12-devel

The netbox and netbox-rq service not found.

~]$ sudo systemctl restart netbox netbox-rq
[sudo] password for jove:
Failed to restart netbox.service: Unit netbox.service not found.
Failed to restart netbox-rq.service: Unit netbox-rq.service not found.

Those files need to be moved to your systemd's configuration directory from the contrib directory.

sudo cp -a /opt/netbox/*.service /etc/systemd/system/
sudo cp -a /opt/netbox/contrib/gunicorn.py /opt/netbox/

sudo systemctl daemon-reload
sudo systemctl restart netbox netbox-rq

The firewall-cmd command failed.

~]$ sudo firewall-cmd --zone=public --add-port=8000/tcp
Traceback (most recent call last):
File "/bin/firewall-cmd", line 24, in <module>
from gi.repository import GObject
ModuleNotFoundError: No module named 'gi'

The Rocky Linux 9.3 default python version is 3.9, modify /usr/bin/firewall-cmd shell bang to python3.9.

~]$ cat /usr/bin/firewall-cmd | head -n 1
#!/usr/bin/python3.9 -s

~]$ sudo firewall-cmd --zone=public --add-port=8000/tcp
success

The following static media file failed to load: setmode.js.

Check the static files exists in /etc/netbox/netbox/static/, and collect static files again.

cd /opt/netbox/netbox/
source /opt/netbox/venv/bin/activate
sudo python3.12 manage.py collectstatic

Reference

Official Installation Guide