Tuesday, 20 May 2014

CLI ifconfig – How to setup IP addess from Command Line in Linux

Posted by krishna

Did you even had trouble with Network Manager or ifconfig and felt that you need to try to set up static IP address from command line / CLI ifconfig? I accidentally removed Gnome (my bad, wasn’t paying attention and did an apt-get autoremove -y .. how bad is that.. ) So I had a problem, I can’t connect to Internet to reinstall my Gnome Network Manager cause I’m in TEXT mode. Similarly I broke my network manager cause I was trying to use VPN and it just wouldn’t come back. I tried reinstalling it, but you need Internet for that. So here’s a small guide for that you can setup IP address and networking from Linux Command Line or CLI. You’ll be able to  browse it from your mobile device and make things work.
How to fix Wired Network interface is Unmanaged error in Debian or Kali Linux - 7 - blackMORE Ops

Firstly STOP and START Networking service

Some people would argue restart would work, but I prefer STOP-START to do a complete rehash. Also if it’s not working already, why bother?
# /etc/init.d/networking stop
 [ ok ] Deconfiguring network interfaces...done.
 # /etc/init.d/networking start
 [ ok ] Configuring network interfaces...done.

STOP and START Network-Manager

If you have some other network manager (i.e. wicd, then start stop that one).

# /etc/init.d/network-manager stop
 [ ok ] Stopping network connection manager: NetworkManager.
 # /etc/init.d/network-manager start
 [ ok ] Starting network connection manager: NetworkManager.
Just for the kicks, following is what restart would do.. similar I still prefer stop/start combination.
# /etc/init.d/network-manager restart
 [ ok ] Stopping network connection manager: NetworkManager.
 [ ok ] Starting network connection manager: NetworkManager.

Now to bring up your interface:

# ifconfig eth0 up
 # ifconfig eth0
 eth0      Link encap:Ethernet  HWaddr aa:bb:cc:11:22:33
 UP BROADCAST MULTICAST  MTU:1500  Metric:1
 RX packets:0 errors:0 dropped:0 overruns:0 frame:0
 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Now lets set IP, subnet mask, broadcast address.

# ifconfig eth0 192.168.43.226
 # ifconfig eth0 netmask 255.255.255.0
 # ifconfig eth0 broadcast 192.168.43.255
Let check the outcome:
# ifconfig eth0
 eth0     Link encap:Ethernet  HWaddr aa:bb:cc:11:22:33
 inet addr:192.168.43.226  Bcast:192.168.43.255  Mask:255.255.255.0
 UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 RX packets:19325 errors:0 dropped:0 overruns:0 frame:0
 TX packets:19641 errors:0 dropped:0 overruns:0 carrier:0
 collisions:0 txqueuelen:1000
 RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
and try to ping Google.com (cause if google.com is down, Internet is broken).
# ping google.com
 ping: unknown host google.com
Ah Internet is broken. Maybe not! So what went wrong in our side.

Simple, we didn’t add any default Gateway. Let’s do that

# route add default gw 192.168.43.1 eth0
and Just to confirm:
# route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 0.0.0.0         192.168.43.1    0.0.0.0         UG    0      0        0 eth0
 192.168.43.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
Looks good to me, lets ping google.com again:
# ping google.com
 PING google.com (119.30.40.16) 56(84) bytes of data.
 64 bytes from cache.google.com (119.30.40.16): icmp_req=1 ttl=49 time=520 ms
 64 bytes from cache.google.com (119.30.40.16): icmp_req=2 ttl=49 time=318 ms
 64 bytes from cache.google.com (119.30.40.16): icmp_req=3 ttl=49 time=358 ms
 64 bytes from cache.google.com (119.30.40.16): icmp_req=4 ttl=49 time=315 ms
 ^C
 --- google.com ping statistics ---
 4 packets transmitted, 4 received, 0% packet loss, time 3002ms
 rtt min/avg/max/mdev = 315.863/378.359/520.263/83.643 ms
Done.

0 comments:

Post a Comment