In this guide, we'll walk through the process of creating a private Ethereum network using Geth, the Go-language Ethereum client.
Getting Started with Geth
Install Geth:
Download and install Geth from the official GitHub page. Follow the step-by-step instructions for your operating system.Note: Geth frequently updates, so JSON configurations and
enodedata in this guide may become outdated.Running Geth:
Execute the following command to sync with the Ethereum mainnet:geth
Understanding Network Identity
Ethereum networks are distinguished by two critical components:
- Network ID: A unique identifier for the blockchain.
- Genesis Block: The first block of the chain.
If two chains share the same Network ID and Genesis Block, the longer chain will overwrite the shorter one. This guide includes safeguards to prevent unintended overwrites.
Configuring Your Private Chain
Step 1: Specify Data Directory
Store private chain data separately from the mainnet using --datadir:
geth --datadir "./privatechain" --networkid 123Step 2: Create Genesis Block
Save the following JSON as custom_genesis.json:
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x123456789abcdef",
"gasLimit": "0x0123123",
"difficulty": "0x400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
"3282791d6fd713f1e94f4bfd565eaa78b3a0599d": {
"balance": "1337000000000000000000"
}
}
}extraData: Optional 32-byte field for custom data.alloc: Pre-funds specified addresses with ETH (in wei).
Step 3: Initialize Genesis Block
geth --datadir "./privatechain" --networkid 123 init custom_genesis.jsonSuccessful initialization displays: successfully wrote genesis block and/or chain rule set: …
Step 4: Launch the Console
geth --datadir "./privatechain" --networkid 123 consoleRemember: Always include--datadirand--networkidto connect to your private chain.
Advanced Configuration
Enhance security and performance with these flags:
--maxpeers 5: Limit peer connections.--nodiscover: Disable automatic peer discovery.--netrestrict 192.168.0.0/16: Restrict connections to a specific IP subnet.
Example:
geth --datadir "./privatechain" --networkid 123 --maxpeers 5 --nodiscover --netrestrict 192.168.0.0/16 consoleBuilding a Private Network
To connect multiple nodes:
- Obtain Node Info:
Runadmin.nodeInfoin the console to getenodedetails (ID, IP, port). Connection Methods:
Bootnodes Flag:
geth --bootnodes enode://<node-id>@<ip>:30303Manual Peer Addition:
admin.addPeer("enode://<node-id>@<ip>:30303")Static Nodes File:
Save peers inprivatechain/static-nodes.json:["enode://<node-id>@<ip>:30303"]
Verify connections with admin.peers.
Additional Tips
- Identity: Use
--identity "NodeName"for easier peer identification. - Logging: Adjust verbosity with
--verbosity(0-6). Mining: Enable auto-mining via
--mineor control manually:miner.start() miner.stop()
👉 Explore more Ethereum development tools
FAQ
Q1: Can I run multiple private chains simultaneously?
A1: Yes, by specifying different --datadir and --networkid for each chain.
Q2: How do I reset my private chain?
A2: Delete the privatechain folder and reinitialize the genesis block.
Q3: Why isn’t my node connecting to peers?
A3: Ensure --nodiscover isn’t enabled or use manual peer addition.
Q4: What’s the purpose of extraData?
A4: It allows miners to embed custom data (32 bytes max) in blocks.
Q5: How can I fund additional accounts?
A5: Use alloc in the genesis JSON or send ETH via transactions after mining.