How to determine gateway address when going from DHCP to static IP address with Ubuntu
Solution 1:
To get your gateway's IP address, use the route
command with the -n
flag to translate hostnames into IP addresses (or rather, disable DNS lookups on the IP address).
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 10.10.10.1 0.0.0.0 UG 100 0 0 eth0
The default gateway is the one where the destination is 0.0.0.0
, in this case, 10.10.10.1
.
Though I'm wondering, why are you manually assigning a static IP? Does your access device (router, wifi, whatever) not allow DHCP reservations? Most devices do these days. You just need to provide the MAC address, and many will already know the MAC by hostname. If your's doesn't you can get the MAC from ip
, the "link/ether" address:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:50:56:10.10:21 brd ff:ff:ff:ff:ff:ff
inet 10.10.10.121/24 brd 10.10.10.255 scope global eth0
inet6 fe80::250:56ff:fe10.1021/64 scope link
valid_lft forever preferred_lft forever
or ifconfig
, the "HWaddr" address:
$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:50:56:10.10:21
inet addr:10.10.10.121 Bcast:10.10.10.255 Mask:255.255.255.0
inet6 addr: fe80::250:56ff:fe10.1021/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:107139 errors:0 dropped:0 overruns:0 frame:0
TX packets:12345 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:29375815 (29.3 MB) TX bytes:1447714 (1.4 MB)
Interrupt:18 Base address:0x2000
Solution 2:
/etc/network/interfaces is the right thing to use, but the GUI tools are fine as well.
Are you simply statically configuring the same IP that DHCP gave you? If you do that, the DHCP server will eventually lease the same IP out to another computer/device and you'll have an IP conflict. If you give your system an IP via any of those static methods, you need to give it an IP nothing else will use. Either outside the range of DHCP, or one that DHCP knows belongs only to your MAC address. If you configure a static assignment on the DHCP server for your MAC address, you shouldn't have to worry about any of this...