Linux Networking and Services with Examples

In this article, we are going to cover Linux Networking and Services with Examples, interview questions and answers to Linux Networking and Services.

What is Networking in Linux?

Networking in Linux is about setting up and managing connections between computers so they can share information and resources efficiently and securely over a network

Basic networking commands in Linux

  1. ifconfig – (Interface Configuration) used to display and manage network interfaces. It allows you to configure network interfaces, assign IP addresses, enable/disable interfaces, and gather useful information about the network setup.
ifconfig

output:

2. ip – Latest and updated versio of ifconfig commad

ip a ,  Ip address

output:

Linux Networking and Services with Examples 2

3.netstat –( network statistics) command-line tool in Linux and other UNIX-like systems) used to display network-related information It provides useful insights about your system’s network connections, routing tables, interface statistics, and more. It’s particularly valuable for network troubleshooting and monitoring

netstat

output:

4. ping-  Checks connectivity to another machine on the network or the internet. 

Example:

ping google.com

output:

Linux Networking and Services with Examples 4

5. nslookup– looks up DNS records to find the IP address of a websites.

Example :

nslookup google.com

output:

Linux Networking and Services with Examples 5

How to configure Network Interfaces in Linux

Configuring network interfaces in Linux allows you to set up and manage network connections, whether wired (Ethernet), wireless (Wi-Fi), or virtual. Network configuration in Linux typically involves editing configuration files or using command-line tools.

There are many processes/methods by which we can configure network services that are :

  1. Configure Network Interface Using DHCP (Dynamic IP)
  2. Configure Network Interface with Static IP
  3. Manually Configure the Interface with ifconfig or ip Command (Temporary Settings)
  4. Verifying Network Interface Configuration
  5. Restart Network Services
  6. Troubleshooting

3. Manually Configure the Interface with ifconfig in Linux

  1. Static IP Address

The static IP address should belong to the same subnet as your EC2 instance. Here’s how to find the subnet:

Check the current IP address and subnet: Run ifconfig or ip a. Look for the inet field of your interface (enX0 in this case). For example:

inet 172.31.15.163  netmask 255.255.240.0

This means your EC2 instance is in the 172.31.0.0/20 subnet. Any unused IP within this range can be used as a static IP.

2. Default Gateway

The default gateway is usually the first IP address of your subnet. For the example subnet 172.31.0.0/20, the default gateway is 172.31.0.1.

To confirm the default gateway: Run

ip route

You will see an output similar to:

default via 172.31.0.1 dev enX0

172.31.0.0/20 dev enX0 proto kernel scope link src 172.31.15.163

Here, default via 172.31.0.1 indicates the default gateway.

3. Updating ifconfig Command

Once you have identified an appropriate static IP address and gateway:

  • Assign the static IP:
sudo ifconfig enX0 172.31.15.200 netmask 255.255.240.0 up
  • Set the default gateway:
sudo route add default gw 172.31.0.1 enX0

4. Persistent Configuration

To make this change persistent across reboots, update your network configuration:

  • Edit the netplan configuration file, typically located at /etc/netplan/50-cloud-init.yaml or similar:
sudo nano /etc/netplan/50-cloud-init.yaml
  • Apply the changes:
sudo netplan apply

This ensures your EC2 instance uses the static IP and gateway even after reboots.

What are Processes and Services in Linux

In Linux, services and processes are essential concepts for understanding how the system operates and manages tasks. 

What is the process in Linux ?

A process is simply a running instance of a program on your computer. When you open a program (like a web browser or text editor), the operating system creates a process to run that program.

Example Imagine your computer as a kitchen, and each process is a chef cooking a specific dish. Each chef (process) is busy with its own task, like preparing soup, frying eggs, or baking a cake.

What is Service in Linux ?

A service (also called a daemon) is a background process that runs to perform specific functions for the system or for users. These services don’t typically interact with you directly, but they run continuously, providing important system functionality like network management, printing, or web serving.

 Example: Think of a service as a janitor who works in the background, cleaning up the kitchen (your computer) while the chefs (processes) cook. You may not always see the janitor, but they’re constantly working to keep everything in order.

