Install RaNCID With Gitweb On Ubuntu 20.04 LTS

Wow! CENTOS 8 didn’t last long. As you may have heard by now CENTOS will be front-running RHEL  from now on and is no longer considered stable or Enterprise-ready by my company so we are re-standardizing on Ubuntu 20 LTS.

This post is a rehash of the previous “INSTALL RANCID WITH GITWEB ON CENTOS 8” article, configuring the same features for Ubuntu (Rancid w/ GIT; scheduling CRON; setting up email notifications; Apache w/ TLS & basic security). The sequence of the installations is a bit different so if you are not as familiar with Ubuntu as you are with CentOS pay attention, specifically to the Apache/SSL configuration which I find to be much more fragmented on Ubuntu.

I assume that the reader already has a working Ubuntu 20 LTS installation as well as a basic comfort level of navigating the Linux command line.

Server Preparation

This configuration requires among other packages: PERL; GIT; and POSTFIX. To install the required packages run the following from the command line:

apt-get install -y diffutils perl tcl expect git gitweb gcc make wget apache2

Setup a user to run RaNCID:

useradd -s /bin/bash -d /home/rancid/ -m -G sudo rancid
passwd rancid

Finally we download, configure and install the RaNCID package. To keep it simple we will use /opt/rancid for the configuration. Run the following:

wget https://shrubbery.net/pub/rancid/rancid-3.10.tar.gz -P /tmp
tar zxvf /tmp/rancid-3.10.tar.gz -C /tmp/
cd /tmp/rancid-3.10
./configure --prefix=/opt/rancid --localstatedir=/opt/rancid --with-git --host=localhost
make && make install

Assuming all was successful you now have an unconfigured RaNCID installation that uses GIT to capture configuration differences. To begin RaNCIDs configuration a file named .cloginrc needs be created and permissioned properly:

touch /opt/rancid/.cloginrc
chown -R rancid:rancid /opt/rancid/
chmod 0600 /opt/rancid/.cloginrc

Application Configuration

The .cloginrc file contains among other things the methods and credentials to connect to your devices, this will obviously need to be customized for your environment. Here are a few common options (you can read the man page here) to demonstrate RaNCIDs customization capabilities:

add cyphertype * aes128-ctr,aes128-cbc,3des-cbc
add userprompt HOST1 {.*\r\n.*\r\n.*\r\n.*\r\nHOST1>}
add userprompt HOST2 {.*\r\n.*\r\n.*\r\n.*\r\nHOST2>}
add user * admin
add password HOST1 Password EnablePassword
add password HOST2 Password EnablePassword
add password * Password EnablePassword
add method * ssh

The above sample configures specific encryption cyphers for all hosts; configures what output to expect at the user prompt for device HOST1 and device HOST2; adds the user admin for all devices; Configures a specific enable password for device HOST1 and device HOST2; adds an enable password for all other devices; and finally adds SSH as an authentication method.

RaNCID will manage our Cisco firewalls and switches using an application group named: CiscoDevices. Update the configuration /opt/rancid/etc/rancid.conf. This file is documented with comments
regarding the different options, for our purposes we need only add the following directives:

LIST_OF_GROUPS="CiscoDevices"
PATH=/opt/rancid/bin:/usr/bin:/usr/local/bin:/usr/sbin; export PATH

Depending on your needs you can have more than one group,  if you do please note the list is space delimited.

Next we create a symbolic link to the .cloginrc in the rancid user’s home directory and configure permissions on /opt/rancid and its contents:

