RANCID と gitweb で作るコンフィグ履歴管理システム
ネットワーク機器のコンフィグの履歴を管理・追跡するソフトウェアとして、遅くとも 2003 年ごろから RANCID が存在しています。
最近の Web を見ていると他の OSS も登場していますが、RANCID + git で構築している例はあまり無いようです。
筆者も 2010 年頃に RANCID + CVS + ViewVC で構築していたのですが、git で新たに構築したので方法を記録しておきます。
うまく構築できると、以下のようにコンフィグ差分をブラウザから確認することができるようになります。

できること
Cisco を代表とするネットワーク機器のコンフィグを自動で取得し、差分があれば cvs / svn / git リポジトリに記録してくれます。
記録した差分はリポジトリビューアである gitweb により、外部 PC の Web ブラウザから確認できます。
外部の github / gitlab に頼らず単一のサーバで完結するため、OSS 間の相互接続・トラブルシュート・セキュリティをあまり考慮せずに作ることができます。
メリット
デフォルトで様々なメーカーに対応しており、ネットワーク機器側にはログインの設定を行う程度で、特別な設定は必要ありません。
- ネットワーク機器側で設定の変更を行うだけで、自動的に変更履歴が RANCID のリポジトリに保存されていきます
- ネットワーク機器の追加は、RANCID にホスト名や IP アドレス、ログイン情報を追加するだけです
RANCID はスクリプトで各機器のコマンドを取得しているため、取得するコマンドをカスタマイズすることもできます。
複数人で構築を行う場合、自動でコンフィグ差分が記録されていくと、障害発生時に追跡しやすく便利です。
- 構築中は大体その日の事前 / 事後ログくらいしか取らないので、障害に気づかないと日単位でしか記録に残らない
使用する主要なソフトウェア
RANCID
- ネットワーク機器で実行するコマンドのスクリプトなどを担当します。
git
- 通常はソフトウェア開発でバージョン管理に使用します。RANCID では、ネットワーク機器の設定を保存する役割を担います。
httpd (apache)
- 言わずとしれた Apache Software Foundation のWeb サーバです。
gitweb
- httpd (apache) と連携し、git リポジトリの内容を表示します。
- cvs / svn の頃に使われた ViewVC に近いです。
crond (cronie)
- 定期的に処理を行うソフトです。RANCID のスクリプトを定期的に実行します。
RANCID に関連する設定ファイル
/etc/rancid/rancid.conf
一番重要な設定ファイルです。 グループ名や、CVS / SVN / git の指定などを行います。
/var/rancid/.cloginrc
監視対象のネットワーク機器のホスト名や IP アドレス、RANCID からネットワーク機器にログインするためのユーザ名やパスワードなどを保存します。セキュリティ上危険なので、ファイルのアクセス権限に気をつけます。
/var/lib/rancid/<グループ名>/router.db
監視対象のネットワーク機器のホスト名や IP アドレス、動作状況を保存します。
/etc/cron.d/rancid
/etc/aliases
/etc/httpd/conf.d/viewvc.conf
クライアントのブラウザで表示させる際の、Web サーバの設定ファイルです。
RANCID の主なスクリプト
/usr/bin/rancid-cvs
/usr/bin/rancid-run
/usr/libexec/rancid/control_rancid
構築環境
EVE-NG 上に CentOS 8 と CSR1000V を建てて検証環境を構築しています。
CentOS 8 はリンク先のイメージを利用させてもらいました。
起動前に Linux を右クリック Edit から NIC を 4 つ + vnc -> telnet に変更してあります。

