Running a mainnet and testnet on the same bitcoin node

In this post I will tell you how to run a mainnet and testnet bitcoin node simulataneously on the same device.

Bitcoin Conf Sections

It boils down to splitting out the bitcoin.conf into sections:

[main]
mempoolsize=300
maxconnections=20
maxuploadtarget=144
txindex=1
[test]
mempoolsize=100
rpcport=80332
maxconnections=20
maxuploadtarget=144
txindex=1

Then adding an additional systemd startup script for bitcoind -testnet -daemon

You dont need to set the rpcport or other options as they are different anyway:

  • Default Bitcoin network protocol listen port is 18333 (instead of 8333)
  • Default RPC connection port is 18332 (instead of 8332)

Setup Systemd scripts

/etc/systemd/system/bitcoind_test.service:

[Unit]
Description=bitcoin
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu

Environment=BITCOIN_PID=/home/btc/testnet/bitcoin.pid
Environment=BITCOIN_HOME=~/home/btc/testnet/.bitcoin

ExecStart=/usr/local/bin/bitcoind -testnet
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

/etc/systemd/system/bitcoind.service:

[Unit]
Description=bitcoin
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu

Environment=BITCOIN_PID=/home/btc/bitcoin.pid
Environment=BITCOIN_HOME=~/home/btc/.bitcoin

ExecStart=/usr/local/bin/bitcoind
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

Enable the services:

sudo systemctl enable bitcoind_test
sudo systemctl enable bitcoind

Start the services:

sudo systemctl start bitcoind_test
sudo systemctl start bitcoind

Identifying Net in Commands

Now the problem is that when running bitcoin-cli commands it will by default use mainnet, so you must specify testnet.

Example calling mainnet:

bitcoin-cli getblockcount

Example calling testnet:

bitcoin-cli -testnet getblockcount

Sources