Ubuntu以太坊私有链搭建

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
联系方式:jian1098@qq.com
版权声明:文章仅在本人博客和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,说明没有同步完成,如果得到最新的区块,说明已经同步到最新区块了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装Ubuntu系统 首先需要在服务器上安装Ubuntu系统(建议使用16.04及以上版本)。可以使用云服务器或者本地安装。 2. 安装Geth Geth是以太坊的官方客户端,可以用于搭建私有链和联盟链。可以使用以下命令安装: sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install -y ethereum 安装完成后,可以使用以下命令查看版本号: geth version 3. 创建创世块 首先需要创建创世块,创世块是区块链的第一个区块,包含了一些初始化的信息,如初始账户、初始余额、挖矿难度等。可以使用以下命令创建创世块: geth --datadir /path/to/chaindata init /path/to/genesis.json 其中,/path/to/chaindata为存储区块链数据的路径,/path/to/genesis.json为创世块的配置文件。 4. 启动节点 使用以下命令启动节点: geth --datadir /path/to/chaindata --networkid 1234 --nodiscover --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi "eth,web3,personal,net" console 其中,/path/to/chaindata为存储区块链数据的路径,--networkid指定联盟链的ID,--nodiscover禁用节点发现功能,--rpc启用RPC服务,--rpcaddr指定RPC服务监听的IP地址,--rpcport指定RPC服务监听的端口号,--rpcapi指定可用的RPC接口,console表示启动控制台。 5. 创建账户 在控制台中使用以下命令创建账户: personal.newAccount("password") 其中,password为账户的密码。 6. 启动挖矿 使用以下命令启动挖矿: miner.start() 7. 部署智能合约 使用Solidity编写智能合约代码,并使用Remix或Truffle等工具进行编译和部署。 以上就是在Ubuntu系统上搭建以太坊联盟链的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值