Connect C-Lightning to a Remote Bitcoin Node

This post describes how to make your local lightning node connect to a remote node. First set up an ssh tunnel between you and your remote and make it listen to bitcoin’s default rpc port:

ssh -i /path/to/your/ssh_key.pem -f <USER>@<SERVER_IP> -L 8332: -N

If you authenticate to ssh with a password, you can leave out the -i option. There are two different ways at the moment to authenticate to bitcoind. One is the standard rpcuser/rpcpassword method. This however is about to be deprecated. It is recommend to use the new ‘cookie’ authentication. For this first generate a token, bitcoin provides a tool for this in the bitcoin core repository. It is located in share/rpcauth. To get a new token for the user ‘rpcuser’ run ./ rpcuser. The output will be similar to:

Your password:                                                                                                                                                                                                                                                                                       

Now open your bitcoin.conf file in your bitcoin data directory and add the following lines. You can add as many different rpcauth lines with different users as you want to. I added the # commented line for the password to make sure I can lookup the password again at a later stage, however it can be left out. Ideally it should be stored only on the client side:

# password = tvIZqauaRAJ1oU1gDS0JSRgPmR1FEzJ6bfM2CKxfy_4=

Leave out the testnet=1 to run it on mainnet. Save and close the file, then start bitcoind on your remote host. You can track its syncing progress by running tail -f debug.log in your remote host bitcoin data dir.

Now install bitcoin-cli on your local machine. To test if the ssh tunnel is running correctly execute the following line on your local machine:

bitcoin-cli -rpcuser=rpcuser -rpcpassword=tvIZqauaRAJ1oU1gDS0JSRgPmR1FEzJ6bfM2CKxfy_4= -rpcport=8332 getblockchaininfo

Once the remote machine is synced start your local lightning daemon with:

lightningd/lightningd --bitcoin-rpcuser=rpcuser --bitcoin-rpcpassword=tvIZqauaRAJ1oU1gDS0JSRgPmR1FEzJ6bfM2CKxfy_4=
--bitcoin-rpcport=8332 --network=testnet --log-level=debug

Replace --network=testnet with --network=bitcoin to run mainnet. When restarting your local machine, you need to re-open the ssh tunnel again.

Written on July 11, 2018