Table of Contents

Netbox

Netbox is a service to document server-infrastrucure

Installation

  1. Install Postgresql
    # apt install postgresql libpq-dev
  2. Create database and user to access it
    $ sudo -u posgres psql
    psql (9.4.5)
    Type "help" for help.
    
    postgres=# CREATE DATABASE netbox;
    CREATE DATABASE
    postgres=# CREATE USER netbox WITH PASSWORD '<password>';
    CREATE ROLE
    postgres=# GRANT ALL PRIVILEGES ON DATABASE netbox TO netbox;
    GRANT
    postgres=# \q
  3. Install requirements for netbox
    # apt install -y python3 python3-pip python3-dev build-essential libxml2-dev libxslt1-dev libffi-dev graphviz libpq-dev libs
  4. download and extract the latest release
    $ wget https://github.com/digitalocean/netbox/archive/v2.5.10.tar.gz
    $ sudo tar -xzf v2.5.10.tar.gz -C /opt
  5. create a symlink to the new folder
    # ln -s /opt/netbox-2.5.10/ /opt/netbox
  6. Install python dependencies
    $ cd /opt/netbox/
    $ sudo pip3 install -r requirements.txt
  7. copy the example configuration file
    # cp netbox/netbox/configuration.example.py netbox/netbox/configuration.py
  8. generate a secret key
    # cd netbox
    # python3 generate_secret_key.py
  9. insert the key, aswell as your hostname, and posgres account into /opt/netbox/netbox/netbox/configuration.py
    ALLOWED_HOSTS = ['netbox.cm.in.tum.de', '172.24.25.68']
    
    DATABASE = {
        'NAME': 'netbox',               # Database name
        'USER': 'netbox',               # PostgreSQL username
        'PASSWORD': '<psql password>',  # PostgreSQL password
        'HOST': 'localhost',            # Database server
        'PORT': '',                     # Database port (leave blank for default)
    }
    
    SECRET_KEY = '<secret key>'
  10. Initialize the database
    # cd /opt/netbox/netbox
    # python3 manage.py migrate
  11. Create root user
     # python3 manage.py creeatesuperuser 
  12. Collect static files
     # python3 manage.py collectstatic --no-input 
  13. Install webserver
    # apt install nginx
  14. create /etc/nginx/sites-available/netbox with the following content:
    server {
        listen 80;
    
        server_name netbox.cm.in.tum.de;
    
        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 $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
    }
  15. Remove /etc/nginx/sites-enabled/default and create a symlink to the just created file
    # rm /etc/nginx/sites-enabled/default
    # ln -s /etc/nginx/sites-available/netbox /etc/nginx/sites-available/
  16. restart nginx
    # service nginx restart
  17. Install gunicorn and supervisor
    # pip3 install gunicorn
    # apt install supervisor
  18. create /opt/netbox/gunicorn_config.py with the following content:
    command = '/usr/local/bin/gunicorn'
    pythonpath = '/opt/netbox/netbox'
    bind = '127.0.0.1:8001'
    workers = 3
    user = 'www-data'
  19. create /etc/supervisor/conf.d/netbox.conf with the following content:
    [program:netbox]
    command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
    directory = /opt/netbox/netbox/
    user = www-data
    
    [program:netbox-rqworker]
    command = python3 /opt/netbox/netbox/manage.py rqworker
    directory = /opt/netbox/netbox/
    user = www-data
  20. Restart supervisor
    # service supervisor restart
  21. Netbox should now be available under the configured domain.

Configuration

LDAP Login

  1. Additional dependecnies need to be installed
    # apt install libldap2-dev libsasl2-dev libssl-dev
    # pip3 install django-auth-ldap
  2. create /opt/netbox/netbox/netbox/ldap_config.py with the following content:
    import ldap
    
    AUTH_LDAP_SERVER_URI = "ldaps://ldap.in.tum.de:636"
    
    from django_auth_ldap.config import LDAPSearch
    
    AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Personen, ou=IN, o=TUM, c=de", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
    
    AUTH_LDAP_USER_ATTR_MAP = {
            "first_name": "givenName",
            "last_name": "sn",
            "email": "mail"
    }
    
    from django_auth_ldap.config import LDAPSearch, PosixGroupType
    
    # This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group
    # hierarchy.
    AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=Gruppen, ou=IN, o=TUM, c=de", ldap.SCOPE_SUBTREE,
                                                "(objectClass=posixGroup)")
    AUTH_LDAP_GROUP_TYPE = PosixGroupType()
    
    # Define a group required to login.
    AUTH_LDAP_REQUIRE_GROUP = "cn=il11admin, ou=Gruppen, ou=IN, o=TUM, c=de"
    
    # Mirror LDAP group assignments.
    AUTH_LDAP_MIRROR_GROUPS = True
    
    # Define special user types using groups. Exercise great caution when assigning superuser status.
    AUTH_LDAP_USER_FLAGS_BY_GROUP = {
            "is_active": "cn=il11admin,ou=Gruppen,ou=IN,o=TUM,c=de",
            "is_staff": "cn=il11admin,ou=Gruppen,ou=IN,o=TUM,c=de",
            "is_superuser": "cn=il11admin,ou=Gruppen,ou=IN,o=TUM,c=de"
    }
    
    # For more granular permissions, we can map LDAP groups to Django groups.
    AUTH_LDAP_FIND_GROUP_PERMS = True
    
    # Cache groups for one hour to reduce LDAP traffic
    AUTH_LDAP_CACHE_GROUPS = True
    AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
  3. restart netbox
    # supervisorctl restart netbox