Select Page

Setup Geth Node on AWS Ubuntu 16.04

Launch an EC2 instance

●Login on AWS Console.
●In the top-right corner of the AWS Management Console, choose the AWS Region in which you want to create the DB instance. This example uses the US East (N. Virginia) region.
●Select EC2 from the Services dropdown (top-left), click on Launch Instance.
●Search for Ubuntu 16.04 AMI, & Select Ubuntu 16.04 AMI.
●On the next screen, select instance type c4.xlarge, select public subnet.
●Add root storage 30 GiB, Additionally add 300 GiB GP2 storage to keep geth data, Permanent mount it on /home/crypto
●Select Security Group, Create and download key pair.
●Allow port 22 for your public IP address in security group.
●More instruction to launch an EC2 here.

SSH into your Instance and required softwares

●Connect to the EC2 instance using public IP Address and pem key which you launched earlier.
●Update/Upgrade OS using sudo apt-get update; sudo apt-get upgrade -y; sudo apt-get dist-upgrade -y;
●Install Development Tools using sudo apt-get install libboost-all-dev libboost-dev htop git curl wget zip build-essential unzip -y
●Install Stable Geth from PPA as follows-
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum -y

Some Geth Scripts To be SetUp

●Create directory to keep scripts under /home/ubuntu using mkdir -p /home/ubuntu/scripts
●Here create a file called start-geth.sh using Append following content

# vim /home/ubuntu/scripts/start-geth.sh

#!/bin/bash
## Geth Control Script Main Admin ##
[[ `id -u` = ‘0’ ]] || { echo “[+] Run The Script From Root” ; exit ; }

## Main Variables ##
project=”crypto”
port=”54329″

timer(){
date1=$((`date +%s` + $sleep));
while [ “$date1” -ge `date +%s` ]; do
echo -ne “$msg $(date -u –date @$(($date1 – `date +%s` )) +%H:%M:%S)\r”;
done ;
}
echo “[+] Killing Running Geth Processes”
kill -9 $(ps aux | grep ‘geth ‘ | awk ‘{print $2}’) &> /dev/null
mkdir -p /home/${project}/geths /home/${project}/logs &> /dev/null
chown -R root.root /home/${project}
sleep=”5″
msg=”[+] Starting $project geth after => ”
timer
## MainNet ##
/usr/bin/geth –networkid 1 –datadir /home/${project}/geths/mainnet –syncmode fast –rpc –rpcvhosts “*” –rpccorsdomain “*” –rpcport “${port}” –rpcaddr “0.0.0.0” –ws –wsaddr “0.0.0.0” –wsport $(($port + 1)) –wsorigins “*” –rpcapi “db,net,web3,admin,eth,miner,personal” –verbosity 3 –port $(($port + 2)) &>> /home/${project}/logs/${project}.mainnet.geth.log &
echo “[+] Mainnet Started @ RPC: “${port}” , WS: $(($port + 1)) , P2P: $(($port + 2))”
exit 0
## E O F ##

Geth Monitor Script

vim /home/ubuntu/scripts/monitor.sh

#!/bin/bash
## Monitor ##
[[ `id -u` == “0” ]] || { printf “[-] Run from root / sudo.” ; exit 1 ; }
cpschk=”$(ps aux | grep “/geth ” | grep “mainnet”)”
[[ -z “$cpschk” ]] && {
echo n|bash /home/ubuntu/scripts/start-geth.sh
echo “[+] Started Paused Geth”
} || { echo “[-] Already Running” ; }
exit 0

Add this monitor script to cron, so that if geth get stopped if any reason it can start it.

Crontab -e

* * * * * /bin/bash /home/ubuntu/scripts/monitor.sh

Now you can run the geth mainnet using this script,

bash /home/ubuntu/scripts/start-geth.sh

To check on the progress of your sync, run this:
geth attach ipc:///home/ubuntu/geths/mainnet/geth.ipc –datadir /home/ubuntu/geths/mainnet

& Run below query to check sync status

> eth.syncing
> eth.blockNumber

Allow occupied port for application server from security group

Below ports will be occupied by geth daemon by above scripts:

54329 => MainNet RPC (App Instances)
54330 => MainNet WS (App Instances)
54331 => MainNet P2P (Public)

Please note, It can take upto two days to fully sync with latest block.
And compare the number of blocks in the output with the current height of the blockchain at a site like this: https://etherscan.io/blocks