ip command is replacing the
People often forget the process for assigning an IP address (static or dynamic) from the Linux command line. Most actually feel too intimidated by the process to commit it to memory, but it’s actually very simple.
Here’s how to get networking all the way up in a matter of seconds using just a few commands.
It’s sad for us UNIX/Linux nerds, but using
ifconfig is now the “old” way of doing things.
1. Set Your IP Address
ifconfig eth0 192.168.1.5 netmask 255.255.255.0 up
2. Set Your Default Gateway
route add default gw 192.168.1.1
3. Set Your DNS Server
Yes, 126.96.36.199 is a real DNS resolver by CloudFlare.
echo "nameserver 188.8.131.52" > /etc/resolv.conf
Assuming you have valid addresses for yourself and your gateway (and you have a clear path to the Interweb) you’re all set. Test by pinging someone.
If you’re using an older Linux system, that’s all you have to do. If you’re on a newer system, read on.
You should start learning
ip now, since it’s about to become common everywhere.
ifconfig is being phased out, it’s time to get used to the new system. By default, Ubuntu 18.04 doesn’t use
ifconfig anymore, and instead uses the new commands,
Show your IP using
ip addr show
Bring an interface up or down using
ip link set eth1 up
ip link set eth1 down
Showing your routing using
ip route show
Editing your networking info
For Ubuntu, here’s the replacement for editing
/etc/networking/* in the old system. The whole system now uses YAML configuration files under
/etc/netplan, and then the
netplan command applies those configurations to the system.
The * represents the file you find there in the directory.
network: ethernets: eno1: addresses: [192.168.1.13/24] gateway4: 192.168.1.1 dhcp4: true optional: true nameservers: addresses: [184.108.40.206,220.127.116.11] version: 2
And then to apply the configuration, you run:
So now—whether you’re running an older system or a new one—you should now have a static IP address!
And finally, here’s how to make the change permanent (static) in various environments.