1. 設定 bitcoin core

這個章節我們要設定一個比特幣節點,我們用雲端伺服器架設,要連上得是比特幣測試網路。

雲端伺服器

我們要架設一個 VPS (Virtual private server),

首先要挑選一個雲端伺服器提供者,這邊我用的是 linode.com,大家可以用我推薦碼,第一個月免費 20 美金喔!XDD

https://www.linode.com/?r=0452dc68ff056f9c30fd7dd1a3056b9fa9aea094

建立機器

要跑比特幣,至少要選 2-3G 記憶體,以及 15G 儲存,然後選 Debian 8 image,挑好了就把 VPS 開機!

設定 VPS 參數

首先線連到機器

$ ssh root@linode's IP

現在我們要強化一下機器的安全

設定主機名字

選定一個名字,然後寫到主機名字的檔案,接著傳遞這個設定

$ echo "mybtc" > /etc/hostname
$ /etc/init.d/hostname.sh start
$ /bin/hostname "mybtc"

把 IP 和主機名字輸入到 /etc/hosts 檔案裡,

$ echo "127.0.0.1    localhost" > /etc/hosts
$ echo "127.0.1.1 mybtc.local mybtc" >> /etc/hosts

設定時區

(略)

保護 VPS

雖然只是測試網路,但我們還是要讓機器安全

建立防火牆規則

首先我們建立防火牆規則

$ cat > /etc/iptables.firewall.rules <<EOF
*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# If you want HTTP and HTTPS, uncomment these

#  Allow SSH connections
#
#  The -dport number should be the same port number you set in sshd_config
#
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -j ACCEPT

# Allow Bitcoin connections
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp --dport 8333 -j ACCEPT
-A INPUT -p tcp --dport 18333 -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT
EOF

port 8333 是比特幣主網的 port,測試網路是 18333

加入 IPv6 的判別

$ cat /etc/iptables.firewall.rules | sed 's/127.0.0.0\/8/::1\/128/' > /etc/ip6tables.firewall.rules

最後加入在開機時就跑這兩個 script 的條件

$ cat > /etc/network/if-pre-up.d/firewall <<EOF
#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules
/sbin/ip6tables-restore < /etc/ip6tables.firewall.rules
EOF

$ chmod a+x /etc/network/if-pre-up.d/firewall

因為我們沒有重開機,所以要跑

/etc/network/if-pre-up.d/firewall

鎖定 SSH

如果是用固定 IP,最好是鎖定 SSH

$ echo "sshd: $YOUR_HOME_IP" >> /etc/hosts.allow
$ echo "sshd: ALL" >> /etc/hosts.deny

更新 Debian

最新的 Debian 就是最安全的 Debian,在下載比特幣之前我們要確保 Debian 是最新的

$ export DEBIAN_FRONTEND=noninteractive
$ apt-get update
$ apt-get upgrade -y
$ apt-get dist-upgrade -y

這些更新要花一點時間

之後要安裝一個亂數產生器

$ apt-get install haveged -y

最後,建議把更新設定為自動

$ echo "unattended-upgrades unattended-upgrades/enable_auto_updates boolean true" | debconf-set-selections
$ apt-get -y install unattended-upgrades

設定使用者

$ /usr/sbin/useradd -m -g sudo -s /bin/bash user1
$ /usr/bin/passwd user1
$ /usr/sbin/adduser user1 sudo

安裝比特幣

先建立別名

$ sudo -u user1 cat >> ~user1/.bash_profile <<EOF
alias btcdir="cd ~/.bitcoin/" #linux default bitcoind path
alias bc="bitcoin-cli"
alias bd="bitcoind"
alias btcinfo='bitcoin-cli getwalletinfo | egrep "\"balance\""; bitcoin-cli getinfo | egrep "\"version\"|connections"; bitcoin-cli getmininginfo | egrep "\"blocks\"|errors"'
alias btcblock="echo \\\`bitcoin-cli getblockcount 2>&1\\\`/\\\`wget -O - http://blockexplorer.com/testnet/q/getblockcount 2> /dev/null | cut -d : -f2 | rev | cut -c 2- | rev\\\`"
EOF

給定上面的檔案權限

$ /bin/chown user1 ~user1/.bash_profile

用剛剛加入的使用者登入

$ su user1
$ cd
$ source ~/.bash_profile

設定變數

設定兩個變數讓安裝更自動化

$ export BITCOIN=bitcoin-core-0.15.1
$ export BITCOINPLAIN=`echo $BITCOIN | sed 's/bitcoin-core/bitcoin/'`

第一個是比特幣版本,第二個是簡化字串,某些檔案要用

下載檔案

$ wget https://bitcoin.org/bin/$BITCOIN/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz -O ~user1/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz
$ wget https://bitcoin.org/bin/$BITCOIN/SHA256SUMS.asc -O ~user1/SHA256SUMS.asc
$ wget https://bitcoin.org/laanwj-releases.asc -O ~user1/laanwj-releases.asc

驗證簽章

驗證一下下載到的是不是正確的檔案

$ /usr/bin/gpg --import ~user1/laanwj-releases.asc
$ /usr/bin/gpg --lsign `sudo -u user1 /usr/bin/gpg --list-keys | grep pub | awk '{print $2}' | awk -F/ '{print $2}'`
$ /usr/bin/gpg --verify ~user1/SHA256SUMS.asc

跑完看到 「Good signature」就表示是正確的

驗證比特幣 SHA

$ /usr/bin/sha256sum ~user1/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz | awk '{print $1}'
$ cat ~user1/SHA256SUMS.asc | grep $BITCOINPLAIN-x86_64-linux-gnu.tar.gz | awk '{print $1}'

兩個印出來的一樣,表示正確

安裝

現在可以安裝拉!!!

先解壓縮

$ /bin/tar xzf ~user1/$BITCOINPLAIN-x86_64-linux-gnu.tar.gz -C ~user1
$ sudo /usr/bin/install -m 0755 -o root -g root -t /usr/local/bin ~user1/$BITCOINPLAIN/bin/*
$ /bin/rm -rf ~user1/$BITCOINPLAIN/

建立組態檔

$ /bin/mkdir ~user1/.bitcoin
$ cat >> ~user1/.bitcoin/bitcoin.conf << EOF
server=1
dbcache=1536
par=1
blocksonly=1
maxuploadtarget=137
maxconnections=16
rpcuser=bitcoinrpc
rpcpassword=$(xxd -l 16 -p /dev/urandom)
rpcallowip=127.0.0.1
testnet=1
txindex=1
EOF

限制一下組態檔的權限

$ /bin/chmod 600 ~user1/.bitcoin/bitcoin.conf

開始 daemon

$ /usr/local/bin/bitcoind -daemon

可以看到

Bitcoin server starting

加上每次開啟 daemon 就開啟比特幣的規則

$ ( /usr/bin/crontab -l -u user1 2>/dev/null; echo "@reboot /usr/local/bin/bitcoind -daemon" ) | /usr/bin/crontab -u user1 -

現在就可以使用比特幣了!!

不過剛裝好大概還在下載區塊,可以用 bitcblock 看看,需要一段時間下載,先休息一下吧!

results matching ""

    No results matching ""