:2026-09-17 17:21 点击:2
以太坊作为目前最流行的智能合约平台,其公有链虽然功能强大,但在某些场景下(如企业内部应用、私有数据测试、特定业务逻辑验证等),搭建一个私有链或联盟链更为合适,本文将详细介绍如何在Ubuntu操作系统上,从零开始搭建一个以太坊私有链。
sudo apt update sudo apt upgrade -y
# 下载Go(以1.19.5为例,请根据需要替换版本号) wget https://go.dev/dl/go1.19.5.linux-amd64.tar.gz # 解压到/usr/local sudo tar -C /usr/local -xzf go1.19.5.linux-amd64.tar.gz # 添加到环境变量 echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc source ~/.bashrc # 验证安装 go version
build-essential。sudo apt install -y build-essential
Geth(Go-Ethereum)是以太坊官方的Go语言实现,功能全面,常用于搭建私有链。
sudo apt install -y git make
git clone https://github.com/ethereum/go-ethereum.git cd go-ethereum
# 编译 make geth # 或者直接使用make all编译所有工具 make all # 编译完成后,geth可执行文件在/build/bin目录下,可以将其添加到PATH方便使用 # 假设当前在go-ethereum目录): export PATH=$PATH:$PWD/build/bin # 为了永久生效,可以将上述export命令添加到~/.bashrc echo "export PATH=$PATH:$PWD/build/bin" >> ~/.bashrc source ~/.bashrc
geth version
如果看到版本信息,则说明安装成功。
私有链需要一个独特的创世区块配置文件。
创建创世区块配置文件:在用户目录下创建一个ethereum目录,并在其中创建一个genesis.json文件。
mkdir -p ~/ethereum cd ~/ethereum nano genesis.json
粘贴到genesis.json文件中(这是一个基本的PoA权威证明创世配置,适合私有链):
{
"config": {
"chainId": 15, // 私有链的ID,用于区分不同的以太坊网络,避免与公有链冲突
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"mergeNetsplitBlock": 0,
"istanbul": {
"epoch": 30000,
"period": 1
},
"clique": { // 使用Clique共识算法,适合私有链/联盟链
"period": 15, // 出块时间(秒)
"epoch": 30000,
"blocktime": 15
}
},
"nonce": "0x0",
"timestamp": "0x0",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", // 可以预留签名者地址
"gasLimit": "0xffffffff",
"difficulty": "0x40000", // 初始难度,私有链可以设置低一些
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000", // 矿工地址
"alloc": {} // 预分配账户,可以在这里给某些地址预先分配以太币
}
说明:
chainId:自定义一个唯一的链ID,比如15。clique:这是以太坊用于私有链/联盟链的一种权威证明(PoA)共识算法,不需要挖矿,由授权节点轮流出块。alloc:如果需要预分配一些以太币给特定地址,可以在这里添加,"alloc": {
"0x1234567890123456789012345678901234567890": {
"balance": "1000000000000000000000" // 1000 ETH (以wei为单位)
}
}
初始化创世区块:
geth --datadir ~/ethereum/data init ~/ethereum/genesis.json
--datadir:指定数据存储目录,这里我们创建了~/ethereum/data。~/ethereum/data目录下会生成geth和keystore等文件夹。基本启动命令:
geth --datadir ~/ethereum/data --networkid 15 --nodiscover --maxpeers 0 --http --http.addr "0.0.0.0" --http.port "8545" --http.api "personal,eth,net,web3,txpool,miner"
--datadir ~/ethereum/data:指定数据目录。--networkid 15:指定网络ID,与创世区块配置中的chainId保持一致。--nodiscover:禁止自动发现其他节点,因为我们是在私有链环境中。--maxpeers 0:限制最大连接数为0,即不与其他节点连接(单节点私有链)。--http:启用HTTP-RPC服务。--addr "0.0.0.0":允许任何IP地址访问HTTP-RPC接口。--http.port "8545":指定HTTP-RPC端口,默认8545。--http.api "personal,eth,net,web3,txpool,miner":开放的HTTP-RPC API列表。后台启动(可选):
如果希望节点在后台运行,可以使用nohup或screen/tmux工具。
使用nohup:
nohup geth --datadir ~/ethereum/data --networkid 15 --nodiscover --maxpeers 0 --http --http.addr "0.0.0.0" --http.port "8545" --http.api "personal,eth,net,web3,txpool,miner" > ~/ethereum/geth.log 2>&1 &
这样,Geth的日志会输出到~/ethereum/geth.log文件中。
连接到私有链节点:
你可以使用Mist(MetaMask早期版本,已不更新)、Remix IDE的内置环境,或者通过geth的控制台连接。
使用Geth控制台:
在新的终端窗口中,执行:
geth attach ~/ethereum/data/geth.ipc
或者(如果启用了HTTP-RPC):
geth attach http://localhost:8545
进入控制台后,你会看到类似 Welcome to the Get 的提示。
控制台常用命令:
eth.blockNumber:查看当前区块高度。eth.accounts:查看节点中的账户列表。personal.newAccount("your_password"):创建一个新账户,并设置密码。本文由用户投稿上传,若侵权请提供版权资料并联系删除!