How to Manage Services in Linux

systemctl is the primary command used for controlling and managing services (also called daemons) in modern Linux distributions that use systemd as the init system.

systemctl which is part of system d and used to manage services, processes, and the overall system state.

How to manage services with systemctl

1.Checking the Status of a Service : To start a service :

sudo systemctl status <service-name>

2.Starting a Service : To start the service :

sudo systemctl start <service-name>

3.Stopping a Service : To stop a running service :

 sudo systemctl stop <service_name>

4.Restarting a Service : to restart a service :

sudo systemctl restart <service_name>

5.Reloading a Service   : To reload the service’s configuration without restarting the entire service :

sudo systemctl reload <service_name>

6.Enabling a Service at Boot : To ensure a service starts automatically when the system boots :

sudo systemctl enable <service_name>

7.Disabling a Service at Boot : To prevent a service from starting automatically at boot :

sudo systemctl disable <service_name>

How to manage processes in Linux

Process management in Linux involves controlling and monitoring the execution of processes on a system

1.ps(Process Status): The ps command is used to display information about running processes on your system.

Show all processes:

ps aux 

Show detailed info:

ps -ef
Linux Networking and Services with Examples 6

2. top (Task Manager): Shows running processes in real time

top

  Press p: Sort processes by CPU usage.

 Press m: Sort processes by memory usage.

 Press q: Quit top

Linux Networking and Services with Examples 7

3. htop (improved top) : htop is a more advanced, user-friendly alternative to top. It provides an interactive, colorful interface that allows users to sort and filter processes more easily.

htop

4. kill (Terminate Processes) : Terminate processes by PID.

pkill <process_name>

5. nice: Start a process with a specified priority (niceness)

Start with lower priority:

nice -n 10 <command>

Change running process priority:

renice -n 10 -p <pid>
Linux Networking and Services with Examples 10

SSH and Secure communication in Linux

SSH is a protocol that allows 2 servers to communicate securely over the network also we can SSH protocol for below usage.

  • Secure Remote Login: Access remote systems securely.
  • File Transfer: Secure file copying via scp or sftp.
  • Command Execution: Run remote commands securely.

How to connect Linux Server using SSH

  1. To use SSH for remote connection, follow these steps Install an SSH Client

 Download and install PuTTY, a popular SSH client for Windows.

2. Generate SSH Key Pair (Optional) If you prefer key-based authentication instead of using a password, follow these steps:

 Open PowerShell and generate your SSH key.

ssh-keygen -t rsa -b 4096 -C "[email protected]"

3. Copy Public Key to Remote Server (if using SSH key)

Use this command (or manually copy the key):

ssh-copy-id user@remote_host

4. Connect to remote server:

Open PuTTY.

Enter the remote host’s IP or hostname in the Host Name (or IP address) field.

Select SSH as the connection type and click Open.

Log in with your username and password (or use your private key for key-based authentication)

5 . Close the SSH Session: To disconnect from the remote session, type exit

This setup will allow you to use SSH to connect to remote machines from a Windows environment.

How to Generate SSH key pair in Linux?

1.Generate SSH Key Pair: Create an SSH key pair (private and public) on your local machine

ssh-keygen -t rsa -b 4096

 2. Copy Public Key to Remote Server: Upload your public key to the remote server to enable key-based authentication.

ssh-copy-id username@remote_server_ip

3. Test SSH Key Login: Try logging into the remote server without entering a password. If successful, your SSH key is working.

ssh username@remote_server_ip

