Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How does a router assign an IP address? Is this random or is there a set method?

It seems that the same device – regardless of the time it was connected – seems to get the same IP assigned. Does the router know the MAC address and assign the same IP or is there something else going on?

share|improve this question
An ip on a network or your own personal pc? – Christopher Chipps Mar 31 '13 at 0:36
if you got the time and care to learn it in deep, peek in: – Lorenzo Von Matterhorn Mar 31 '13 at 0:39
up vote 12 down vote accepted

IPv4 addresses are usually assigned using the DHCP protocol. How this happens, depends on the particular DHCP server running on the router...

  • With DHCP, addresses are leased for a certain time period, so if a device reboots requests an address before the old lease expires, the router usually gives the same old address (based on DHCP client ID, or on the MAC address).

  • Some DHCP servers remember which address was issued even for a while after the lease expires, so they always give the same address to the same device.

  • If the device wasn't seen previously, it depends on the implementation – usually the new address is chosen randomly, but sometimes sequentially, and sometimes based on some sort of a hash on the MAC address so that the router at least tries to give the old address again.

For example, to quote the manual page dhcpd.conf(5) of the ISC DHCP server dhcpd:

The  DHCP  server  generates  the list of available IP addresses from a
hash table.  This means that the addresses are not sorted in  any  par‐
ticular  order, and so it is not possible to predict the order in which
the DHCP server will allocate IP addresses.  Users of previous versions
of  the  ISC  DHCP server may have become accustomed to the DHCP server
allocating IP addresses in ascending order, but this is no longer  pos‐
sible, and there is no way to configure this behavior with version 3 of
the ISC DHCP server.

For IPv6, there is a similar protocol DHCPv6, to which the answer above still applies.

However, many networks use a simpler "stateless autoconfiguration" protocol in which the router only broadcasts the prefix (aka network address), and devices assign their own IP addresses. The assignment is done in two ways:

  • With normal "Stateless Autoconfiguration" (RFC 4862), the 'host' part of IP addresses is based on the device's hardware address. For 48-bit MAC addresses, the second bit is flipped, and ff:fe is inserted in the middle (to pad to 64 bits)

    For example, prefix 2001:470:1f0b:915::/64 plus MAC address 48:5d:60:e8:65:8f results in IP address 2001:470:1f0b:915:4a5d:60ff:fee8:658f.

  • With "Privacy Extensions" (RFC 4941), the 'host' part is chosen randomly – and a new address is added every 10 hours, too.

    Note that "Privacy Extensions" are almost always used in addition to the normal "stateless" MAC-based IP address.

share|improve this answer
You mention IPv4, is this any difference with IPv6? – agz Mar 31 '13 at 18:10
@agovizer: Yes – I updated the answer. – grawity Mar 31 '13 at 18:29
If it's simply a hash, wouldn't a single IP address be applied to more than one host? – Pacerier Sep 9 '13 at 16:13
@Pacerier: Depends on how well-designed the hash is, how many IP addresses you get to choose from, etc. Also note that I said "tries". If the resulting IP address is already in use, the router might just assign another one. – grawity Sep 9 '13 at 19:21

Generally (for IPV4), the system used is called "DHCP", and works, broadly, as follows.

  • The router runs a "DHCP Server", which has a table with IP's which can be used.
  • Each computer makes a DHCP request, which is basically a packet (with its MAC address) sent on the network saying "What IP can I have ?"
  • The router looks at the packet received and says here is your IP address, gateway, DNS server [and anything else]
  • The computer then configures itself.

The MAC address can be used by the DHCP server table to make sure the machine gets the same IP address, if available each time.

share|improve this answer

I think it is important to point out that routers do not assign IP addresses: routeurs receive an IP packet on an interface and send it almost unchanged on another interface (the changes I can think of is decreasing the TTL and updating the checksum of the IP packet; even changing the IP address in the packet (NAT) is beyond the task of a router).

The devices that are called home routers are much more than routers, they also include some of the following functionalitys/services:

  • DHCP Server (this is the service that assigns IP addresses),
  • ADSL Modem (to negotiate a connection to the ISP),
  • Firewall (to filter incoming and outgoing connections),
  • Inbound and outbound NAT device (to share the single IPv4 address allocated by the ISP for all the devices on the home network),
  • Router,
  • Switch,
  • WiFi Antenna,
  • Web Server,
  • Console (web, ssh or telnet) to configure the device,
  • Others...

(Note: firewalls can usually do Inbound and outbound NAT and routing, even though those 4 functions can be done using dedicated devices.)

In large Enterprise deployment, the DHCP Server is often hosted on dedicated Linux or Windows servers (usually a cluster) that does not have anything to do with routers.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .