Configure IP address on Ubuntu Linux from CLI
Suppose we need to configure following IPv4 paratmeters:
IP address: 10.10.10.1 Netmask: 255.255.255.0 Gateway: 10.10.10.254
First we need to identify our interface. Parameter “-a” command will show all interfaces even if they are down.
ifconfig -a
We will use interface ens160. Ip configurations needs to be done with super user privilages. Following command will set Ip and netmask:
sudo ifconfig eth1 10.10.10.1 netmask 255.255.255.0 broadcast 10.10.10.255
Short form of the command could also be used. But it will set the default mask for the IPv4 address class which is 255.0.0.0 for A class IP address. In our case we useĀ C class mask – 255.255.255.0.
sudo ifconfig ens160 10.10.10.1
To set default gateway use following command:
sudo route add default gw 10.10.10.254
If you need to static route to some network(192.168.10.0/24) that is not reachable throught gateway this can be done as follows:
sudo route add -net 192.168.10.0 netmask 255.255.255.0 gw 10.10.10.250
To check routing table use following command. Option “-n” will display routes in numeric format.
netstat -rn