ln -s /opt/rancid/.cloginrc /home/rancid/ 
chmod -R 750 /opt/rancid/*
chmod 0600 /opt/rancid/.cloginrc

Run RaNCID to generate the repository, you will execute the command in the context of the rancid user created earlier:

su - rancid
/opt/rancid/bin/rancid-cvs

The rancid-cvs script will establish your base repository and generate a file named router.db located: /opt/rancid/CiscoDevices/router.db. This file will maintain a list of the devices RaNCID will be connecting to, the records are semi-colon delimited and each entry begins with the device host name (must be resolvable via DNS or hosts file), followed by the device type, and ending with either an “up” or “down” status. For example:

dev-fw1.domain.com;cisco;up
dev-switch1.domain.com;cisco;up

To test your connectivity enter the “rancid” user’s command context and and try connecting to the device configured above by executing:

su - rancid
/opt/rancid/bin/clogin -f /opt/rancid/.cloginrc dev-fw1.domain.com

If everything has been successful so far you should be connected to the device CLI and can execute commands as if you had manually connected to it via SSH.

So far you can connect to RaNCID, it’s now time to initialize your GIT repository. This will create an empty record for each configured device which will be populated with their configurations upon subsequent executions:

su - rancid
/opt/rancid/bin/rancid-run

Ubuntu requires some SSH tweaking to communicate with my network devices properly and I needed to update /etc/ssh/ssh_config. Beneath the line “Host *” I add:

KexAlgorithms +diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1
StrictHostKeyChecking accept-new

Then restart SSH

systemctl restart sshd

RaNCID with GIT is now configured!

Automate With A CRON Job

I doubt you’re going to want to constantly connect to your RaNCID instance and manually run it so let’s automate – Cron is where Linux jobs are scheduled. Cron is an arcane art form unto itself and is beyond the scope of this article, for a good primer check out this article by Mark Drake from Digital Ocean (syntax is identical on Ubuntu and CentOS). Enter crontab using the rancid user context:
su - rancid
crontab -e

Add the line below to crontab to schedule RaNCID to execute hourly.

1 * * * * /opt/rancid/bin/rancid-run #hourly device dump

Save and exit (for those totally unfamiliar with vi commands, type :wq and press enter to save the changes and quit crontab).

Notifications

Ubuntu has a configuration applet that runs during installation, this was not the process on CentOS but the gist is to install the package and run through the wizard. First install Postfix:
apt-get -y install postfix

When the applet appears:

  • select Satellite system
  • enter FQDN of the RaNCID server
  • enter SMTP relay host

Should you need to make adjustments after the installation the configuration file is located: /etc/postfix/main.cf (NOTE: email server setup and options are numerous and will depend on your specific environment). My email infrastructure allows servers to relay if they are on a trusted subnet so “relayhost” will be the only directive I require.

 
Restart Postfix:
systemctl restart postfix
Now to configure the Postfix aliases RaNCID will use to send email. This is a two step process: the first is to update the /etc/aliases file; the second is to run the newaliases command to make Postfix aware of the changes. RaNCID is programmed to send to two different addresses per group specified in the  /opt/rancid/etc/rancid.conf file: an admin address for error messages; and a general group for operational notifications. The Admin alias should always be formatted as rancid-admin-GROUPNAME and the general alias rancid-GROUPNAME. As my groupname is CiscoDevices my aliases will be rancid-admin-CiscoDevices and rancid-CiscoDevices.

The file itself is colon delimited and ordered as alias: emailAddress. Mine looks like:

rancid-admin-CiscoDevices: admin@goldstein-solution-demos.com
rancid-CiscoDevices: rancidAdmins@goldstein-solution-demos.com

After you save the aliases make Postfix aware of the changes by running (make sure to run as root):

newaliases

Front End Setup

For viewability let’s configure Apache. The following commands: auto start the Apache daemon; enable SSL for Apache; add firewall rules allowing for inbound http and https:
systemctl enable apache2
systemctl daemon-reload
a2enmod ssl
systemctl restart apache2

ufw enable
ufw allow 80/tcp
ufw allow 443/tcp
ufw reload
The next step is configuring Apache to execute .cgi  files properly and to configure gitweb so it can be loaded. We do this be creating a new subdirectory in the webroot, enabling the Apache CGI mod and symlinking the gitweb files. We also make www-data the primary group for the rancid user:
mkdir /var/www/git

ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load
ln -s /usr/lib/cgi-bin/gitweb.cgi /var/www/git/gitweb.cgi
ln -s /usr/share/gitweb/static/ /var/www/git/static

usermod -g www-data rancid
Update the /etc/apache2/apache2.conf file, add the following code block:
AllowOverride None
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
Require all granted
Installing “gitweb” during the Server Preparation phase created a file named gitweb.conf located: /etc/gitweb.conf. We will use this file to provide our custom settings and site variables to gitweb (any variables directly edited in the gitweb program configuration will be overwritten during subsequent upgrades).The only directive required to get us running is our $projectroot so add a line similar to:
our $projectroot = "/opt/rancid/CiscoDevices";

Time to finalize permissions and restart Apache

chown rancid:www-data -R /opt/rancid/
chown rancid:www-data -R /var/www/
systemctl restart apache2

Securing The System

No system should be running without TLS protection and while the topic of TLS is too broad for this article, this command will get you running with a self-signed certificate (fill out the information you are prompted for):

openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt

Copy the defaul-ssl.conf file to the sites-enabled directory:

cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/

Update the new default-ssl.conf configuration file to load gitweb and use the self-signed cert. Update /etc/apache2/conf-available/default-ssl.conf:

DocumentRoot /var/www/git
DirectoryIndex gitweb.cgi
ServerName rancid.FQDN.com
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
Finalize Apache settings. Update /etc/apache2/sites-enabled/000-default.conf:
ServerAlias rancid.domain.com
Redirect permanent / https://rancid.domain.com/
 Update /etc/apache2/apache2.conf:
    AllowOverride None
    AuthType Basic
    AuthName "This Is A Restricted Site"
    AuthUserFile /var/www/git/.htpasswd
    Require valid-user
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .py

We will use the htpasswd command to create and manage credentials for our front-end users. We want the .htpasswd file to be generated in the root of the site we are protecting, in our case: /var/www/git. Once the file is created we need to update its permissions so that Apache can read it. Run the following  (type password twice when prompted):
htpasswd -c /var/www/git/.htpasswd rancid-www
chmod 644 /var/www/git/.htpasswd
One last Apache restart and we should be in business!
systemctl restart apache2
David Goldstein on Linkedin
David Goldstein
David has been innovating enterprise-class solutions for over two decades. His exposure to a variety of industries and regulatory regimes has given him the breadth of knowledge and hands-on experience to successfully navigate the current technology landscape and provide dependable solutions no matter how seemingly mundane or complex. Technologically agnostic with an operational bent, David's philosophy is firmly rooted in the concept of the "Data Custodian" and as such architects solution's with safety, accessibility, redundancy, and integrity of the data always at the top of mind.

As a holistic information fiduciary David leverages proven industry standards and best practices that will optimize your processes and systems to maximize performance and minimize complexity. David has managed projects in varying phases of development, be it greenfield builds or forklift moves, backup design and architecture, infrastructure monitoring and alerting, scripting and automation, communications and network infrastructure, policy review and auditing, rack and stacks and virtualization.

He doesn't just get IT, he groks IT.

4 Replies to “Install RaNCID With Gitweb On Ubuntu 20.04 LTS”

  1. Lalit Gupta says:

    very helpful document, it was an easy job post going through the document. Do you have something for RANCID & SVN integration ?

    Email: lalitgupta1977@gmail.com

  2. Lalit Gupta says:

    Thanks David
    I am currently on Ubuntu 20.04 and how I simply cannot get the front end for it to work with your broad instruction’s. I keep on landing on the default apache page post SSL implementation.

  3. David Goldstein David Goldstein says:

    What does your front end Apache configuration look like? I.e., websvn.conf. Also post the contents of your rancid.conf file, I’m particularly interested in what is assigned to the CVSROOT variable

Leave a Reply

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