実際のトポロジではルータの配下にサーバが接続されることになるでしょうが、CSR1000V はライセンスなしだとスループットが制限されるため、上記のトポロジへアレンジしています。
構築手順
CentOS の設定
これ以降、
Linux
- $ は user or rancid ユーザ
- # はルートユーザでログイン
CSR
- $ は exec モード
- # は特権モード
にしていることを示します。
ルートパスワード変更・ルートログイン
上述の Serial Console telnet 対応の CentOS 8 イメージは、user / Test123 でログインできます。
root パスワードを任意に変更します。
Linux
$ sudo passwd root
$ su -
sudo でやっても良いですが、筆者はめんどいので root でやりました。
現用機に構築する際は sudo でやるべきです。
インターフェースの確認と有効化
EVE-NG 上の e3 は CentOS 8 の ens6 になっており、Internet には DHCP で Source NAT される環境と Bridge されています。
筆者と同じように構築するには、EVE-NG の External Connect 設定が必要です。
CentOS 8 の要件としては dnf / yum が実施できないといけないため、
- ping 8.8.8.8
- ping www.google.co.jp
で外部疎通と Global な名前解決ができれば良いです。
Linux
# nmcli -t device
# nmcli general status
# nmcli con up id ens6
ちょっと分かりづらいですが、NIC は以下の対応になっています。
NIC1 | NIC2 | NIC3 | NIC4 | |
---|---|---|---|---|
EVE-NG | e0 | e1 | e2 | e3 |
CenOS 8 | ens3 | ens4 | ens5 | ens6 |
CentOS の参照する DNF リポジトリを修正
CentOS 8 は Stream に移行したため、そのままでは dnf / yum が動作しません。
このため dnf / yum の参照リポジトリを書き換える必要があります。
ここの記事を参考にさせてもらいました。
Linux
# sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
# sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
RANCID のインストール
EPEL リポジトリを有効化
RANCID は CentOS の Base リポジトリに入っていません。まずは EPEL リポジトリを追加します。
Linux
# dnf install epel-release
RANCID 関連パッケージをインストール
RANCID / Apache などをインストールします。crond は Minimum でもインストール済みです。
Linux
# dnf install rancid git gitweb httpd telnet mlocate vim glibc-langpack-en
# updatedb
セキュリティ機能の停止
Firewall の確認・停止
検証環境として構築したので、Firewalld は今回止めています。
Linux
# systemctl status firewalld
# systemctl stop firewalld
# systemctl disable firewalld
必要なら適切に設定してください。
SELinux の確認と無効化
検証環境として構築したので、SELinux でも以下略。停止します。
Linux
# cat /etc/selinux/config
# cp -p /etc/selinux/config /etc/selinux/config.`date "+%Y%m%d-%H%M%S"`
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
# diff -c /etc/selinux/config.[0-9]* /etc/selinux/config
# reboot
SELinux の設定差分です。
*** /etc/selinux/config.20220916-210032 2020-06-27 09:00:09.058770191 -0400
--- /etc/selinux/config 2022-09-16 21:02:05.016796511 -0400
***************
*** 4,10 ****
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
! SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
--- 4,10 ----
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
! SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
[root@centos8 ~]#
RANCID の構築 その 1
RANCID ディレクトリの作成と権限変更
root で実施します。
mysql / mariadb などでは /var/lib 配下に DB が保存されるため、RANCID でも倣って作成しました。
Linux
# mkdir -p /var/lib/rancid
# chown rancid:apache /var/lib/rancid
RANCID の設定
rancid.conf を日付付きでバックアップ、編集します。
Linux
# cp -p /etc/rancid/rancid.conf /etc/rancid/rancid.conf.`date "+%Y%m%d-%H%M%S"`
# vi /etc/rancid/rancid.conf
# diff -c /etc/rancid/rancid.conf.`date "+%Y%m%d-%H%M"`* /etc/rancid/rancid.conf
diff コマンドではファイルを日時指定で比較元としていますが、同日に作業していない場合は、適宜ファイル名を変更して実施してください。
行先頭の "!" や "+" は diff コマンドがつけているものなので、設定では記載しないようにしてください。
- ! = 差分があることを示す
- + = 追加されたことを示す
- - = 削除されたことを示す
rancid.conf の変更差分は以下です。
*** /etc/rancid/rancid.conf.20220916-210843 2022-02-07 17:52:49.000000000 -0
--- /etc/rancid/rancid.conf 2022-09-16 21:12:27.710460964 -0400
***************
*** 25,31 ****
TMPDIR=/tmp; export TMPDIR
# Be careful changing this, it affects CVSROOT below. It should be a FQPN, nt
# relative.
! BASEDIR=/var; export BASEDIR
PATH=/usr/libexec/rancid:/usr/bin:/usr/sbin:/usr/local/bin; export PATH
#
# Check that the effective UID is not root, which could lead to permissions
--- 25,32 ----
TMPDIR=/tmp; export TMPDIR
# Be careful changing this, it affects CVSROOT below. It should be a FQPN, nt
# relative.
! #BASEDIR=/var; export BASEDIR
! BASEDIR=/var/lib/rancid; export BASEDIR
PATH=/usr/libexec/rancid:/usr/bin:/usr/sbin:/usr/local/bin; export PATH
#
# Check that the effective UID is not root, which could lead to permissions
***************
*** 56,69 ****
# - for git, it is likely easiest to leave CVSROOT as a local path, use
# rancid-cvs to create the repository for groups, then move those
# repositories to the remote and update the group's .git/config.
! CVSROOT=$BASEDIR/CVS; export CVSROOT
# Location of log files produced by rancid-run(1).
LOGDIR=$BASEDIR/logs; export LOGDIR
#
# Select which RCS system to use, "cvs" (default), "svn" or "git". Do not
# change this after CVSROOT has been created with rancid-cvs. Changing betwen
# these requires manual conversions.
! RCSSYS=cvs; export RCSSYS
#
# if ACLFILTERSEQ is NO, prefix-list/access-list sequence numbers will NOT be
# filtered. this option implies ACLSORT=NO for lists with sequence numbers.
--- 57,72 ----
# - for git, it is likely easiest to leave CVSROOT as a local path, use
# rancid-cvs to create the repository for groups, then move those
# repositories to the remote and update the group's .git/config.
! #CVSROOT=$BASEDIR/CVS; export CVSROOt
! CVSROOT=$BASEDIR/GIT; export CVSROOT
# Location of log files produced by rancid-run(1).
LOGDIR=$BASEDIR/logs; export LOGDIR
#
# Select which RCS system to use, "cvs" (default), "svn" or "git". Do not
# change this after CVSROOT has been created with rancid-cvs. Changing betwen
# these requires manual conversions.
! #RCSSYS=cvs; export RCSSYS
! RCSSYS=git; export RCSSYS
#
# if ACLFILTERSEQ is NO, prefix-list/access-list sequence numbers will NOT be
# filtered. this option implies ACLSORT=NO for lists with sequence numbers.
***************
*** 79,84 ****
--- 82,88 ----
# if NOCOMMSTR is set to YES, snmp community strings will be stripped from the
# configs.
#NOCOMMSTR=YES; export NOCOMMSTR
+ NOCOMMSTR=ALL; export NOCOMMSTR
#
# FILTER_OSC determines if oscillating data such as keys, passwords, etc are
# filtered from configs by the value set (NO | YES | ALL). FILTER_PWDS may
***************
*** 105,110 ****
--- 109,115 ----
#LIST_OF_GROUPS="sl joebobisp"; export LIST_OF_GROUPS
# more groups...
#LIST_OF_GROUPS="$LIST_OF_GROUPS noc billybobisp"; export LIST_OF_GROUPS
+ LIST_OF_GROUPS="TKY"
#
# Define an alternate filter for the output of the RCS diff. The filter
# should read from stdin and write to stdout. The default is defined in
[root@centos8 ~]#
各項目の役割は以下のとおりです。
- BASEDIR=
- RANCID のベースとなるディレクトリ指定します。
- CVSROOT=
- cvs / svn / git で設定履歴を保存するディレクトリを指定します。
- LOGDIR=
- RANCID のログを保存するディレクトリを指定します。Fedora や CentOS では /var/log 以下が標準で使用するログのディレクトリなので、変更しても良いでしょう。
以下の設定はセキュリティ上重要な項目です。Cisco 機器などでは設定ファイルからパスワードが解読できるため、RANCID の設定が不適切な場合、パスワードが漏れる可能性があります。
- FILTER_PWDS=
- ALL を指定することで、ネットワーク機器の設定ファイルの、username <> password <> や、enable secret <> <> などのパスワードを <removed> に置換します。
- NOCOMMSTR=
- ネットワーク機器の設定ファイルの、snmp-server のコミュニティ名を <removed> に置換します。
- LIST_OF_GROUPS=
- ネットワーク機器をまとめるグループ名を指定します。ネットワーク機器のメーカー名や、L2_switch や FW などの役割を指定するといいでしょう。筆者の現用環境では拠点をグループとして設定しています。ここでは Tokyo を意味する TKYを指定します。
- 複数指定する場合は、TKY YKH のようにスペースで区切ります。
英語ですが、詳細な説明は rancid.conf - rancid environment configuration file にあります。man rancid.conf で確認しましょう。
cloginrc のコピー
Linux
# cp -p /usr/share/doc/rancid/cloginrc.sample /var/rancid/.cloginrc
# vi /var/rancid/.cloginrc
# diff -c /usr/share/doc/rancid/cloginrc.sample /var/rancid/.cloginrc
cloginrc は既存の行をコメントアウトして、新規に行を作成します。
*** /usr/share/doc/rancid/cloginrc.sample 2019-05-11 22:29:06.000000000 -0
--- /var/rancid/.cloginrc 2022-09-16 21:17:52.451049484 -0400
***************
*** 86,128 ****
# customer x
# these routers ask for a username and password. we automatically get
# enable access after successful authentication.
! add user *.custx.net roger
! add password *.custx.net {doger}
! add autoenable *.custx.net 1
# customer y
# this is the normal cisco login. a password followed by and enable password.
# try ssh first, then rlogin.
! add password *.custy.net {vector} {victor}
! add method *.custy.net ssh rlogin
# customer z; they use ssh only.
! add user *.custz.net shirley
! add password *.custz.net {jive} {surely}
! add method *.custz.net ssh
# the route-server's do not provide enable access. cmdline -noenable
# equivalent.
! add noenable route-server* 1
# all our routers, i.e.: everything else
! add password * {clearance} {clarence}
# set ssh encryption type, dflt: 3des
! add cyphertype * {3des}
# set the username prompt to "router login:"
#add userprompt * {"router login:"}
# ssh identity for a juniper; used with jlogin
! add identity my.juniper $env(HOME)/.ssh/juniper
# riverstone / enterasys / cabletron (rivlogin) example
# these boxes are 'back-to-front' from cisco (i.e., ask
# for vty password always, then tac+/radius if configured).
#
# vty password and last resort (enable) password for rivlogin
! add password rs3000 {vtypass} {lastresort}
# if using tac+ or radius login, include these lines
! add user rs3000 {monster}
! add userpassword rs3000 {scary}
--- 86,134 ----
# customer x
# these routers ask for a username and password. we automatically get
# enable access after successful authentication.
! #add user *.custx.net roger
! #add password *.custx.net {doger}
! #add autoenable *.custx.net 1
# customer y
# this is the normal cisco login. a password followed by and enable password.
# try ssh first, then rlogin.
! #add password *.custy.net {vector} {victor}
! #add method *.custy.net ssh rlogin
# customer z; they use ssh only.
! #add user *.custz.net shirley
! #add password *.custz.net {jive} {surely}
! #add method *.custz.net ssh
# the route-server's do not provide enable access. cmdline -noenable
# equivalent.
! #add noenable route-server* 1
# all our routers, i.e.: everything else
! #add password * {clearance} {clarence}
# set ssh encryption type, dflt: 3des
! #add cyphertype * {3des}
# set the username prompt to "router login:"
#add userprompt * {"router login:"}
# ssh identity for a juniper; used with jlogin
! #add identity my.juniper $env(HOME)/.ssh/juniper
# riverstone / enterasys / cabletron (rivlogin) example
# these boxes are 'back-to-front' from cisco (i.e., ask
# for vty password always, then tac+/radius if configured).
#
# vty password and last resort (enable) password for rivlogin
! #add password rs3000 {vtypass} {lastresort}
# if using tac+ or radius login, include these lines
! #add user rs3000 {monster}
! #add userpassword rs3000 {scary}
!
! add user * user
! add password * password password
! add method * telnet
!
!
[root@centos8 ~]#
user を設定しましたが、今回は IOS-XE でユーザ認証を使用していない (vty + enable だけ) ため、使いませんでした。。 ネットワーク機器へ SSH などでログインするように設定したい場合、以下を参考にしてみてください。
# vi /var/rancid/.cloginrc
#ログインパスワードと特権パスワードの例
#+add password <ホスト名> <ログインパスワード> <特権パスワード>
#ログインしたときに自動的に特権、な例
#+add autoenable <ホスト名> 1
#+add password <ホスト名> <パスワード>
#ログインしたときにユーザ名を聞かれるケースで、自動的な特権はナシ
#+add user <ホスト名> <ユーザ名>
#+add autoenable <ホスト名> 0
#+add password <ホスト名> <ログインパスワード> <特権パスワード>
#telnetでなく、 sshを優先させる例
#+add method <ホスト名> {ssh}
#+add user <ホスト名> <ユーザ名>
#+add autoenable <ホスト名> 1
#+add password <ホスト名> <パスワード>
CentOS の CSR1000V 設定
CSR1000V を hosts に登録
IP アドレスでアクセスすると、ホストの役割がわからなかったり IP アドレスを変更した際にリポジトリの一貫性を保つのが難しくなります。ここでは RANCID からホスト名でアクセスさせるようにします。
Linux
# cp -p /etc/hosts /etc/hosts.`date "+%Y%m%d-%H%M%S"`
# vi /etc/hosts
# diff -c /etc/hosts.`date "+%Y%m%d-%H%M"`* /etc/hosts
以下の変更を行いました。
*** /etc/hosts.20220916-213253 2018-09-10 07:51:03.000000000 -0400
--- /etc/hosts 2022-09-16 21:33:45.827406503 -0400
***************
*** 1,2 ****
--- 1,3 ----
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
+ 10.0.0.1 csr1000v
[root@centos8 ~]#
今回は /etc/hosts でローカルで名前解決させましたが、現用環境ではもちろん DNS サーバで名前解決させても良いです。
筆者の現用環境は /etc/hosts でやっています。
実際の環境にはネットワーク機器の管理ポートか、Routing が Failover したときにもアクセスできる Loopback アドレスを指定するほうが良いでしょう。
CSR1000V にログイン・IP アドレス・ホスト名を設定
CSR
# enable
(config)#line vty 0 15
(config-line)#password password
(config-line)#login
(config-line)#transport input telnet
(config-line)#exit
(config)#enable secret password
(config)#interface GigabitEthernet1
(config-if)#ip address 10.0.0.1 255.255.255.0
(config-if)#no shutdown
(config)#hostname csr1000v
(config)#end
CentOS にも e0 / ens3 へ 10.0.0.2/24 を設定し、ping が飛ぶことを確認します。
CentOS から telnet 確認
Linux
# telnet csr1000v
Trying 10.0.0.1...
Connected to 10.0.0.1.
Escape character is '^]'.
User Access Verification
Password:
CSR1000V>ena
CSR1000V>enable
Password:
CSR1000V#
CSR1000V#
CSR1000V#
CSR1000V#exit
Connection closed by foreign host.
RANCID 構築 その 2
RANCID ユーザでリポジトリを作成
RANCID 構築時の一番のハマりポイントは、ここです。
- git リポジトリは、git init コマンドなどで作成しないでください RANCID が正常に動作しません RANCID に専用の rancid-cvs コマンドが用意されているため、このコマンドを使用してリポジトリを作成します
- rancid にユーザを変更して、専用の rancid-cvs コマンドを使って作成します 他のユーザで作成すると、権限が異なってしまい、正常に作成されません
Linux
ユーザを rancid に変更し、コンフィグを保存する git リポジトリを作成します。
# su - rancid
$ cd /var/lib/rancid/
$ locate rancid-
$ /usr/bin/rancid-cvs
上記の例では、locate コマンドで rancid のスクリプトを index 検索しています。
何も出てこない場合は、root ユーザで updatedb 後に再度実行してみてください。
git リポジトリ作成時のログは以下です。
Initialized empty Git repository in /var/lib/rancid/GIT/TKY/
Cloning into '.'...
warning: You appear to have cloned an empty repository.
done.
[master (root-commit) 224dc87] new
1 file changed, 3 insertions(+)
create mode 100644 configs/.gitignore
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (4/4), 258 bytes | 14.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To /var/lib/rancid/GIT/TKY
* [new branch] master -> master
[master 9ab589c] new
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 router.db
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 260 bytes | 12.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To /var/lib/rancid/GIT/TKY
224dc87..9ab589c master -> master
$ ls -al
total 4
drwxr-xr-x 5 rancid apache 40 Sep 16 21:29 .
drwxr-xr-x. 32 root root 4096 Sep 16 21:06 ..
drwxr-x--- 3 rancid rancid 17 Sep 16 21:29 GIT
drwxr-x--- 2 rancid rancid 33 Sep 16 21:29 logs
drwxr-x--- 4 rancid rancid 107 Sep 16 21:29 TKY
[rancid@centos8 rancid]$
rancid.conf の LIST_OF_GROUPS、CVSROOT、LOGDIR の内容にそってディレクトリが作成されました。
うまくいかない場合は、/var/lib/rancid の権限や、/etc/rancid/rancid.conf の設定を見直してみると良いでしょう。
RANCID の router.db を変更
router.db を編集して、ホスト名・機器の種類・状態を設定します。
ネットワーク機器のリストを以下の書式で設定します。
<書式> dns 名 か ip アドレス;デバイスタイプ;ステータス
実際に追加してみます。
Linux
$ cp -p /var/lib/rancid/TKY/router.db.`date "+%Y%m%d-%H%M%S"` /var/lib/rancid/TKY/router.db
$ vi /var/lib/rancid/TKY/router.db
$ diff -c /var/lib/rancid/TKY/router.db.`date "+%Y%m%d"`* /var/lib/rancid/TKY/router.db
今回の設定は以下を行いました。
*** TKY/router.db.20220916-213118 2022-09-16 21:29:53.035959126 -0400
--- TKY/router.db 2022-09-16 21:34:30.128061058 -0400
***************
*** 0 ****
--- 1 ----
+ csr1000v;cisco;up
[rancid@centos8 rancid]$
router.db は IP アドレスでも DNS 名を指定しても大丈夫です。ここはリポジトリで表示する際のホスト名になるので、分かりやすい名前がよいでしょう。 今回は、DNS 名で設定しています。
ログインテスト
clogin からログインテスト
RANCID は *loginrc というログインスクリプトを使用してログインを行います。c が先頭に付く場合は Cisco を意味しています。
clogin / .cloginrc / hosts ファイルを用いられて、自動でネットワーク機器にログインできるかテストします。
Linux
$ /usr/libexec/rancid/clogin csr1000v
Error: /var/rancid//.cloginrc must not be world readable/writable
.cloginrc の権限が適切でない場合、上記のように失敗します。
root ユーザから .cloginrc 権限変更
.cloginrc はセキュリティ上重要であるため、他ユーザから読めてはいけません。
また、httpd で git ディレクトリとして公開している、/var/lib/rancid とは必ず別のディレクトリに配置するべきです。
この環境では rancid ユーザのホーム ディレクトリに保存しています。
以下のように権限を変更します。
Linux
# chmod 600 /var/rancid//.cloginrc
# chown rancid:rancid /var/rancid//.cloginrc
[root@centos8 rancid]# ls -al /var/rancid//.cloginrc
-rw------- 1 rancid rancid 4073 Sep 16 21:17 /var/rancid//.cloginrc
筆者は Linux セキュリティが素人同然であるため、上記は不適切な例である可能性があります。
rancid ユーザに変更して再テスト
Linux
# su - rancid
$ /usr/libexec/rancid/clogin csr1000v
csr1000v
spawn telnet -K csr1000v
Trying 10.0.0.1...
Connected to csr1000v.
Escape character is '^]'.
User Access Verification
Password:
csr1000v>enable
Password:
csr1000v#
csr1000v#
telnet> quit
Connection closed.
[rancid@centos8 ~]$
Ctrl + ] して telnet プロンプトを呼び出し -> quit で抜けられます。
コンフィグ取得テスト
手動でコンフィグ取得
実際には cron でコンフィグを取得しますが、まずは手動で取得してみます。
Linux
$ rancid-run
/var/lib/rancid/logs/<GROUPS>.YYMMDD.HHMMSS にログが記録されます。以下は csr1000v を初回取得したときのログです。
# cat /var/lib/rancid/logs/TKY.20220916.213939
starting: Fri Sep 16 21:39:39 EDT 2022
/usr/libexec/rancid/control_rancid: line 431: /usr/sbin/sendmail: No such file or directory
[master a1886b6] new router
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 configs/csr1000v
Added csr1000v
Trying to get all of the configs.
All routers successfully completed.
[master 71199e4] updates
3 files changed, 287 insertions(+)
create mode 100644 .gitignore
To /var/lib/rancid/GIT/TKY
9ab589c..71199e4 master -> master
/usr/libexec/rancid/control_rancid: line 722: /usr/sbin/sendmail: No such file or directory
ending: Fri Sep 16 21:45:06 EDT 2022
[root@centos8 rancid]#
自動でコンフィグ取得
2022/09 時点の CentOS 8 + EPEL リポジトリの rancid.x86_64 3.13-3.el8 では、cron の登録が自動で行われます。
Linux
# cat /etc/cron.d/rancid
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/var/rancid
# Run config differ hourly
1 * * * * rancid /usr/libexec/rancid/rancid-run
3 3 * * * rancid /usr/bin/find /var/log/rancid -type f -mtime +30 -exec /usr/bin/rm {} \;
[root@centos8 rancid]#
1 * * * * は 1 時間を表しており、毎時間コンフィグを取得する設定になっています。
保存したネットワーク機器のコンフィグ表示
長いので折りたたみにしてあります。
機器によっては 1h ごとに毎回差分が出てしまうコマンドがデフォルトで RANCID で登録されているため、取得コマンドをアレンジしたほうが良い機器も存在します。
CentOS で httpd / gitweb の構築
root で httpd の gitweb 設定を変更
conf.d 配下の gitweb.conf は dnf / yum でインストールすると、自動で作成されています。
編集して設定を変更します。
Linux
# cp -p /etc/httpd/conf.d/gitweb.conf /etc/httpd/conf.d/gitweb.conf.`date "+%Y%m%d-%H%M%S"`
# vi /etc/httpd/conf.d/gitweb.conf
# diff -c /etc/httpd/conf.d/gitweb.conf.`date "+%Y%m%d-%H"`* /etc/httpd/conf.d/gitweb.conf
/httpd/conf.d/gitweb.conf
-bash: command substitution: line 1: unexpected EOF while looking for matching `"'
-bash: command substitution: line 2: syntax error: unexpected end of file
*** /etc/httpd/conf.d/gitweb.conf.20220916-215040 2020-07-20 15:30:33.000000000 -0400
--- /etc/httpd/conf.d/gitweb.conf 2022-09-16 22:27:41.334710015 -0400
***************
*** 1,7 ****
! Alias /git /var/www/git
! <Directory /var/www/git>
! Options +ExecCGI
! AddHandler cgi-script .cgi
! DirectoryIndex gitweb.cgi
! </Directory>
--- 1,12 ----
! #Alias /git /var/www/git
! Alias /git /var/lib/rancid
! <VirtualHost *:80>
! <Directory /var/lib/rancid>
! Options +ExecCGI
! AddHandler cgi-script .cgi
! DirectoryIndex gitweb.cgi
! Require all denied
! Require ip 10.0.0.0/8
! </Directory>
! </VirtualHost>
[root@centos8 rancid]#
apache 配下に git ディレクトリのエイリアス、10.0.0.0/8 からのアクセスを許可しています。
ブラウザでアクセスさせたい PC の IP セグメントを Require ip に記載してください。また、複数行書いても動作します。
gitweb の設定を変更
gitweb は RANCID 専用というわけではないため、カスタマイズする必要があります。
まずは設定ファイルの変更を行います。
Linux
# cp -p /etc/gitweb.conf /etc/gitweb.conf.`date "+%Y%m%d-%H%M%S"`
# vi /etc/gitweb.conf
# diff -c /etc/gitweb.conf.`date "+%Y%m%d-%H"`* /etc/gitweb.conf
以下が変更点です。
-bash: command substitution: line 1: unexpected EOF while looking for matching `"'
-bash: command substitution: line 2: syntax error: unexpected end of file
*** /etc/gitweb.conf.20220916-215509 2020-07-20 15:34:28.000000000 -0400
--- /etc/gitweb.conf 2022-09-16 21:57:37.351537362 -0400
***************
*** 8,13 ****
--- 8,14 ----
# Set the path to git projects. This is an absolute filesystem path which will
# be prepended to the project path.
#our $projectroot = "/var/lib/git";
+ our $projectroot = "/var/lib/rancid";
# Set the list of git base URLs used for URL to where fetch project from, i.e.
# the full URL is "$git_base_url/$project". By default this is empty
***************
*** 41,47 ****
#
# Allow projects to override the default setting via git config file.
# Example: gitweb.grep = 0|1;
! #$feature{'grep'}{'override'} = 1;
# Disable the pickaxe search, which will list the commits that modified a given
# string in a file. This can be practical and quite faster alternative to
--- 42,48 ----
#
# Allow projects to override the default setting via git config file.
# Example: gitweb.grep = 0|1;
! $feature{'grep'}{'override'} = 1;
# Disable the pickaxe search, which will list the commits that modified a given
# string in a file. This can be practical and quite faster alternative to
***************
*** 50,53 ****
#
# Allow projects to override the default setting via git config file.
# Example: gitweb.pickaxe = 0|1;
! #$feature{'pickaxe'}{'override'} = 1;
--- 51,54 ----
#
# Allow projects to override the default setting via git config file.
# Example: gitweb.pickaxe = 0|1;
! $feature{'pickaxe'}{'override'} = 1;
[root@centos8 rancid]#
gitweb の projectroot を /var/lib/rancid に、いくつかのコメントアウトを解除して有効化します。
他にも有効な設定がありそうですが、筆者は開拓できていません。
gitweb のファイルを rancid ディレクトリにコピー
cgi などのファイルを apache で公開する rancid のディレクトリにコピーします。
Linux
# cp -p /var/www/git/gitweb.cgi /var/lib/rancid/
# cp -pr /var/www/git/static /var/lib/rancid/
# ls /var/lib/rancid/static/
git-favicon.png git-logo.png gitweb.css gitweb.js
rancid ユーザと apache ユーザのグループ相互追加
RANCID のファイルは、User:rancid,group:rancid で作成されるので、 apache で読み取れず、gitweb で表示できません。 ここでは、RANCID と apache ユーザ間でグループに相互追加します。
Linux
# usermod -aG rancid apache
# usermod -aG apache rancid
# groups rancid
rancid : rancid apache
# groups apache
apache : apache rancid
ここはあまり考えていないため、もっと適切なやり方があると思います。
rancid ディレクトリの権限変更
chown -R で apache のグループ権限を付与して、httpd がアクセスできるようにします。
Linux
# ls -al
total 4
drwxr-xr-x 5 rancid apache 40 Sep 16 21:29 .
drwxr-xr-x. 32 root root 4096 Sep 16 21:06 ..
drwxr-x--- 3 rancid rancid 17 Sep 16 21:29 GIT
drwxr-x--- 2 rancid rancid 60 Sep 16 21:39 logs
drwxr-x--- 4 rancid rancid 174 Sep 16 21:45 TKY
# chown -R rancid:apache /var/lib/rancid/*
# ls -al
total 4
drwxr-xr-x 5 rancid apache 40 Sep 16 21:29 .
drwxr-xr-x. 32 root root 4096 Sep 16 21:06 ..
drwxr-x--- 3 rancid apache 17 Sep 16 21:29 GIT
drwxr-x--- 2 rancid apache 60 Sep 16 21:39 logs
drwxr-x--- 4 rancid apache 174 Sep 16 21:45 TKY
[root@centos8 rancid]#
httpd サーバプロセスを起動 + 自動起動
start で起動、enable で再起動後に自動起動させます。
Linux
# systemctl status httpd
# systemctl start httpd
# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
gitweb 動作確認
PC のブラウザから httpd + gitweb にアクセス
http://<CentOS_8_IP>/git/
うまく行けば、以下のようなページが表示されます。
PC

