Netbox
Netbox
Netbox
Install
PostgreSQL Database
Installation
This section entails the installation and configuration of a local PostgreSQL database. If you already
have a PostgreSQL database service in place, skip to the next section.
NetBox requires PostgreSQL 10 or later. Please note that MySQL and other relational databases are
not supported.
Installation
◉○
UbuntuCentOS
Once PostgreSQL has been installed, start the service and enable it to run at boot:
psql -V
Database Creation
At a minimum, we need to create a database for NetBox and assign it a username and password
for authentication. Start by invoking the PostgreSQL shell as the system Postgres user.
Within the shell, enter the following commands to create the database and user (role), substituting
your own value for the password:
Do not use the password from the example. Choose a strong, random password to ensure
secure database authentication for your NetBox installation.
off)
netbox=> \conninfo
You are connected to database "netbox" as user "netbox" on host "localhost" (address
off)
netbox=> \q
If successful, you will enter a netbox prompt. Type \conninfo to confirm your connection, or type
\q to exit.
Redis Installation
Install Redis
Redis is an in-memory key-value store which NetBox employs for caching and queuing. This section
entails the installation and configuration of a local Redis instance. If you already have a Redis
service in place, skip to the next section.
NetBox v2.9.0 and later require Redis v4.0 or higher. If your distribution does not offer a recent
enough release, you will need to build Redis from source. Please see the Redis installation
◉○
UbuntuCentOS
Before continuing, verify that your installed version of Redis is at least v4.0:
redis-server -v
redis-cli ping
NetBox Installation
This section of the documentation discusses installing and configuring the NetBox application itself.
◉○
UbuntuCentOS
Before continuing, check that your installed Python version is at least 3.8:
python3 -V
Download NetBox
This documentation provides two options for installing NetBox: from a downloadable archive, or
from the git repository. Installing from a package (option A below) requires manually fetching and
extracting the archive for every future update, whereas installation via git (option B) allows for
seamless upgrades by re-pulling the master branch.
Note
It is recommended to install NetBox in a directory named for its version number. For example,
NetBox v3.0.0 would be installed into /opt/netbox-3.0.0 , and a symlink from /opt/netbox/ would
point to this location. (You can verify this configuration with the command ls -l /opt | grep
netbox .) This allows for future releases to be installed in parallel without interrupting the current
installation. When changing to the new release, only the symlink needs to be updated.
cd /opt/netbox/
◉○
UbuntuCentOS
Next, clone the master branch of the NetBox GitHub repository into the current directory. (This
branch always holds the current stable release.)
sudo git clone -b master --depth 1 https://github.com/netbox-community/netbox.git .
Note
The git clone command above utilizes a "shallow clone" to retrieve only the most recent commit.
If you need to download the entire history, omit the --depth 1 argument.
The git clone command should generate output similar to the following:
remote: Total 996 (delta 148), reused 386 (delta 34), pack-reused 0
Note
Installation via git also allows you to easily try out different versions of NetBox. To check out a
specific NetBox release, use the git checkout command with the desired release tag. For example,
git checkout v3.0.8 .
◉○
UbuntuCentOS
cd /opt/netbox/netbox/netbox/
Open configuration.py with your preferred editor to begin configuring NetBox. NetBox offers many
configuration parameters, but only the following four are required for new installations:
ALLOWED_HOSTS
DATABASE
REDIS
SECRET_KEY
ALLOWED_HOSTS
This is a list of the valid hostnames and IP addresses by which this server can be reached. You
must specify at least one name or IP address. (Note that this does not restrict the locations from
which NetBox may be accessed: It is merely for HTTP host header validation.)
If you are not yet sure what the domain name and/or IP address of the NetBox installation will be,
you can set this to a wildcard (asterisk) to allow all host values:
ALLOWED_HOSTS = ['*']
DATABASE
This parameter holds the database configuration details. You must define the username and
password used when you configured PostgreSQL. If the service is running on a remote host, update
the HOST and PORT parameters accordingly. See the configuration documentation for more detail
on individual parameters.
DATABASE = {
REDIS
Redis is a in-memory key-value store used by NetBox for caching and background task queuing.
Redis typically requires minimal configuration; the values below should suffice for most
installations. See the configuration documentation for more detail on individual parameters.
Note that NetBox requires the specification of two separate Redis databases: tasks and caching .
These may both be provided by the same Redis service, however each should have a unique
numeric database ID.
REDIS = {
'tasks': {
'DATABASE': 0, # Database ID
},
'caching': {
'HOST': 'localhost',
'PORT': 6379,
'PASSWORD': '',
'SSL': False,
}
SECRET_KEY
This parameter must be assigned a randomly-generated key employed as a salt for hashing and
related cryptographic functions. (Note, however, that it is never directly used in the encryption of
secret data.) This key must be unique to this installation and is recommended to be at least 50
characters long. It should not be shared outside the local system.
A simple Python script named generate_secret_key.py is provided in the parent directory to assist in
generating a suitable key:
python3 ../generate_secret_key.py
In the case of a highly available installation with multiple web servers, SECRET_KEY must be
identical among all servers in order to maintain a persistent user session state.
When you have finished modifying the configuration, remember to save the file.
Optional Requirements
All Python packages required by NetBox are listed in requirements.txt and will be installed
automatically. NetBox also supports some optional packages. If desired, these packages must be
listed in local_requirements.txt within the NetBox root directory.
NAPALM
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 /opt/netbox/upgrade.sh
Note that Python 3.8 or later is required for NetBox v3.2 and later releases. If the default
Python installation on your server is set to a lesser version, pass the path to the supported
installation as an environment variable named PYTHON . (Note that the environment variable must
be passed after the sudo command.)
Note
Upon completion, the upgrade script may warn that no existing virtual environment was detected.
As this is a new installation, this warning can be safely ignored.
Once the virtual environment has been activated, you should notice the string (venv) prepended
to your console prompt.
Next, we'll 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 very
strong password.
cd /opt/netbox/netbox
Next, connect to the name or IP of the server (as defined in ALLOWED_HOSTS ) on port 8000; for
example, http://127.0.0.1:8000/. You should be greeted with the NetBox home page. Try logging in
using the username and password specified when creating a superuser.
Note
By default RHEL based distros will likely block your testing attempts with firewalld. The
development server port can be opened with firewall-cmd (add --permanent if you want the rule to
survive server restarts):
The development server is for development and testing purposes only. It is neither performant nor
secure enough for production use. Do not use it in production.
Warning
If the test service does not run, or you cannot reach the NetBox home page, something has gone
wrong. Do not proceed with the rest of this guide until the installation has been corrected.
Gunicorn
Like most Django applications, NetBox runs as a WSGI application behind an HTTP server. This
documentation shows how to install and configure gunicorn (which is automatically installed with
NetBox) for this role, however other WSGI servers are available and should work similarly well.
uWSGI is a popular alternative.
Configuration
NetBox ships with a default configuration file for gunicorn. To use it, copy
/opt/netbox/contrib/gunicorn.py to /opt/netbox/gunicorn.py . (We make a copy of this file rather
than pointing to it directly to ensure that any local changes to it do not get overwritten by a future
upgrade.)
While the provided configuration should suffice for most initial installations, you may wish to edit
this file to change the bound IP address and/or port number, or to make performance-related
adjustments. See the Gunicorn documentation for the available configuration parameters.
systemd Setup
We'll use systemd to control both gunicorn and NetBox's background worker process. First, copy
contrib/netbox.service and contrib/netbox-rq.service to the /etc/systemd/system/ directory and
reload the systemd daemon:
Then, start the netbox and netbox-rq services and enable them to initiate at boot time:
You can use the command systemctl status netbox to verify that the WSGI service is running:
systemctl status netbox.service
Active: active (running) since Mon 2021-08-30 04:02:36 UTC; 14h ago
Docs: https://docs.netbox.dev/
Memory: 666.2M
CGroup: /system.slice/netbox.service
...
Note
If the NetBox service fails to start, issue the command journalctl -eu netbox to check for log
messages that may indicate the problem.
Once you've verified that the WSGI workers are up and running, move on to HTTP server setup.
Info
For the sake of brevity, only Ubuntu 20.04 instructions are provided here. These tasks are not
unique to NetBox and should carry over to other distributions with minimal changes. Please consult
your distribution's documentation for assistance if needed.
The command below can be used to generate a self-signed certificate for testing purposes,
however it is strongly recommended to use a certificate from a trusted authority in production. Two
files will be created: the public certificate ( netbox.crt ) and the private key ( netbox.key ). The
certificate is published to the world, whereas the private key must be kept secret at all times.
-keyout /etc/ssl/private/netbox.key \
-out /etc/ssl/certs/netbox.crt
The above command will prompt you for additional details of the certificate; all of these are
optional.
Once nginx is installed, copy the nginx configuration file provided by NetBox to /etc/nginx/sites-
available/netbox . Be sure to replace netbox.example.com with the domain name or IP address of
your installation. (This should match the value configured for ALLOWED_HOSTS in configuration.py .)
Option B: Apache
Begin by installing Apache:
Next, copy the default configuration file to /etc/apache2/sites-available/ . Be sure to modify the
ServerName parameter appropriately.
Finally, ensure that the required Apache modules are enabled, enable the netbox site, and reload
Apache:
Confirm Connectivity
At this point, you should be able to connect to the HTTPS service at the server name or IP address
you provided.
Info
Please keep in mind that the configurations provided here are bare minimums required to get
NetBox up and running. You may want to make adjustments to better suit your production
environment.
Warning
Certain components of NetBox (such as the display of rack elevation diagrams) rely on the use of
embedded objects. Ensure that your HTTP server configuration does not override the X-Frame-
Options response header set by NetBox.
Troubleshooting
If you are unable to connect to the HTTP server, check that:
If you are able to connect but receive a 502 (bad gateway) error, check the following:
The WSGI worker processes (gunicorn) are running ( systemctl status netbox should show
a status of "active (running)")
Nginx/Apache is configured to connect to the port on which gunicorn is listening (default is
8001).
SELinux is not preventing the reverse proxy connection. You may need to allow HTTP
network connections with the command setsebool -P httpd_can_network_connect 1
Upgrading to a New NetBox
Release
Python 3.8
PostgreSQL 10
Redis 4.0
wget https://github.com/netbox-community/netbox/archive/vX.Y.Z.tar.gz
Copy local_requirements.txt , configuration.py , and ldap_config.py (if present) from the current
installation to the new version:
Be sure to replicate your uploaded media as well. (The exact action necessary will depend on
where you choose to store your media, but in general moving or copying the media directory will
suffice.)
Also make sure to copy or link any custom scripts and reports that you've made. Note that if these
are stored outside the project root, you will not need to copy them. (Check the SCRIPTS_ROOT and
REPORTS_ROOT parameters in the configuration file above if you're unsure.)
If you followed the original installation guide to set up gunicorn, be sure to copy its configuration as
well:
cd /opt/netbox
If you need to upgrade to an older version rather than the current stable release, you can check
out any valid git tag, each of which represents a release. For example, to checkout the code for
NetBox v2.11.11, do:
sudo ./upgrade.sh
Warning
If the default version of Python is not at least 3.8, you'll need to pass the path to a supported
Python version as an environment variable when calling the upgrade script. For example:
Note
If the upgrade script prompts a warning about unreflected database migrations, this indicates that
some change has been made to your local codebase and should be investigated. Never attempt to
create new migrations unless you are intentionally modifying the database schema.
If you are upgrading from an installation that does not use a Python virtual environment (any
release prior to v2.7.9), you'll need to update the systemd service files to reference the new Python
and gunicorn executables before restarting the services. These are located in
/opt/netbox/venv/bin/ . See the example service files in /opt/netbox/contrib/ for reference.