How to troubleshoot network configuration in Linux?

  1. Check Interface Status: Ensure the interface is up and has an IP address.
  2. Ping Localhost: Confirms the local network stack is working.
  3. Test External Connectivity: If successful, the issue is DNS-related.
  4. Check DNS: Verify your DNS is working.
  5. Verify Gateway: Ensure a default route exists.
  6. Check Firewall: Temporarily disable firewall to test.
  7. Restart Network: Restart the network server.
  8. Check Logs: Check logs for network-related errors
  9. Reconfigure IP (DHCP/Static): If using DHCP: Ensure your interface is set to receive an IP automatically or if static IP: Check the configuration in /etc/network/interfaces (for Debian-based systems) or /etc/netplan/*.yaml (for newer systems like Ubuntu).

Interview Questions and Answers on Linux Networking

1.What is the difference between a router and a switch?

  • Router: Connects different networks (e.g., between your local network and the internet), and routes traffic between them.
  • Switch: Operates within a single network (LAN) and forwards data packets between devices on the same network based on MAC addresses.

2.Explain the ifconfig and ip commands. What are the key differences between them?

  • ifconfig: Used to display or configure network interfaces (older tool).
  • ip: A more modern and powerful tool to show or manipulate routing, devices, interfaces, and tunnels. It is preferred over ifconfig in newer Linux distributions.

3.What is the netstat command used for?

  • netstat displays network connections, routing tables, interface statistics, and other network-related information. It is commonly used to check open ports, active connections, and network statistics.

4.How do you check the IP address of a Linux system?

  • You can use either ifconfig (older systems) or ip addr show (preferred for newer systems) to view the IP addresses of network interfaces.

5.  What are the different types of network interfaces available in Linux?

  • Ethernet interfaces (e.g., eth0, eth1)
  • Wi-Fi interfaces (e.g., wlan0)
  • Loopback interface (lo)
  • Virtual interfaces (e.g., tun0 for VPN, docker0 for Docker containers)

6. How does DHCP work, and how would you configure a Linux server as a DHCP server?

  • DHCP (Dynamic Host Configuration Protocol) automatically assigns IP addresses to devices on a network. To set up a DHCP server, install the isc-dhcp-server package and configure the /etc/dhcp/dhcpd.conf file.

7. What is the role of systemd in networking services? How can you enable or disable networking services using systemd?

  • systemd is the system and service manager in modern Linux distributions. You can enable or disable networking services using:

            systemctl enable networking

           systemctl disable networking

8.What is the purpose of the ping command?

  • ping is used to test the reachability of a host on a network by sending ICMP echo requests and measuring the round-trip time for responses.

9.Explain the function of the /etc/network/interfaces file in Debian-based systems.

  • This file defines the network interfaces configuration for Debian-based systems (e.g., Ubuntu). It specifies settings like IP addresses, gateways, and DNS servers.

10.  What is iptables, and how does it work?

  • iptables is a firewall tool that filters network traffic based on predefined rules. It operates at the kernel level and can be used to configure network security policies.

11. What are some common network troubleshooting tools in Linux?

  • ping, traceroute, netstat, ss, ip, nslookup, dig, tcpdump, nmap.

12.What is the difference between public and private IP addresses?

  • Public IPs are globally routable on the internet.
  • Private IPs are used within private networks and are not routable on the internet (e.g., 192.168.x.x, 10.x.x.x).

13. Explain the concept of bonding in Linux. How does it work and when would you use it?

  • Bonding allows you to combine multiple network interfaces into one logical interface for load balancing or redundancy. It’s useful for increasing bandwidth or providing failover protection.

14.What is SELinux, and how does it affect networking in Linux?

  • SELinux (Security-Enhanced Linux) enforces mandatory access control (MAC) policies. It can limit the network services a user or process can access based on predefined security contexts.

Conclusion:

In this article we have covered Linux Networking and Services with Examples.

Related Articles:

Package Management and Software Installation in Linux

Reference:

Learn the networking basics every sysadmin needs to know

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

2 thoughts on “Linux Networking and Services with Examples”

  1. Hello Shruthi Kothari
    I firstly appreciate for your great efforts to make this article,

    it’s a small correction to rewrite the meaning of the command.
    Checking the Status of a Service : To start a service: [(To show the service Status)]
    sudo systemctl status

    Thank you!!!

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap