Daniel

Posted on Nov 20, 2023Read on Mirror.xyz

一键在Ubuntu上运行Bitcoin全节点(BTC系列教程1)

现在经常需要btc全节点。很多人得开着电脑24小时不关机,属实很麻烦。

但是如果购买一个小主机,配上Ubuntu系统,24小时运行全节点。开启RPC之后,电脑随时都能连上使用。当然也可以做Atom索引等本文将会讲解如何搭建一个基于ubuntu系统的bitcoin全节点。

需求

  • 操作系统os: ubuntu20.04

  • 内存: 2G

  • SSD: 1TB

对于家用小服务器,我推荐买个N100的mini主机大概几百块钱+一块2tssd,总成本可能在1k左右 功率在10w左右,可以24小时不关机。一个linux设备代表能拥有什么呢: 各种好玩的docker服务,各种脚本

如何安装BTC全节点

使用root用户登陆上ubuntu之后,运行下面命令即可。

wget -O bitcoin_node.sh https://pub-e3b4652c5d5f4c1b8fbfdff04685c330.r2.dev/bitcoin_node.sh && chmod +x bitcoin_node.sh && sudo ./bitcoin_node.sh

当然这个脚本也可以在window开启ubuntu的wsl2子系统使用,也可以在各种云服务使用。只要是X86的Ubuntu20.04都可以。安装脚本开源,安全放心~ 可以直接访问查看

在运行过程中,会提示让你输入
rpc user: 输入你想配置的帐号
rpc password : 输入你想配置的密码是否开启RPC
Do you want to enable external RPC access? (yes/no)
yes: 家里内网都可以访问rpc,(如果机器有外网ip的话,外网也可以访问)
no: 只有本机可以访问rpc (比如做atom索引之类的使用)
等待提示,安装完成Bitcoin Core is now installed and running.
输入这个命令即可以查看到同步高度:bitcoin-cli getblockchaininfo
后续更新如何在这台机器里面一键启动Atom索引,代码也完整的放到下面

#!/bin/bash

# Check if the script is running as root
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root. Please use sudo or log in as the root user."
    exit 1
fi

# Define Bitcoin Core version
BITCOIN_VERSION="25.0"
BITCOIN_DIST="bitcoin-${BITCOIN_VERSION}-x86_64-linux-gnu.tar.gz"


# Prompt user for RPC credentials
read -p "Enter your RPC username: " rpcuser
read -s -p "Enter your RPC password: " rpcpassword
echo

# Ask user if they want to enable external RPC access
read -p "Do you want to enable external RPC access? (yes/no): " enable_rpc

rpcallowip="127.0.0.1" # Default to localhost

if [[ $enable_rpc == "yes" ]]; then
    rpcallowip="0.0.0.0/0" # Allow all IPs (be cautious with this setting)
fi

# Update system
sudo apt update

# Install dependencies
sudo apt install -y build-essential libtool autotools-dev automake pkg-config bsdmainutils python3
sudo apt install -y libevent-dev libboost-system-dev libboost-filesystem-dev libboost-test-dev libboost-thread-dev

# Download Bitcoin Core and signatures
wget https://bitcoin.org/bin/bitcoin-core-${BITCOIN_VERSION}/${BITCOIN_DIST}

# Install Bitcoin Core
tar -xzvf ${BITCOIN_DIST}
sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-${BITCOIN_VERSION}/bin/*

# Create Bitcoin data directory
mkdir -p ~/.bitcoin



# Create bitcoin.conf file
cat >~/.bitcoin/bitcoin.conf <<EOF
server=1
rpcuser=$rpcuser
rpcpassword=$rpcpassword
rpcallowip=$rpcallowip
rpcbind=0.0.0.0
rpcport=8332
listen=1
daemon=1
txindex=1
EOF


# Create the systemd service file for bitcoind
cat >/etc/systemd/system/bitcoind.service <<EOF
[Unit]
Description=Bitcoin daemon
After=network.target

[Service]
ExecStart=/usr/local/bin/bitcoind -daemon -conf=/root/.bitcoin/bitcoin.conf -pid=/root/.bitcoin/bitcoind.pid
User=root
Group=root
Type=forking
PIDFile=/root/.bitcoin/bitcoind.pid
Restart=on-failure
RestartSec=5
KillMode=process

[Install]
WantedBy=multi-user.target
EOF


# Reload the systemd manager configuration
systemctl daemon-reload

# Enable the bitcoind service to start on boot
systemctl enable bitcoind.service

# Start the bitcoind service now
systemctl start bitcoind.service

echo "Bitcoin Core is now installed and running."

Bitcoin