Linux以太坊节点搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_jian/article/details/83274696

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
联系方式:[email protected]
版权声明:文章仅在本人博客和CSDN博客中发布,所有文章未经授权禁止转载!

安装Geth

root@dserv834-mtl8:~#  apt-get install software-properties-common
root@dserv834-mtl8:~#  add-apt-repository -y ppa:ethereum/ethereum
root@dserv834-mtl8:~#  apt-get update
root@dserv834-mtl8:~#  apt-get install ethereum

安装后查询到geth版本即为安装成功

root@dserv834-mtl8:~# which geth  ###查看geth路径
/usr/bin/geth
root@dserv834-mtl8:~# geth version
Geth
Version: 1.8.12-stable
Git Commit: 37685930d953bcbe023f9bc65b135a8d8b8f1488
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.10
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.10

创建Geth启动/停止脚本

查看本地IP地址,得到内网地址为173.209.49.10

root@dserv834-mtl8:~# ifconfig -a
eno1      Link encap:Ethernet  HWaddr ac:1f:6b:82:8c:f4  
          inet addr:173.209.49.10  Bcast:173.209.49.15  Mask:255.255.255.248
          inet6 addr: fe80::ae1f:6bff:fe82:8cf4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:164109642 errors:0 dropped:10 overruns:0 frame:0
          TX packets:143189840 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:87852826043 (87.8 GB)  TX bytes:35500135072 (35.5 GB)
          Memory:df200000-df27ffff 

创建geth启动脚本starteth.sh,将第二行开始的脚本复制保存,需要修改rpcaddr的ip,datadir可以省略,一般将datadir改为大容量的数据盘目录

root@dserv834-mtl8:~# vi starteth.sh 
#!/bin/bash

nohup geth --rpcaddr "173.209.49.10" --rpc --rpccorsdomain="*" --datadir "/data/ethereum" >> geth.log 2>&1 &

创建geth停止脚本stopeth.sh,将第二行开始的脚本复制保存

root@dserv834-mtl8:~# more stopeth.sh 
#!/bin/bash

PIDS=`ps -ef |grep geth |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
	kill -9 $PIDS
else
	echo `date +%F" "%H:%M:%S` "Geth is NOT runing!"
fi

配置脚本执行权限

root@dserv834-mtl8:~# chmod u+x st*.sh
root@dserv834-mtl8:~# ls -la *.sh
-rwxr--r-- 1 root root    95 Jul 10 11:41 starteth.sh
-rwxr--r-- 1 root root   171 Jul 11 21:49 stopeth.sh

启动geth程序同步区块数据

启动Geth开始同步数据,通过查看geth.log来监控Geth的同步情况

root@dserv834-mtl8:~# ./starteth.sh
root@dserv834-mtl8:~# tail -f geth.log
INFO [07-11|21:51:06.609] Imported new chain segment               blocks=1  txs=55   mgas=7.992   elapsed=368.336ms  mgasps=21.697  number=5948083 hash=14c024…a9d1bb cache=267.07mB
INFO [07-11|21:51:10.051] Imported new chain segment               blocks=1  txs=43   mgas=7.987   elapsed=105.155ms  mgasps=75.959  number=5948084 hash=ede212…56e3b8 cache=267.22mB
INFO [07-11|21:51:13.530] Imported new chain segment               blocks=1  txs=133  mgas=7.961   elapsed=431.951ms  mgasps=18.431  number=5948085 hash=533343…c40637 cache=267.59mB
INFO [07-11|21:51:17.785] Imported new chain segment               blocks=1  txs=51   mgas=7.982   elapsed=304.528ms  mgasps=26.211  number=5948086 hash=8918b3…263ea8 cache=268.06mB
..........

一般情况下geth从0开始同步需要3天左右的时间,由服务器的带宽和性能决定,可以购买境外的服务器,如香港的阿里云。

判断同步完成

geth在同步的时候,通过geth console查看eth.blockNumber命令查询区块高度,如果得到的结果是0,说明没有同步完成,如果得到最新的区块,说明已经同步到最新区块了。

猜你喜欢

转载自blog.csdn.net/C_jian/article/details/83274696
今日推荐