Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active April 23, 2024 18:56
Show Gist options
  • Save npodonnell/d296e908fe43214de126bce5f12031cc to your computer and use it in GitHub Desktop.
Save npodonnell/d296e908fe43214de126bce5f12031cc to your computer and use it in GitHub Desktop.
Bitcoind Ubuntu Setup

Bitcoind Ubuntu Setup

N. P. O'Donnell, 2021

Getting Bitcoind

ARCH=x86_64
BITCOIN_VERSION=0.21.0
wget https://bitcoin.org/bin/bitcoin-core-$BITCOIN_VERSION/bitcoin-$BITCOIN_VERSION-$ARCH-linux-gnu.tar.gz

Check File Integrity

wget https://bitcoin.org/bin/bitcoin-core-$BITCOIN_VERSION/SHA256SUMS.asc
grep bitcoin-$BITCOIN_VERSION-$ARCH-linux-gnu.tar.gz SHA256SUMS.asc | sha256sum --check

Extract Files and Copy

tar -xf bitcoin-$BITCOIN_VERSION-$ARCH-linux-gnu.tar.gz
sudo cp -R bitcoin-$BITCOIN_VERSION/* /usr/

Create bitcoin User

sudo adduser bitcoin

Create Config File

Config File:

rpcallowip=127.0.0.1
rpcuser=someuser
rpcpassword=somepass

Make bitcoin data dir:

sudo mkdir -p /etc/bitcoin

Save the config file to /etc/bitcoin/bitcoind.conf

Create Unit File

Edit unit file by typing:

sudo systemctl edit --force --full bitcoind.service

Copy-paste the following unit file:

# It is not recommended to modify this file in-place, because it will
# be overwritten during package upgrades. If you want to add further
# options or overwrite existing ones then use
# $ systemctl edit bitcoind.service
# See "man systemd.service" for details.

# Note that almost all daemon options could be specified in
# /etc/bitcoin/bitcoin.conf, but keep in mind those explicitly
# specified as arguments in ExecStart= will override those in the
# config file.

[Unit]
Description=Bitcoin daemon
Documentation=https://github.com/bitcoin/bitcoin/blob/master/doc/init.md

# https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/bitcoind -daemon \
                            -pid=/run/bitcoind/bitcoind.pid \
                            -conf=/etc/bitcoin/bitcoin.conf \
                            -datadir=/var/lib/bitcoind

# Make sure the config directory is readable by the service user
PermissionsStartOnly=true
ExecStartPre=/bin/chgrp bitcoin /etc/bitcoin

# Process management
####################

Type=forking
PIDFile=/run/bitcoind/bitcoind.pid
Restart=on-failure
TimeoutStopSec=600

# Directory creation and permissions
####################################

# Run as bitcoin:bitcoin
User=bitcoin
Group=bitcoin

# /run/bitcoind
RuntimeDirectory=bitcoind
RuntimeDirectoryMode=0710

# /etc/bitcoin
ConfigurationDirectory=bitcoin
ConfigurationDirectoryMode=0710

# /var/lib/bitcoind
StateDirectory=bitcoind
StateDirectoryMode=0710

# Hardening measures
####################

# Provide a private /tmp and /var/tmp.
PrivateTmp=true

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

# Deny access to /home, /root and /run/user
ProtectHome=true

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

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

# Deny the creation of writable and executable memory mappings.
MemoryDenyWriteExecute=true

[Install]
WantedBy=multi-user.target

Save the unit file.

Starting the Service

sudo systemctl enable bitcoind
sudo systemctl start bitcoind

You may want to reboot the machine at this point.

Check service is running:

sudo systemctl status bitcoind

Ensure you see active (running)

Tailing the Logs

You can check on the status of blockchain sync (and any other issues) with the command:

sudo -u bitcoin tail -f /var/lib/bitcoind/debug.log

Configuring bitcoin-cli

echo -e "rpcuser=someuser\nrpcpassword=somepass" > /home/$USER/.bitcoin/bitcoin.conf

Test bitcoin-cli:

bitcoin-cli help

If you see the help commands then you've successfully connected to the local daemon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment