Your Cart

Guide for Config_DB in SONiC

Blog By

Config DB holds SONiC’s running configuration in Redis database 4, while /etc/sonic/config_db.json holds the startup configuration. SONiC loads the startup file into Config DB at boot. CLI changes to Config DB do not update that file, and editing the file does not change the running database until the file is loaded or reloaded.

Use config save to write the running database to a file. This guide follows Humza Altaf’s GNS3 lab through an address change, VLAN creation, an FRR configuration change and an import on a second switch.

How Save, Load and Reload Differ

OperationRunning Config DB and servicesFiles
config save [file]Reads Config DB. Services keep running.Overwrites /etc/sonic/config_db.json, or the named file, with the whole running database.
config load [file]Merges the file into Config DB without clearing it. Entries and fields that the file does not mention remain. It restarts no services, merges no init_cfg.json and runs no db_migrator.py.Reads the file only, or /etc/sonic/config_db.json when no file is named.
config reload [file]Stops sonic.target, clears Config DB, loads init_cfg.json and then the file, runs db_migrator.py and restarts sonic.target.Reads the file only, or /etc/sonic/config_db.json when no file is named.

Every reload merges /etc/sonic/init_cfg.json first when it exists, and values in the loaded file win. If the file has no platform or mac in DEVICE_METADATA, the reload first loads both from the local hardware. The restart covers the SONiC containers and briefly takes the management interface down, while the operating system keeps running.

The -y option answers the confirmation prompt. The -f option skips two readiness checks. One requires systemd to have finished starting, and the other requires SWSS to have been active for more than 120 seconds. Waiting two minutes after the previous reload finishes makes -f unnecessary.

About This Lab

The lab uses community SONiC 202305. The original PDF is revision 1.0, dated 15 April 2024. The outputs below come from that lab and were not re-run for this page.

Commands and JSON marked as added are not part of the original lab, and the lab recorded no output for them. Every other command and output comes from the original lab, including the unsuccessful FRR input.

Reloading configuration interrupts SONiC services and can interrupt management access. Use the GNS3 console for this lab and keep it open through each reload.

Topology and Port Mapping

S1 and S2 connect through Ethernet4. PC1 connects to S1 Ethernet0, and PC2 connects to S2 Ethernet0.

S1S2PC1PC2Ethernet4Ethernet4Ethernet0Ethernet0
Lab topology. The lab edits S1 and then imports a configuration file on S2.
ConnectionGNS3 PortsSONiC Interfaces
Inter-switch linkS1 e1 to S2 e1Ethernet4 to Ethernet4
PC1 connectionPC1 e0 to S1 e0S1 Ethernet0
PC2 connectionPC2 e0 to S2 e0S2 Ethernet0
GNS3 InterfaceSONiC Interface
Ethernet0Ethernet0
Ethernet1Ethernet4
Ethernet2Ethernet8
Ethernet3Ethernet12

The lab gives the hosts no addresses and runs no traffic tests, so it shows configuration changes rather than connectivity.

Step 1. Inspect and Back Up S1

Check S1’s starting addresses before editing its startup file. The table keeps the first four rows of the original output, with the interface, address and state columns.

show ip interfaces
InterfaceIPv4 address/maskAdmin/Oper
Ethernet010.0.0.0/31up/up
Ethernet410.0.0.2/31up/up
Ethernet810.0.0.4/31up/up
Ethernet1210.0.0.6/31up/up

Open the configuration directory and inspect the startup file with the original commands. The directory listing is omitted, and the metadata table retains selected fields from the JSON screenshot.

cd /etc/sonic/
ls
cat config_db.json
LocationRecorded value
DEVICE_METADATA.localhost.hwskuForce10-S6000
DEVICE_METADATA.localhost.platformx86_64-kvm_x86_64-r0
DEVICE_METADATA.localhost.hostnamesonic
DEVICE_METADATA.localhost.typeLeafRouter
DEVICE_METADATA.localhost.bgp_asn65100

Back up the startup file and export the running database separately before any edit. These commands are additions, and each backup needs an unused filename.

sudo cp -p /etc/sonic/config_db.json /etc/sonic/config_db.json.before-lab
sudo config save -y /etc/sonic/config_db.running-before-lab.json

Step 2. Change Ethernet0’s Address

If S1 has CLI changes you want to keep, run sudo config save -y before you edit the file. Then edit, validate and reload the file. A config save between the edit and the reload overwrites the edit with the running database, and a config reload before saving discards the unsaved CLI changes.

