Networking Basics: Commands ping
, ip addr
, ifconfig
1. Introduction to Network Concepts: A Bit of Theory
In today’s lecture, we’ll cover the basics of working with Linux networking tools. You’ll learn how to check the availability of network nodes, analyze the state and configuration of network interfaces, and work with network settings.
Networks are like the nervous system of the IT universe. If you’ve ever wondered how your browser opens a website page, here’s a quick answer: thanks to networking. Regardless of the role you’ll take on (developer, admin, or engineer), understanding networking basics is a key skill.
So what’s a network? It’s a bunch of computers connected together to share data. In the context of Linux, managing networks starts with understanding the basic concepts:
- IP Address: This is a unique identifier for a device on a network. Think of it like your computer’s mailing address.
- Subnet: A logical grouping of devices on a network. It’s like a neighborhood with houses that have their own addresses.
- Gateway: This is the "way out," through which devices in your local network can access the internet or connect to other subnets.
There are two types of IP addresses: IPv4 (for example, 192.168.1.1
) and IPv6 (for example, 2001:0db8:85a3:0000:0000:8a2e:0370:7334
). IPv4 is simpler, and that’s what we’ll focus on in this lecture. IPv6 is cool and a long-term standard, but we’ll get to that later.
2. The ping
Command: Checking Node Availability
What Does ping
Do?
ping
is a utility that checks whether another node on the network is reachable. It sends a small "hello note" to the target server (an ICMP request) and waits for a response. If the node responds, everything is good; if not, something might be off (or the server just decided to ignore you, how rude!).
How to Use ping
?
Let's try using the ping
command. Open a terminal and type the following command:
ping 8.8.8.8
This command will send requests to Google's public DNS server. You'll see something like this:
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=117 time=10.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=10.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=10.3 ms
Here's what those lines mean:
- icmp_seq: the number of the sent request.
- ttl: the "time-to-live" of the packet (how many network "hops" it can make).
- time: the time (in milliseconds) it took to send and receive a response.
To limit the number of requests sent, you can use the -c
flag:
ping -c 4 8.8.8.8
This command will send only 4 requests instead of an infinite stream.
Practice: Checking Connection to Local Host and Internet
Try using ping
with your router's IP address (it's usually something like 192.168.1.1
) and with 8.8.8.8
. This will help you understand if your local network is working and if there is access to the internet.
3. The ip addr
Command: Checking Network Interfaces
What’s a Network Interface?
A network interface is what your computer uses to "talk" to the network. It could be an interface for Ethernet
(wired connection), WLAN
(wireless connection), or virtual interfaces created for specific tasks.
The ip addr
command shows the current configuration of network interfaces. Let’s try it:
ip addr
The output will look something like this:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3
inet6 fe80::1a2b:3c4d:5e6f:f7g8/64 scope link
What we see here:
lo
— this is the loopback interface (localhost). Its address is always127.0.0.1
.enp0s3
— this is the name of your Ethernet adapter’s network interface.inet
— the IPv4 address of the interface.inet6
— the IPv6 address of the interface.
How to Temporarily Set an IP Address?
If you have superuser privileges (via sudo
), you can temporarily set an IP address:
sudo ip addr add 192.168.1.101/24 dev enp0s3
This IP address will only last until the next reboot.
4. The ifconfig
Command: old but still popular tool
Back in the day, ifconfig
was used to manage network interfaces. Now it's gradually being phased out (replaced by ip addr
), but you might still encounter it in older distributions.
Checking the status of interfaces
To see the status of interfaces using ifconfig
, run:
ifconfig
The output will look similar to the ip addr
command.
Enabling/disabling an interface
You can enable or disable an interface using:
sudo ifconfig enp0s3 down
sudo ifconfig enp0s3 up
Again, remember that ifconfig
doesn't work on all modern distributions. If the command isn't found, try installing the net-tools
package or just switch to ip addr
.
5. Example: Checking Availability and Network
Alright, let’s bring together all this knowledge in a quick hands-on example.
Check the availability of the local interface:
ping 127.0.0.1
Check the IP address of your network:
ip addr
Try setting up a temporary new IP address:
sudo ip addr add 192.168.1.102/24 dev enp0s3 ip addr show enp0s3
Check the availability of a public node:
ping -c 3 8.8.8.8
If you’re using an older distribution, try toggling the interface:
sudo ifconfig enp0s3 down sudo ifconfig enp0s3 up
Now you’re armed with some basic tools for network diagnostics in Linux. The ping
, ip addr
, and ifconfig
commands are your first steps into the world of network administration. And trust me, it only gets more exciting from here!
GO TO FULL VERSION