RSS で更新時にブラウザに通知を表示させたりできます。
検索ボックスはいまいち何が対象になるのかわかりません・・・
apache の root ディレクトリはテストページが表示されるため、現用にする際は無効にしたほうが良いです。
差分表示
上記のトップページから、TKY 行の summary -> commit diff をクリックします。
下記の例では、初回コンフィグ取得 -> 後述の description 変更 -> 再取得を行ったものになっています。
PC

初期構築・取得後は、まだリポジトリにコンフィグがまったくないため、すべてが差分として記録されます。

csr1000v で interface description を追加し、/usr/bin/rancid-run を実行して、差分が増えていないか確認します。
csr1000v
enable
!
configure terminal
!
interface GigabitEthernet1
description centos8_ens3
!
end
Linux
# su - rancid
$ locate rancid-run
/usr/bin/rancid-run
/usr/libexec/rancid/rancid-run
/usr/share/man/man1/rancid-run.1.gz
$ /usr/bin/rancid-run
先ほどクリックした commit diff の、もう一個上の新しい commit diff をクリックしてみましょう。
PC

interface GigabitEthernet1 に description が追加されたことが、gitweb で確認できました。
複数人で検証環境を構築していて、「hh:mm から障害が起きてるんだけどなんか変更した ?」 というときに見に行くと非常に便利です。
なお、NTP で RANCID のサーバを時刻同期していないと使いものにならないため、同期しておきましょう。
差分の横並び比較
上部にある side by side をクリックすることで、Winmerge のような横並び比較をすることが可能です。
PC