Edit Ethernet0’s address in the existing INTERFACE table. Replace the old prefix entry instead of adding another address alongside it. The t1 sample also defines BGP neighbor 10.0.0.1 with local address 10.0.0.0 on this port. Remove or update that BGP_NEIGHBOR entry in the same edit, because 10.0.0.1 is no longer on a connected subnet once the address changes.

sudo vi /etc/sonic/config_db.json

The original edit changes the existing key to the line below.

"Ethernet0|192.168.10.2/24": {},

The added example below shows the plain Ethernet0 entry and its address entry together. Keep any existing interface attributes and all unrelated configuration.

{
    "INTERFACE": {
        "Ethernet0": {},
        "Ethernet0|192.168.10.2/24": {}
    }
}

The 202305 virtual image builds its first startup file from the t1 sample, which writes only prefix keys such as Ethernet0|10.0.0.0/31. That file has no VERSIONS table, so db_migrator.py adds the plain Ethernet0 entry during each reload, and the new address works. A file saved with config save already holds both entries and a current VERSIONS entry. Keep the plain interface entry in any file you write by hand, because SONiC does not configure an address until that entry exists.

Validate the edited file with the added command below. It catches JSON syntax errors only. A duplicated table or key also passes, and the last copy silently replaces the earlier one, so extend existing tables instead of pasting a second copy.

python3 -m json.tool /etc/sonic/config_db.json > /dev/null

Apply the file with the original reload command. Write the file and quit vi first, so the reload reads the edited contents.

sudo config reload -y

Display the addresses again with the original command. The output confirms Ethernet0’s new address, and the other rows are unchanged.

show ip interfaces
InterfaceIPv4 address/maskAdmin/Oper
Ethernet0192.168.10.2/24up/up
Ethernet410.0.0.2/31up/up
Ethernet810.0.0.4/31up/up

Step 3. Create VLANs Through the CLI and JSON

Check that S1 has no VLANs yet. The original output has no VLAN rows.

show vlan brief

Create VLAN 10 through the CLI and save the running database to the startup file. The save output below is from the original lab.

sudo config vlan add 10
admin@sonic:~$ sudo config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

VLAN 10 already exists in the running Config DB before the save. Saving makes its entry appear in the JSON file that will be edited next.

The selected JSON excerpt below preserves the VLAN 10 entry shown in the PDF. Other tables in the file are omitted.

"VLAN": {
    "Vlan10": {
        "vlanid": "10"
    }
}

Reopen the same file and add VLANs 20 and 30 to its existing VLAN table. The original lab does not show this edit, so the block below rebuilds it in the format that config vlan add wrote for VLAN 10.

{
    "VLAN": {
        "Vlan10": {
            "vlanid": "10"
        },
        "Vlan20": {
            "vlanid": "20"
        },
        "Vlan30": {
            "vlanid": "30"
        }
    }
}

These entries define the VLANs without assigning member ports. No VLAN_MEMBER entries are added in this lab.

Repeat the added syntax check, then use the original reload and display commands. The historical excerpt below retains selected columns from the VLAN table.

python3 -m json.tool /etc/sonic/config_db.json > /dev/null
sudo config reload -y
show vlan brief
VLAN IDIP AddressPortsPort TaggingProxy ARP
10disabled
20disabled
30disabled

All three VLANs exist, with no addresses or member ports shown.

Step 4. Switch FRR Configuration to frrcfgd

Before the change, FRR refuses a second BGP instance and has no OSPF daemon. The original session below shows both errors. After ospfd is not running, vtysh still changes the prompt to config-router, but nothing is configured.

admin@sonic:~$ vtysh

Hello, this is FRRouting (version 8.5.1).
Copyright 1996-2005 Kunihiro Ishiguro, et al.

sonic# configure
sonic(config)# router bgp 65000
BGP is already running; AS is 65100
sonic(config)# exit
sonic# configure
sonic(config)# router ospf
ospfd is not running
sonic(config-router)#

Edit /etc/sonic/config_db.json again and add the following field under DEVICE_METADATA and localhost, keeping the other metadata. The block is an excerpt of the original edit.

"DEVICE_METADATA": {
    "localhost": {
        "frr_mgmt_framework_config": "true"
    }
}

The original lab then reloads with the force option and opens FRR again. Its subsequent input is preserved as a historical excerpt, with the startup banner and reload log omitted.

sudo config reload -y -f
vtysh
sonic# configure
sonic(config)# router ospf
sonic(config-router)# network 192.168.10.0/24 area 0
sonic(config-router)# exit
sonic(config)# router bgp 65000
sonic(config-router)# neighbor 192.168.10.2 remote-as 65000

