Setup a Bitcoin node

This post is a translation of a french guide I made on the Crypto Lyon website, writed because of a request made to me after having made a tutorial about the setup of a Lightning Network node on top of a Bitcoin node.

Everything said in this post is directly taken from the doc available on Github and/or the wiki, it is just intended to regroup informations and make a step-by-step explanation of what has to be done.

I will use Debian 9 but the configuration could be easily adapted to another distribution with a basic knowledge.

Doing things the good way

Let’s start by setting up the environment, we first create a bitcoin user which will run the bitcoind daemon.

useradd -r -m -d /home/bitcoin bitcoin

This will create a system user named bitcoin which home directory is in /home/bitcoin, as if it was a common user. For more about useradd : man useradd. To log in as bitcoin use su — bitcoin as root.

I won’t use another directory than the default one to stock bitcoin-core’s data (the blockchain, the configuration, the debug log, etc..) : /home/bitcoin/.bitcoin/. You can (and it’s common to see it) use, for instance, an external hard drive to stock the blockchain (which is about ~200GB at the time of writing), by using datadir=/path/to/your/mount/point/ or simply by making a symbolic link from the mounting point to the bitcoin-core default sudo apt-get install software-properties-common
 sudo add-apt-repository ppa:bitcoin/bitcoin
 sudo apt-get update
 sudo apt-get install libdb4.8-dev libdb4.8++-dev
data directory.

We then install the dependencies required by bitcoind :

apt install git build-essential libtool autotools-dev automake  pkg-config libssl-dev libevent-dev bsdmainutils libboost-system-dev  libboost-filesystem-dev libboost-chrono-dev libboost-program-options-dev  libboost-test-dev libboost-thread-dev libminiupnpc-dev libzmq3-dev jq

And, if you want to compile the qt wallet too, the bitcoin-qt dependencies :

libqt5gui5 libqt5core5a libqt5dbus5 qttools5-dev qttools5-dev-tools libprotobuf-dev protobuf-compiler

In order to install the last dependency, Berkeley-DB 4.8, I’ve made a script for the Insacoin which can be used for Bitcoin :

wget && chmod +x &&  ./ amd64 && rm

It will fetch the deb packages and install it. If your OS is a 32 bits one, just replace amd64 by i386. If you are on Ubuntu, there is a PPA :

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:bitcoin/bitcoin
sudo apt-get update
sudo apt-get install libdb4.8-dev libdb4.8++-dev

Diving in it

Let’s fetch the last version of bitcoin-core (v0.17 at the time of writing) and compile it :

su - bitcoin
bash # In order to a little more comfortable
cd ~
git clone -b 0.17 && cd bitcoin
./configure --enable-cxx --without-gui
make -j 4 # Change 4 to fit your number of cores
su -c 'make install'

Obviously, if you want to compile the gui too, don’t set --without-gui.

It might take a few moments, depending on your number or core, quality of your CPU, and whether or not you compile bitcoin-qt.

Once compilation is finished, you can check everything is fine by running bitcoind . Everything is fine means it will starting to synchronize the blockchain, displaying messages like :

UpdateTip: new best=0000000000000793368a62b6995769ff74c146d0a1d214eaaf6261502be688a7  height=142344 version=0x00000001 log2_work=66.309681 tx=1359484  date='2011-08-24T05:07:03Z' progress=0.003804 cache=130.2MiB(967004txo)

(This message means, we got a new best block hash. A “best block hash” is the hash of the last block we’ve heard of, which is at the “““top””” of the chain.)

Making it easier to use/administrate

We can specifiy options to pas to bitcoind(which can be found here) directly from the command line, but a configuration file is easier to manage. Default directory for this file (which i’ll use) is /home/bitcoin/.bitcoin/bitcoin.conf. You can specify a conf from another directory by running bitcoind -conf=/path/to/the/conf , otherwise it will use the default path. I will specify in the configuration rpc credentials in order to use the cli, and set server=1 (to accept RPC calls) and daemon=1. If you want to store the blockchain in another directory than /home/bitcoin/.bitcoin/ , you can specify datadir=/path/to/your/dir. There are also options you can set in order to reduce your RAM/CPU use, again check out the wiki for more information about this.

vim ~/.bitcoin/bitcoin.conf

Now let’s create a sytemd service called bitcoind.service. A sample of it can be found in bitcoin/contrib/init/ alongside init script for other service managers. We will copy and adapt it :

exit # To use systemd we have to be root
cp /home/bitcoin/bitcoin/contrib/init/bitcoind.service /etc/systemd/system/bitcoind.service
vim /etc/systemd/system/bitcoind.service

Here is what my init script looks like after modification :

Description=Bitcoin daemon
ExecStart=/usr/local/bin/bitcoind -daemon -conf=/home/bitcoin/.bitcoin/bitcoin.conf -pid=/home/bitcoin/.bitcoin/
# Creates /run/bitcoind owned by bitcoin

# Hardening measures

# Provide a private /tmp and /var/tmp.

# Mount /usr, /boot/ and /etc read-only for the process.

# Disallow the process and all of its children to gain
# new privileges through execve().

# Use a new /dev namespace only populated with API pseudo devices
# such as /dev/null, /dev/zero and /dev/random.

# Deny the creation of writable and executable memory mappings.

As you can see, I just changed a few things compared to the sample file.

Now you just have to run systemctl daemon-reload and systemctl start bitcoind.

Running Bitcoin behind Tor

You can, if you want, easily making your Bitcoin node behind Tor. Making your node accessible from the Tor network is not a way, as could suggest some people (or association of people), to do something illicit but helps a lot of people (for instance, someone trying to access the internet of value from a country in which Bitcoin is censored). As root :

apt install dirmngr
vim /etc/apt/sources.list
# Add the tor repos
# deb stretch main
# deb-src stretch main
gpg --recv A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89
gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | apt-key add -
apt update
apt install tor

Then add the bitcoin user to the Tor group :

usermod -aG debian-tor bitcoin

And finally modify /etc/torrc :

ControlPort 9051
CookieAuthentication 1
CookieAuthFileGroupReadable 1

That’s it ! systemctl restart tor@default && systemctl restart bitcoind Congratulations for what you have done for the accessibility of Internet and Bitcoin.

Additionnaly, if you want to pass all Bitcoin traffic through tor, add this to your bitcoin.conf :


If you want to know more about Bitcoin and Tor, checkout this and this.

Update bitcoin-core to the last version

Here are the steps to update your Bitcoin node in case there is a new released version :

# We stop the daemon
systemctl stop bitcoind
# Compiling the new version
su - bitcoin
cd /home/bitcoin
rm -r bitcoin/
git clone -b x.x && cd bitcoin
./configure --enable-cxx --without-gui
make -j 4
su -c 'make install'
# Restarting the daemon
systemctl restart bitcoind

Use the CLI

This post is not intended to teach the use of the command-line interface bitcoin-cli , if you want to now more about it, you can check out the post I made about insacoin (a fork of Bitcoin) and the use of its CLI.