Sunday, December 8, 2013

How to temporary configure the network interfaces with ifconfig


In Linux and Unix, the IP adresses can be set temporary or persistent (stored in the configuration file).
ifconfig is a an “old” generical command for setting temporary IP addresses all over the Unix systems.
The network changes you make with ifconfig will be discarded at reboot.

How to display network information with ifconfig:

Ifconfig displays the IP address, netmask, broadcast, MAC address and other usefull info.
Without any option, ifconfig displays info for all the network interfaces:
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:5d:83:c7
inet addr:192.168.8.180 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5d:83c7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20690 errors:0 dropped:0 overruns:0 frame:0
TX packets:10760 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15025558 (15.0 MB) TX bytes:1730394 (1.7 MB)
Interrupt:19 Base address:0x2000
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:16436 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:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
To display the details for a interface, use ifconfig interface_name:
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0c:29:5d:83:c7
inet addr:192.168.8.180 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5d:83c7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20910 errors:0 dropped:0 overruns:0 frame:0
TX packets:10892 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15044405 (15.0 MB) TX bytes:1747090 (1.7 MB)
Interrupt:19 Base address:0x2000
ifconfig -a displays all the network interfaces, even the disabled ones.

How to temporary enable or disable a network interface:

To enable a network interface, use as root: ifconfig interface_name up.
To disable a network interface, use as root: ifconfig interface_name down.
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 up

How to temporary set the IP address with ifconfig:

To set a temporary IP use, as root, ifconfig interface_name ip_addr.
I will set my IP, on eth0: 192.168.8.185
$ sudo ifconfig eth0 192.168.8.185
$ ifconfig eth0 | grep "inet addr"
inet addr:192.168.8.185 Bcast:192.168.8.255 Mask:255.255.255.0

How to temporary set the IP address and netmask with ifconfig:

To set the temporary IP and netmask, use ifconfig interface_name ip_addr netmask mask_addr up
I will set my ip, on eth1: 192.168.8.188 and mask: 255.255.255.0
$ sudo ifconfig eth1 192.168.8.185 netmask 255.255.255.0 up
$ ifconfig eth1 | grep "inet addr"
inet addr:192.168.8.188 Bcast:192.168.8.255 Mask:255.255.255.0
You can change the IP and mask like this: sudo ifconfig eth0 192.168.8.185/24 . 192.168.8.185 is the IP and /24 is the netmask. 255.255.255.0 is /24, 255.255.0.0 is /16 and 255.0.0.0 is /8 .

How to temporary change the MAC address with ifconfig:

To configure a new MAC address for a interface, you need to set that interface down first and than change the MAC. You can do both this things with the command:
sudo ifconfig interface_name down hw ether AA:BB:CC:DD:EE:FF && ifconfig eth0 up
$ sudo ifconfig eth0 down hw ether AA:BB:CC:DD:EE:FF && ifconfig eth0 up
Or, in the old fashioned way:
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 hw ether AA:BB:CC:DD:EE:FF
$ sudo ifconfig eth0 up

Bonus: How to set a default gateway

A new default gateway can be set with the route tool:
$ sudo route add default gw 10.0.0.1

No comments:

Post a Comment