Setting frr_mgmt_framework_config to the string true selects frrcfgd in place of bgpcfgd. It also includes ospfd, bfdd and pimd among the supervised routing processes.

With the flag set, bgpcfgd no longer builds router bgp 65100 from DEVICE_METADATA. The frrcfgd template builds a BGP instance only from the BGP_GLOBALS table, which this file does not have, so FRR starts with no BGP instance and accepts router bgp 65000. The t1 sample’s BGP neighbors also drop out, because frrcfgd reads only BGP_NEIGHBOR keys that start with a VRF name, such as default|10.0.0.1.

This input still does not build a working design. If Step 2’s address is still on S1 Ethernet0, 192.168.10.2 is S1’s own address, and FRR 8.5.1 rejects the neighbor with % Can not configure the local system as neighbor. The original session ends before FRR replies. The OSPF network 192.168.10.0/24 is on Ethernet0, which faces PC1 rather than S2.

A working design runs both protocols over Ethernet4. Both switches start with 10.0.0.2/31 on that port, so change one end first, for example S2 to 10.0.0.3/31. Give each switch its own router ID as well, because FRR rejects an iBGP session from a peer with the same router ID, and ospfd discards hellos that carry its own router ID.

When docker_routing_config_mode is absent or separated, container startup regenerates the per-daemon FRR files from Config DB. With the framework flag enabled, that generation includes the OSPF file.

The unified mode regenerates a combined frr.conf. The split and split-unified modes do not rebuild anything from Config DB. FRR starts from the files already in /etc/sonic/frr, the per-daemon files in split mode or a single frr.conf in split-unified mode. Running write memory in vtysh updates them. This lab’s DEVICE_METADATA has no docker_routing_config_mode, and neither init_cfg.json nor the t1 sample adds one, so the bgp container uses the separated behavior.

frrcfgd applies routing settings from Config DB tables to FRR. Each config reload restarts the bgp container, which then rebuilds the FRR files from Config DB, so the input typed in Step 4 is lost at the next reload. To keep routing settings, put them in Config DB tables such as BGP_GLOBALS, BGP_NEIGHBOR, BGP_NEIGHBOR_AF, OSPFV2_ROUTER and OSPFV2_INTERFACE, then run config save.

ASN 65100 comes from DEVICE_METADATA.localhost.bgp_asn in the t1 sample that the 202305 virtual image writes at first boot. It is not a default for every SONiC switch.

Additional checks below inspect the routing processes and FRR state. Run the first command in the Linux shell and the remaining display commands inside vtysh.

sudo docker exec bgp supervisorctl status
show running-config
show bgp summary
show ip ospf neighbor

With the flag set, supervisorctl status lists frrcfgd, ospfd, bfdd and pimd, and no bgpcfgd. The original lab does not run these checks, so no output is shown.

Step 5. Prepare and Import S2’s File

The original lab copies S1’s complete config_db.json onto S2 and reloads it. Check S2’s starting VLANs and addresses with the original commands first.

show vlan brief
show ip interfaces

S2 starts with no VLANs. The address output shows the two starting addresses below.

InterfaceIPv4 address/maskAdmin/Oper
Ethernet010.0.0.0/31up/up
Ethernet410.0.0.2/31up/up

Create and inspect the empty file with the original commands below. The directory listing is omitted.

sudo touch /etc/sonic/humza_config_db.json
cd /etc/sonic/
ls
cat humza_config_db.json

Pasting S1’s whole file gives S2 the MAC address, hostname, interface addresses and BGP neighbors of S1. A safer file starts from S2’s own /etc/sonic/config_db.json and adds only the shared settings, such as the VLAN table. When only the VLANs are needed, a file that holds just the VLAN table can be merged with sudo config load -y <file>, which keeps the rest of S2’s configuration and restarts no services.

Give S2 the addresses, router IDs and peer settings required by the network design. Do not copy S1’s local address into the opposite end of a routed link.

Review the following fields before reusing another switch’s file, and check every other table in it as well.

