Networking is a critical aspect of any Linux system, as it allows the system to communicate with other devices over a network. This page will cover the basics of configuring network settings, managing interfaces, and using key networking tools in Linux.
- Networking Components:
- IP Address: Identifies a device on a network.
- Subnet Mask: Defines the network segment.
- Gateway: The device that routes traffic from a local network to other networks.
- DNS (Domain Name System): Resolves domain names to IP addresses.
- Command:
ifconfig
- Displays the current network interfaces and their configurations.
- Example Output:
eth0 Link encap:Ethernet HWaddr 00:1a:2b:3c:4d:5e inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0 ...
- Shows the configuration of the
eth0
interface.
- Shows the configuration of the
- Command:
ip addr show
- Displays detailed information about all network interfaces.
- Example:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0 ...
- Assign an IP Address:
sudo ifconfig eth0 192.168.1.20 netmask 255.255.255.0
- Assigns the IP address
192.168.1.20
with a subnet mask of255.255.255.0
toeth0
.
- Assigns the IP address
- Assign an IP Address:
sudo ip addr add 192.168.1.20/24 dev eth0
- Assigns the IP address
192.168.1.20/24
toeth0
.
- Assigns the IP address
- Debian/Ubuntu: Edit the
/etc/network/interfaces
file.auto eth0 iface eth0 inet static address 192.168.1.20 netmask 255.255.255.0 gateway 192.168.1.1
- RHEL/CentOS: Edit the appropriate file in
/etc/sysconfig/network-scripts/
.DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.1.20 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ONBOOT=yes
- Debian/Ubuntu:
sudo systemctl restart networking
- RHEL/CentOS:
sudo systemctl restart network
-
Temporary Configuration: Edit
/etc/resolv.conf
.nameserver 8.8.8.8 nameserver 8.8.4.4
- Adds Google DNS servers.
-
Persistent Configuration:
- Debian/Ubuntu: Edit
/etc/network/interfaces
or/etc/systemd/resolved.conf
. - RHEL/CentOS: Edit
/etc/sysconfig/network-scripts/ifcfg-eth0
.
- Debian/Ubuntu: Edit
- Command:
nslookup example.com
- Checks the DNS resolution of
example.com
.
- Checks the DNS resolution of
- View Current Hostname:
hostname
- Set a New Hostname:
sudo hostnamectl set-hostname newhostname
- Command:
sudo nano /etc/hosts
- Maps IP addresses to hostnames for local name resolution.
- Command:
sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
- Routes traffic destined for the
192.168.2.0/24
network through192.168.1.1
usingeth0
.
- Routes traffic destined for the
- Debian/Ubuntu: Add the route to
/etc/network/interfaces
.up ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
- RHEL/CentOS: Edit
/etc/sysconfig/network-scripts/route-eth0
.192.168.2.0/24 via 192.168.1.1 dev eth0
- Ping Command:
ping -c 4 8.8.8.8
- Sends 4 ICMP packets to
8.8.8.8
to test connectivity.
- Sends 4 ICMP packets to
- Command:
ip route show
- Displays the current routing table.
- Command:
ip link show
- Displays the status of network interfaces.
Configuring network settings in Linux is a fundamental skill for managing systems in any environment. This guide provides a basic overview of key networking tasks, from viewing and configuring interfaces to managing DNS settings and static routes. Mastering these skills will enable you to ensure your Linux systems are properly connected and communicating over the network.
Next: Security Best Practices
Previous: SSH: Secure Shell