Go-Ethereum – Getting Current Network ID in Geth

go-ethereumnetworkidweb3js

How can I know the network id, with web3 or command line, so I know to which network my geth has attached?

Best Answer

A few ideas.

The network ID is output in the logs when the node is first started:

I0110 21:51:32.687353 eth/backend.go:191] Protocol Versions: [63 62], Network Id: 1
I0110 21:51:32.726772 eth/backend.go:219] Chain config: {ChainID: 1 Homestead: 1150000 DAO: 1920000 DAOSupport: true EIP150: 2463000 EIP155: 2675000 EIP158: 2675000}

If you haven't started the node for a while, you could grep the logs for the string.


The output of admin.nodeInfo shows the ID in the protocols sub-section:

> admin.nodeInfo
{
  enode: "enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303",
  id: "44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d",
  ip: "::",
  listenAddr: "[::]:30303",
  name: "Geth/v1.5.0-unstable/linux/go1.6",
  ports: {
    discovery: 30303,
    listener: 30303
  },
  protocols: {
    eth: {
      difficulty: 17334254859343145000,
      genesis: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
      head: "0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a",
      network: 1    <------ HERE!
    }
  }
}

Finally, unless you've manually instantiated the network using --networkid <value> or the --testnet flag, it's currently hard-coded to 1 (the main net).

In protocol.go:

const (
    NetworkId          = 1
    ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message
)