Managing Network Interfaces from the Command Line: ifconfig

You 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 Status

If 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 Interfaces

Not 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.

Table 28.1. Command Documentation Table for ifconfig
ifconfigConfigures network interface parameters
ifconfig [-L] [-m] <interface> [create] <address_family> [<address[/prefixlength]> [<dest address>]] [<parameters>]
ifconfig <interface> destroy
ifconfig -a [-L] [-d] [-m] [-u] [<address_family>]
ifconfig -l [-d] [-u] [<address_family>]
ifconfig [-L] [-d] [-m] [-u]
ifconfig <interface> vlan <vlan-tag> vlandev <iface>
ifconfig <interface> -vlandev <iface>
ifconfig <interface> bonddev <iface>
ifconfig <interface>-bonddev <iface>
ifconfig assigns an address to a network interface and/or configures network interface parameters. It must be used at boot time to define the network address of each network interface. It may also be used at a later time to redefine an interface's network address or other operating parameters.
Only the superuser can modify the configuration of a network interface.
-mIf passed before an interface name, ifconfig displays all the supported media for the specified interface.
-aProduces a full listing of all available interfaces.
-lProduces a name-only listing of all available interfaces.
-dLimits a listing to those interfaces that are down.
-uLimits a listing to those interfaces that are up.
Available options for ifconfig are
<address>For the DARPA-Internet family, the address is either a hostname in the hostname database or a DARPA-Internet address expressed in the Internet standard dot notation.
<address family>Specifies the <address family>, which affects the interpretation of the remaining parameters. The address or protocol families currently supported are inet, iso, and ns.
<dest address>Specifies the address of the correspondent on the other end of a point to point link.
<interface>The <interface> parameter is a string of the form <name of physical unit>, such as en0.
The following parameters may be set with ifconfig:
addAnother name for the alias parameter. Introduced for compatibility with BSD/OS.
aliasEstablishes an additional network address for this interface.
-aliasRemoves the network address specified.
broadcast(inet only) Specifies the address to use to represent broadcasts to the network. The default broadcast address is the address with a host part of all 1s.
deleteRemoves the network address specified. This is used if you incorrectly specified an alias or if it's no longer needed.
downMarks an interface as down. When an interface is marked down, the system does not attempt to transmit messages through that interface. If possible, the interface is reset to disable reception as well. This doesn't automatically disable routes using the interface.
tunnel <src-addr> <dest-addr>(IP tunnel devices only.) Configures the physical source and destination address for IP tunnel interfaces (gif(4)). The arguments <src_addr> and <dest_addr> are interpreted as the outer source/destination for the encapsulating IPv4/IPv6 header.
deletetunnelUnconfigures the physical source and destination address for IP tunnel interfaces previously configured with tunnel.
createCreates the specified network pseudo-device.
destroyDestroys the specified network pseudo-device.
mtu <n>Sets the maximum transmission unit of the interface to n; default is interface specific. The MTU is used to limit the size of packets that are transmitted on an interface. Not all interfaces support setting the MTU, and some interfaces have range restrictions.
netmask <mask>(inet and ISO) Specifies how much of the address to reserve for subdividing networks into subnetworks. The mask includes the network part of the local address and the subnet part, which is taken from the host field of the address.
UpMarks an interface as up. Can be used to enable an interface after ifconfig down has been run. It happens automatically when setting the first address on an interface. If the interface was reset when previously marked down, the hardware is reinitialized.

Creating Interface Aliases

As 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.171