< Day Day Up > |
Managing Network Interfaces from the Command Line: ifconfigYou are already familiar with managing your network interfaces through the Network System Preferences pane, but if you need to, you can also manage your network interfaces from the command line, using the ifconfig utility. Viewing Current Network StatusIf you have ever changed your network location, and then found yourself unable to make a network connection, a convenient way to check your current network status is to use the ifconfig command. The ifconfig command displays and sets interface information on your system. The following is an example of running ifconfig -a, which lists all the interfaces on your machine: brezup:sage sage $ ifconfig -a lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1 prefixlen 64 scopeid 0x1 gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280 stf0: flags=0<> mtu 1280 en0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC, SIMPLEX,MULTICAST> mtu 1500 inet6 fe80::20d:93ff:fe49:f56 prefixlen 64 scopeid 0x4 inet 140.254.104.243 netmask 0xfffffe00 broadcast 140.254.105.255 ether 00:0d:93:49:0f:56 media: autoselect (100baseTX <full-duplex>) status: active supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 2030 lladdr 00:0d:93:ff:fe:49:0f:56 media: autoselect <full-duplex> status: inactive supported media: autoselect <full-duplex> Typically, the interface of interest would be en0, or for an AirPort card, en1. From this output, we see that interface en0 has been assigned the IP address 140.254.104.243, with a netmask of 0xfffffe00 (the hexadecimal for 255.255.254.0). We see that its MAC address is 00:0d:93:49:0f:56, that the interface is active, that it autoselects what speed to use, and that it is currently running at 100BaseTX. Configuring Network InterfacesNot only can you use ifconfig to view the current network status of your machine, but you can also use it to configure your network interface. You could, for example, change your machine's IP address using ifconfig. Here is the current IP address of interface en0: creampuf:~ joray$ ifconfig en0 en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 inet6 fe80::230:65ff:feca:f9a2 prefixlen 64 scopeid 0x2 inet 192.168.1.200 netmask 0xffffff00 broadcast 192.168.1.255 ether 00:30:65:ca:f9:a2 media: autoselect (10baseT/UTP <half-duplex>) status: active supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback>100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex> 1000baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex,flow-control> 1000baseTX <full-duplex ,flow-control,hw-loopback> To change it to 192.168.1.201 with a netmask of 255.255.254.0, do the following (from the console or you'll be a little surprised when your connection drops): creampuf:~ joray$ sudo ifconfig en0 192.168.1.201 netmask 255.255.254.0 Now en0 has the new IP address and netmask as specified: creampuf:~ joray$ ifconfig en0 en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 inet6 fe80::230:65ff:feca:f9a2 prefixlen 64 scopeid 0x2 inet 192.168.1.201 netmask 0xfffffe00 broadcast 192.168.1.255 ether 00:30:65:ca:f9:a2 media: autoselect (10baseT/UTP <half-duplex>) status: active supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP < full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100ba seTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex> 1000baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex,flow-control> 1000b aseTX <full-duplex,flow-control,hw-loopback> If you want to change only the IP address without changing the netmask, run creampuf:~ joray$ sudo ifconfig en0 192.168.1.201 Select command documentation for ifconfig is included in Table 28.1. If you needed to move your IP address from one interface to the other, you might find ifconfig down and ifconfig up to be useful.
Creating Interface AliasesAs you saw in Chapters 22 and 23, sometimes it can be helpful to have multiple IP addresses bound to the same interface. You can easily do this in the Network Preferences by duplicating your interface and editing the duplicate interface, but you can also use ifconfig to do this. The syntax is ifconfig <interface> alias <additional IP address> 255.255.255.255 To add 192.168.1.201 as an additional IP address for en0, we do the following: creampuf:~ joray$ sudo ifconfig en0 alias 192.168.1.201 255.255.255.255 Now we can see that en0 has two IP addresses: creampuf:~ joray$ ifconfig en0 en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 inet6 fe80::230:65ff:feca:f9a2 prefixlen 64 scopeid 0x2 inet 192.168.1.200 netmask 0xffffff00 broadcast 192.168.1.255 inet 192.168.1.201 netmask 0xffffff00 broadcast 255.255.255.255 ether 00:30:65:ca:f9:a2 media: autoselect (10baseT/UTP <half-duplex>) status: active supported media: none autoselect 10baseT/UTP <half-duplex> 10baseT/UTP <full-duplex> 10baseT/UTP <full-duplex,hw-loopback> 100baseTX <half-duplex> 100baseTX <full-duplex> 100baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex> 1000baseTX <full-duplex,hw-loopback> 1000baseTX <full-duplex,flow-control> 1000baseTX <full-duplex ,flow-control,hw-loopback> To make this change permanent, be sure to add that command to your startup scripts. |
< Day Day Up > |