Linux Networking Commands and Concepts

Networking Concepts

Networking in Linux involves managing and configuring network interfaces, routing, and services. Below are some key concepts:

Class of ip address

IP addresses are divided into classes based on their leading bits. The classes are:

Class IP Address Range
A 0 - 126
B 128 - 191
C 192 - 223
D 224 - 239
E 240 - 255
Note: Class D is used for multicast, and Class E is reserved for future use.
Class Subnet mask range
A 255.0.0.0
B 255.255.0.0
C 255.255.255.0

Class A, B, and C are used for unicast communication, while Class D is used for multicast communication.

Class E is reserved for experimental purposes.

Common Networking Commands

Check Network Interfaces

Use this command to view network interfaces and their IP addresses.

# ip addr
# ifconfig /all
# ip a
# ip add show
# ip link
    
Example Output:
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
2: eth0:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
        

Ping a Host

Use the ping command to test connectivity to another host.

ping google.com
Example Output:
PING google.com (142.250.190.78) 56(84) bytes of data.
64 bytes from 142.250.190.78: icmp_seq=1 ttl=115 time=12.3 ms
        

Display Routing Table

Use the route or ip route command to view the routing table.

ip route
Example Output:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
        

Manual Network Configuration

Network configuration can be done manually by editing network configuration files. For example:

/etc/network/interfaces
Example Configuration:
auto eth0
iface eth0 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
        

Nmcli basic command

NetworkManager provides the nmcli command-line tool for managing network connections.

1. View Network connection status

To view the status of network connections, use:

nmcli connection show

This command displays a list of all network connections and their status.

To view the status of a specific connection, use:

nmcli connection show my-connection

This command displays detailed information about the specified connection.

To view the status of all devices, use:

nmcli device status

This command displays the status of all network devices.

To view the status of a specific device, use:

nmcli device show eth0

This command displays detailed information about the specified device.

Example Output:
DEVICE  TYPE      STATE         CONNECTION
eth0    ethernet  connected     Wired connection 1
lo      loopback  unmanaged     --
        

2. Delete a adapter

To delete a network connection, use:

nmcli connection delete my-connection

This command deletes the specified connection.

To delete a network device, use:

nmcli device delete eth0

This command deletes the specified device.

3. Modify a network connection

To modify a network connection, use:

nmcli connection modify eth0 ipv4.address 192.168.0.15/24

This command modifies the specified connection to use the new IP address.

To modify the DNS settings of a connection, use:

nmcli connection modify eth0 +ipv4.dns 4.4.4.4

This command adds a new DNS server to the connection.

To modify the gateway of a connection, use:

nmcli connection modify eth0 gw4 192.168.1.1

This command modifies the gateway of the specified connection.

4. Add a new connection

nmcli connection add type ethernet ifname eth0 con-name my-connection autoconnect yes ip4 192.168.0.10/24 gw4 192.168.0.1 ipv4.dns 8.8.8.8 +ipv4.dns 4.2.2.2

This command adds a new Ethernet connection named "my-connection" with the specified IP address, gateway, and DNS servers.

Example Output:
Connection 'my-connection' (UUID: 12345678-1234-1234-1234-123456789abc) successfully added.
        

To connect to a Wi-Fi network:

nmcli device wifi connect "SSID" password "your_password"
Example Output:
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
        

To disconnect a device:

nmcli device disconnect eth0
Example Output:
Device 'eth0' successfully disconnected.
        

5. Restart Networking Service

After making changes to network configuration files, restart the networking service:

sudo systemctl restart networking

Conclusion

Linux provides powerful tools and commands for managing networks. Understanding these commands and concepts is essential for system administrators and network engineers.