Use Geth to Setup your Own Private Ethereum Blockchain

Use Geth to Setup your Own Private Ethereum Blockchain

Before starting this tutorial you must have downloaded geth. This tutorial was originally published on the MLG Blockchain blog.

First you must create a genesis.json file with the parameters below. Each field can be changed however you want. It is recommended that you make a random nonce for your private ethereum blockchain to avoid the chance of someone connecting to your private blockchain. The allec object is to prefund accounts on your blockchain with tokens.

<code>

{ “coinbase” : “0x0000000000000000000000000000000000000000”,
“difficulty” : “0x20000”,
“extraData” : “”,
“gasLimit” : “0x2fefd8”,
“nonce” : “0x0000000000000042”,
“mixhash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”,
“alloc”: {
“0x0000000000000000000000000000000000000001”: {
“balance”: “111111111”
},
“0x0000000000000000000000000000000000000002”: {
“balance”: “222222222”
}
}
}

</code>

Once you have setup your genesis.json file you need to run it from your terminal with this command to initiative your private blockchain. Setting a datadir is optional, however it is recommended because otherwise your blockchain ethereum blockchain will be started in a default folder, or can get connected to the live ethereum blockchain if you are running a full ethereum node.

<code>geth — datadir /path/to/genesis.json init genesis.json</code>

This should be returned if you have successfully connected geth to your genesis file.

When executed in your terminal, you should see a success message. You should also see a folder called “chaindata” that was created in the data directory that you initiated your private ethereum blockchain.

You can now connect to your private ethereum blockchain using geth, which will run on your testnet the same way as it does on the live ethereum blockchain. Run the following command to connect geth with your private blockchain.

<code>geth — datadir /path/to/genesis.json</code>

In the message that is returned from the terminal, it is important to note the enode you are listening at and the IPC endpoint to connect other nodes to your private blockchain

This should be returned from your terminal when you successfully connect geth to your private ethereum blockchain instance.

If your terminal returns a message saying that the server has started, you have successfully connected geth to your private ethereum blockchain.

You should now have 3 more folders created in your private ethereum blockchain data directory including dapp, nodekey and nodes.

MLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. See more blockchain development tutorials at www.mlgblockchain.com.