Configuration locationWhat to retain or adapt for S2
DEVICE_METADATA.localhost.hostnameS2’s intended hostname
DEVICE_METADATA.localhost.macS2’s own system MAC
DEVICE_METADATA.localhost.platform and hwskuValues that match S2’s hardware
DEVICE_METADATA.localhost.bgp_asnThe intended local ASN
DEVICE_METADATA.localhost.frr_mgmt_framework_config and docker_routing_config_modeThe intended routing configuration method
MGMT_INTERFACE prefix keys and gwaddrS2’s management address and gateway
MGMT_PORT and MGMT_VRF_CONFIGS2’s management configuration
Address keys in INTERFACE, LOOPBACK_INTERFACE, VLAN_INTERFACE, PORTCHANNEL_INTERFACE and VLAN_SUB_INTERFACE, where presentS2’s intended local addresses, including a unique Loopback0 address, which bgpcfgd uses as the BGP router ID
BGP_GLOBALS fields router_id and local_asnThe intended router ID and ASN for each VRF
BGP_NEIGHBOR keys and fields such as local_addr and asnS2’s peers, local source addresses and remote ASNs. bgpcfgd accepts a plain peer address or a key that starts with the VRF, while frrcfgd reads only keys that start with the VRF, the form shown in Step 4
OSPFV2_ROUTER field router-id and related OSPF interface and network entriesS2’s intended OSPF identity and interfaces
STATIC_ROUTE next hops and interfacesNext hops reachable from S2
PORT, BREAKOUT_CFG, PORTCHANNEL_MEMBER and VLAN_MEMBER, where presentS2’s hardware mapping and intended connections

SONiC keeps the MAC and platform written in the file, even when the file came from another switch. If the copied file has no platform or mac field, config reload first loads both from S2’s own hardware, so deleting those two fields from a copied file keeps S2’s values. Confirm that the HWSKU and port configuration match S2’s hardware.

Use the added editor and syntax check below to prepare the complete S2 file. config reload clears Config DB before it loads the file, so the JSON fragments in earlier steps are excerpts and must not replace the complete configuration.

sudo vi /etc/sonic/humza_config_db.json
python3 -m json.tool /etc/sonic/humza_config_db.json > /dev/null

Import the prepared file using the original command. The historical session answers y to its confirmation prompt.

sudo config reload /etc/sonic/humza_config_db.json

Check the VLANs and addresses with the original display commands. These historical excerpts retain the three VLANs and the visible Ethernet0 row.

show vlan brief
show ip interfaces
VLAN IDIP AddressPortsPort TaggingProxy ARP
10disabled
20disabled
30disabled
InterfaceIPv4 address/maskAdmin/Oper
Ethernet0192.168.10.2/24up/up

S2 now shows S1’s Ethernet0 address. The original reload log has no sonic-cfggen -H -k line, the hardware step that a reload without -l runs only when the file lacks a platform or MAC, so S2 also took the MAC address from S1’s file.

config reload with a file name changes only the running Config DB, so /etc/sonic/config_db.json still holds S2’s old configuration. Until you save, a reboot or sudo config reload -y returns S2 to that configuration, which is a simple rollback. After checking the result, save it as the startup configuration with the added command below.

sudo config save -y

Confirm the Saved Configuration

Compare the whole running database with the startup file using the added command below. It prints startup file matches Config DB when the two hold the same configuration, and the differing lines otherwise.

diff <(sudo sonic-cfggen -d --print-data | python3 -m json.tool --sort-keys) <(python3 -m json.tool --sort-keys /etc/sonic/config_db.json) && echo "startup file matches Config DB"

A match confirms the saved settings. It does not prove that manual FRR changes were saved or that traffic can pass between the hosts.

If the diff shows only FLEX_COUNTER_TABLE lines, the save ran too soon. SWSS writes its default counter settings to Config DB about one minute after it starts, or three minutes after boot, so run sudo config save -y again and repeat the comparison.

Reboot the virtual switches and repeat the checks when the intended configuration is complete. The original lab does not reboot the switches.

Check the intended hardware and image on the supported platforms page. Continue with the VLAN guide or PortChannel guide when adding host ports to the VLANs or bundling more S1 to S2 links into a PortChannel.

The many benefits of SONiC, once exclusive to hyperscalers, are now within reach for enterprise clients. Contact our team of seasoned networking experts to learn why right now is a transformative time in the industry as we move closer to wider SONiC adoption.

Humza Altaf, Network Engineer

Humza is a network engineer at Hardware Nation Labs, where his enthusiasm for Open Networking drives his work. With a blend of deep expertise and innovative approaches, he designs robust, scalable networks of the future. His practical experience includes configuring and deploying a range of protocols such as LACP, VLANs, MPLS, and VRRP. At his previous role, he was part of a SONiC testing team, further honing his skills in network setup and troubleshooting. Humza is also adept at network analysis with tools like Wireshark, enhancing his ability to manage complex network environments.

View more work by Humza

Quickly identify your needs and craft a plan of action with help from our seasoned experts.