Beginners guide to Ethereum (4) — connect Ethereum nodes on different machines
Overview
This tutorial might require prior knowledge in previous tutorials. You can always refer to the previous series or leave a commet if you find something hard to understand:)
Instead of using the localhost on the same machine, we are going to connect two computers to our own router, to see if we can actually connect the nodes between different machines.
Things you will need
- two computer that you have access to connect it to a different network
- geth installed (refer to the first tutorial if haven’t installed)
- a router (here I use D-Link)
Concepts needed
- To let two nodes connected on the same chain and same network, you have to have the same genesis file (the first block has to be the same) and same networkid.
- Since we are not on the same machine, parameters such as “datadir” and “port” ,where the node is running on, don’t have to be different.
Before we start — Config our router settings
I will assume this is your first time to use a router (because I am too XD).
- First connect your router to power
- open wifi preferences and connect to a network called dlink
Note that here we haven’t set it with internet connection, so you’ll be offline once connected to this network.
*If you are not going to change your network’s name, skip the following and jump to the “start coding” session. But it’s always good to have a taste of how to customize this router by going through a simple example.
3. Open the web browser and go to 192.168.0.1 (This brings you to the router’s settings page)
4. As specified in the guidebook, the password for login is ‘’(leave this field blank)
5. As to keep things simple, we are not going to set up internet connection, go to the wireless setup in the left nav bar and click manual wireless connection setup on the buttom.
6. Find a column called “Wireless Network Name” and set this value to the name you want.
7. Now wait for it to reconnect and you are good to go!
Start coding!
In terminal,
- create an empty folder for testing(optional)
mkdir test
cd test
2. See what is your ip address
ifconfig|grep netmask|awk '{print $2}'
output:
127.0.0.1
192.168.0.100
The first value is localhost, and the second value is your ip address
Reference (official document): https://github.com/ethereum/go-ethereum/wiki/Setting-up-private-network-or-local-cluster
3. Init your node with the same genesis file
The reason I don’t use the default settings is because the default mining difficulty is too high which is not good for testing.
cat > genesis.json
and paste
{"config": {},"gasLimit": "2000000","difficulty": "10","alloc": {}}
PressEnter
and Ctrl+C
to save and exit
geth init genesis.json --datadir hi
4. Open our node on network id 123, set the rpcaddr to our new ip, and open geth console (the flag — port 3000 is optional, easier to follow)
geth --networkid 123 --datadir hi --rpc --rpcaddr 192.168.0.100 --port 3000 console // remember to use your own ip
- Remmeber to enable rpc server by adding
—-rpc
flag before setting rpcaddr
(Reference: https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options)
5. Now switch to the other computer and repeat step 1 to 4
Note that the rpcaddr should be differnt based on the ip address you get.
6. Get the enode value on one of your nodes and use this to let the other node connect with this.
In computer A:
admin.nodeInfo.enode
In computer B:
admin.addPeer("enode://46366e8dfb79923e7f6ceedca30a288cfd63e47bf3684a2952e27e28c6a72dd9499cf93249fc244fe39ebdfef565c54858a04bc771832c9311c9e09131aaef78@192.168.0.100:3000")
- Remember to replace [::] with your corresponding IP address. If you leave this as it is now, it will be parsed to localhost by default
before: enode://…@[::]:3000
after: enode://…@192.168.0.100:3000
7. Now you should see the output is no longer an empty array :)
admin.peers
sample output: (in computer A)
[{caps: ["eth/63"],id: "0270018d6fea493ba2c7f90d2be95ab9e8cff50d211792babf4f27866f0d90451d939ce0f7c19789f3ec3b9d4215c1091dd41f4e3c2bfcd3feb90dafc4d7d769",name: "Geth/v1.6.6-stable-10a45cb5/darwin-amd64/go1.8.3",network: {localAddress: "192.168.0.100:3000",remoteAddress: "192.168.0.101:53693"},protocols: {eth: {difficulty: 10,head: "0x54d897dc8f82f8995bc47f78381aae21981031c84090c3e5f8b64a1dd3ec32ef",version: 63}}}]
You can find the node id is the same as the node in computer B (use the following command to see the properties in a node)
admin.nodeInfo
Problems you may encounter …
- You may find it difficult to exchange the enode value between computers. You can use a unix utility called “nc” (netcat) for reading and writing to network connections using TCP or UDP (so we can use this to transfer msg)
Open a new terminal window for each computer.
In computer A:
nc -l 12345 // the "-l" flag is for listening on port 12345
In computer B:
nc 192.168.0.100 12345 // (without the "-l" flag) This means connecting to computer A's ip address on port 12345
Now you can type whatever msgs on either terminals and receive them in real time like a live chat.
2. You may accidentally use different genesis file. With the command admin.nodeInfo
, you should see the same hash code under the “genesis” parameter.
Wrap up
Now you have more experience in using geth to open a ethereum node on your customized blockchain, is able to connect your nodes on different machines, and get a taste of the basics of a router.
Thank you for your patient reading and supports :) Comments and suggestions are welcomed!