How to Configure IP, Gateway, Netmask, MAC Address, and DNS on Linux
Before changing network settings, first check the active network interfaces with ifconfig:
eth0 Link encap:Ethernet HWaddr b8:27:eb:1b:63:a8
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::6502:67ff:89b:b2fd/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:43239 errors:0 dropped:24 overruns:0 frame:0
TX packets:30644 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25068614 (23.9 MiB) TX bytes:5993030 (5.7 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1961 errors:0 dropped:0 overruns:0 frame:0
TX packets:1961 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:160232 (156.4 KiB) TX bytes:160232 (156.4 KiB)
From there, you can update the main network parameters one by one.
Set the IP address and subnet mask
Use ifconfig to assign the address and netmask to eth0:
ifconfig eth0 192.168.1.6 netmask 255.255.255.0
Set the default gateway
Add the gateway with route:
route add default gw 192.168.1.1
Change the network card MAC address
Bring the interface down first, modify the hardware address, and then bring it back up:
ifconfig eth0 down
ifconfig eth0 hw ether b877c322f8
ifconfig eth0 up
Configure DNS
One common way is to edit /etc/resolv.conf directly:
vi /etc/resolv.conf
nameserver 114.114.114.114
nameserver 114.114.115.115
After that, restart the network service to apply the changes:
service network restart
On newer Ubuntu versions, simply editing /etc/resolv.conf is no longer enough. In that case, the DNS settings need to be placed in the resolvconf configuration and then updated with the resolvconf command.
Edit the head file under /etc/resolvconf/resolv.conf/:
nameserver 114.114.114.114
Then refresh the configuration so it takes effect:
resolvconf -u