行中の差分はわからないため、その場合は各時刻の raw でコンフィグを取得して PC の Winmerge で差分を取ると良いでしょう。
特に Juniper では display set したコンフィグが非常に長い行になるため、side by side の比較は辛い場合が多いです。
その他
昔、筆者が Shrubbery Networks に送ったスクリプトの差分です。
RANCID は password , enable , SNMP community などがマスクされるのですが、ppp と dot11 ssid がマスクされなかったので、随分前に追加してもらいました。
# diff -c /usr/libexec/rancid/rancid.rpmdef /usr/libexec/rancid/rancid_20100224
*** /usr/libexec/rancid/rancid.rpmdef 2011-01-23 23:30:02.000000000 +0900
--- /usr/libexec/rancid/rancid_20100224 2011-02-24 10:23:04.427575427 +0900
***************
*** 1669,1675 ****
ProcessHistory("","","","! neighbor $1 password <removed>\n");
next;
}
! if (/^(ppp .* password) 7 .*/ && $filter_pwds >= 1) {
ProcessHistory("","","","!$1 <removed>\n"); next;
}
if (/^(ip ftp password) / && $filter_pwds >= 1) {
--- 1669,1678 ----
ProcessHistory("","","","! neighbor $1 password <removed>\n");
next;
}
! if (/^( ppp .* password) 7 .*/ && $filter_pwds >= 1) {
! ProcessHistory("","","","!$1 <removed>\n"); next;
! }
! if (/^( wpa-psk .*) 7 .*/ && $filter_pwds >= 1) {
ProcessHistory("","","","!$1 <removed>\n"); next;
}
if (/^(ip ftp password) / && $filter_pwds >= 1) {
公式マニュアル
/usr/share/rancid/README
man rancid-run
man rancid-cvs
man rancid.conf
リファレンス
telnet で接続できる CentOS 7/8 の eve-ng 用 Linux ノードイメージを作った
Fedora Wiki 昔筆者が書いてたサイト DB が吹っ飛んで無くなった リンクは Web Archive のもの