# Manage and Analyze Network

Managing networks and analyzing them are also very important parts of Linux. Let's dive mainly into ways of analyzing networks, but we will also cover some aspects of network management.

## `Ifconfig` Command

**`ifconfig`** (short for **Interface Configuration**) is a command-line tool in Unix/Linux used to configure, manage, and diagnose network interfaces. It provides information about network interfaces, such as IP addresses, MAC addresses, netmasks, and network status (up/down). It is commonly used to:

1. **View Network Interface Details**: Check IP addresses, MAC addresses, and network stats.
2. **Configure Network Interfaces**: Set IP addresses, enable/disable interfaces, or modify network settings.
3. **Troubleshoot Networks**: Diagnose connectivity issues or verify interface status.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::28d4:c11a:a8f7:4180  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:6e:13:6e  txqueuelen 1000  (Ethernet)
        RX packets 3  bytes 1770 (1.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 65  bytes 6434 (6.2 KiB)
        TX errors 0  dropped 5 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 480 (480.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 480 (480.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
```

The output from `ifconfig` describes two network interfaces: **`eth0`** and **`lo`**. The `eth0` interface is the primary Ethernet interface used for external network communication. It is currently active (`UP`), with an IPv4 address of `10.0.2.15`, a netmask of `255.255.255.0`, and a broadcast address of `10.0.2.255`. It also has an IPv6 address (`fe80::28d4:c11a:a8f7:4180`) and a MAC address (`08:00:27:6e:13:6e`). The interface has transmitted 65 packets and received 3 packets, with no errors or collisions. On the other hand, the `lo` interface is the loopback interface, used for internal communication within the system. It has an IPv4 address of `127.0.0.1` and an IPv6 address of `::1`. It has transmitted and received 8 packets each, with no errors. Both interfaces are active and functioning properly, with `eth0` handling external traffic and `lo` managing internal system communication.

Essentially, **`10.0.2.15`** is a **private IP address** assigned to a device within a **local network**. Private IP addresses, such as those in the `10.x.x.x` range, are reserved for internal use and are not routable on the public internet. This address allows the device to communicate with other devices within the same local network while remaining isolated from external networks.

## Checking Wireless Network Devices with `iwconfig`&#x20;

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ iwconfig
lo        no wireless extensions.

eth0      no wireless extensions.
```

1. **`lo`**: The loopback interface, which is used for internal communication, has **no wireless extensions** because it is not a wireless interface.
2. **`eth0`**: The Ethernet interface also has **no wireless extensions**, indicating it is a wired network interface and does not support wireless functionality.

## Checking Routing Tables

A **routing table** is a set of rules used by a device to determine where to send network traffic. It contains information about network destinations, gateways, and interfaces, helping the device decide the best path for data packets.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ route -n                                                                                                                 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0

┌──(kali㉿kali)-[~/Desktop]
└─$ ip route show
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 100 
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15 metric 100 
```

The output displays the **routing table** of the system, which defines how network traffic is directed. The `route -n` command shows two entries: the first (`0.0.0.0`) indicates that the **default gateway** is `10.0.2.2`, meaning all non-local traffic is routed through this gateway. The second entry (`10.0.2.0`) specifies that traffic for the `10.0.2.0/24` network is handled directly via the `eth0` interface. Similarly, the `ip route show` command provides the same information in a different format, confirming the default gateway (`10.0.2.2`) and the local network route (`10.0.2.0/24`) through `eth0`. Both outputs indicate that `eth0` is the active interface for communication, with the system's IP address being `10.0.2.15`.

## **Testing Network Connectivity with Ping**

The **`ping` command** is a network diagnostic tool used to test connectivity between devices by sending **ICMP echo requests** and receiving replies. It helps verify if a remote host or IP address is reachable, measures round-trip time (latency), and identifies packet loss, making it essential for troubleshooting network issues. Additionally, it can test DNS resolution by pinging domain names.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ ping google.com                                                                                                          
PING google.com (142.250.192.46) 56(84) bytes of data.
64 bytes from bom12s15-in-f14.1e100.net (142.250.192.46): icmp_seq=1 ttl=55 time=57.6 ms
64 bytes from bom12s15-in-f14.1e100.net (142.250.192.46): icmp_seq=2 ttl=55 time=57.0 ms
```

`ping google.com` shows successful ICMP echo requests to Google's server at `142.250.192.46`. Each response includes the packet size (`64 bytes`), sequence number (`icmp_seq`), Time to Live (`ttl=55`), and round-trip time (`time=57.6 ms`), which measures latency. This confirms a stable and responsive connection to Google's server, with low latency and no packet loss.

## Changing Your Network Information

Changing your IP address and other network configurations is a crucial skill, enabling you to access various networks while appearing as a legitimate device. For example, during penetration testing, you can modify your IP to simulate different devices on a network, helping you test security measures without revealing your true identity. Similarly, in scenarios like bypassing geo-restrictions, you can change your IP to appear as if you're accessing the network from a different region. In Linux, this is easily achieved using the `ifconfig` command, making it a powerful tool for network manipulation and anonymity.

### Changing IP Address

To configure the **IP address**, **subnet mask**, and **broadcast address** for the `eth0` interface in Linux, you can use the `ifconfig` command. For example, to set the IP address to `10.0.0.5`, the subnet mask to `255.255.255.0`, and the broadcast address to `10.0.0.255`, you would run the command:

```jsx
┌──(kali㉿kali)-[~/Desktop]
└─$ sudo ifconfig eth0 10.0.0.5 netmask 255.255.255.0 broadcast 10.0.0.255                                              
[sudo] password for kali: 

┌──(kali㉿kali)-[~/Desktop]
└─$ ifconfig                                                                                                            
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.5  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::28d4:c11a:a8f7:4180  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:6e:13:6e  txqueuelen 1000  (Ethernet)
        RX packets 14  bytes 2793 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 78  bytes 7476 (7.3 KiB)
        TX errors 0  dropped 5 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 480 (480.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 480 (480.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

This command assigns the specified IP address, defines the subnet mask for the network, and sets the broadcast address for communication. within the local network. After executing the command, you can verify the changes by running `ifconfig eth0`, which will display the updated network configuration for the `eth0` interface. This method is useful for manually configuring network settings during troubleshooting or testing. Let me know if you need further clarification!

### **Changing MAC Address**

A MAC address is a unique identifier assigned to network devices, and spoofing it can help you avoid tracking or access networks that filter devices based on MAC addresses. For example, if a network blocks your device, you can change your MAC address to mimic an allowed device. In Linux, you can use the `macchanger` tool or the `ip` command, such as:

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ sudo ifconfig eth0 down

┌──(kali㉿kali)-[~/Desktop]
└─$ sudo ifconfig eth0 hw ether 00:11:22:33:44:55                                                                       

┌──(kali㉿kali)-[~/Desktop]
└─$ sudo ifconfig eth0 up                                                                                               

┌──(kali㉿kali)-[~/Desktop]
└─$ ifconfig                                                                                                            
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::28d4:c11a:a8f7:4180  prefixlen 64  scopeid 0x20<link>
        ether 00:11:22:33:44:55  txqueuelen 1000  (Ethernet)
        RX packets 14  bytes 2793 (2.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 88  bytes 9036 (8.8 KiB)
        TX errors 0  dropped 7 overruns 0  carrier 0  collisions 0
```

### Assigning New IP Addresses from the DHCP Server

To assign a new IP address from a **DHCP server** in Linux, you can use the `dhclient` command. First, release the current IP address by running `sudo dhclient -r eth0`, which disconnects the interface from the DHCP server. Next, request a new IP address by executing `sudo dhclient eth0`, which contacts the DHCP server to obtain a fresh IP configuration. For example, to renew the IP for the `eth0` interface, you would run:

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ sudo dhclient -r eth0

┌──(kali㉿kali)-[~/Desktop]
└─$ sudo dhclient eth0
```

### Manipulating the Domain Name System

DNS (Domain Name System) can be dangerous because it translates human-readable domain names (e.g., `google.com`) into IP addresses, making it a critical part of internet communication. If hackers compromise or modify a DNS server, they can redirect users to malicious websites without their knowledge, a technique called **DNS spoofing** or **DNS poisoning**. For example, when you type `example.com`, a hacked DNS server could redirect you to a fake website designed to steal sensitive information like passwords or credit card details. This can lead to phishing attacks, malware infections, or data breaches. Securing DNS servers and using encrypted DNS protocols (like DNS over HTTPS) are essential to prevent such attacks.

#### Dig

The **`dig` command** (Domain Information Groper) is a versatile Linux tool used to query DNS (Domain Name System) servers and retrieve detailed information about domain names, IP addresses, and DNS records. It allows users to look up specific DNS record types, such as **A records** (IPv4 addresses), **AAAA records** (IPv6 addresses), **MX records** (mail servers), **NS records** (name servers), and **CNAME records** (domain aliases). For example, running `dig google.com` provides comprehensive details about Google's DNS configuration. The command is particularly useful for troubleshooting DNS issues, verifying domain configurations, and analyzing DNS responses. Its flexibility and detailed output make it a preferred tool for network administrators and security professionals. Let me know if you need further clarification!

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ dig ncateam.xyz ns
---snip---
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;ncateam.xyz.                   IN      NS

;; ANSWER SECTION:
ncateam.xyz.            14400   IN      NS    coby.ns.cloudflare.com.
ncateam.xyz.            14400   IN      NS    wally.ns.cloudflare.com.
---snip---
```

#### **Changing Your DNS Server**

Changing the DNS server in Linux is often done to enhance **privacy**, **speed**, or **security**. For instance, you might switch to a public DNS service like Google DNS (`8.8.8.8`) or Cloudflare DNS (`1.1.1.1`) for faster and more secure internet access. To change the DNS server, you can edit the **`/etc/resolv.conf`** file and add or modify the `nameserver` line with the desired DNS server address. For a persistent change, you can use the `NetworkManager` tool.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ sudo nano /etc/resolv.conf
//..SNIP..//

# Generated by NetworkManager
nameserver 192.168.1.1
```

The line **`nameserver 192.168.1.1`** in a Linux configuration file (like `/etc/resolv.conf`) specifies that the system should use the DNS server at the IP address `192.168.1.1` for domain name resolution. This is typically the IP address of a **local router** or a **custom DNS server** within a private network. By setting this, your system will send all DNS queries to `192.168.1.1`, which then resolves domain names to IP addresses. This is useful in environments where the local DNS server provides specific domain resolutions or filters content. For example, in a home network, the router at `192.168.1.1` often acts as the default DNS server.

**Google Public DNS** is a free, global Domain Name System (DNS) service provided by Google, offering fast and secure domain name resolution. Its primary DNS servers are `8.8.8.8` and `8.8.4.4`.&#x20;

To add Google Public DNS to our Linux just add following 2 line in `/etc/resolv.conf` file.

```
nameserver 8.8.8.8
nameserver 8.8.4.4
```

### **Mapping Your Own IP Addresses**

The **`/etc/hosts`** file in Linux allows you to manually map IP addresses to hostnames, enabling custom domain resolutions on your system. This is particularly useful for development, testing, or blocking specific websites. For instance, if you're developing a website locally, you can map a domain like `mywebsite.local` to your local machine's IP address (`127.0.0.1`) by adding the line `127.0.0.1 mywebsite.local` to the `/etc/hosts` file. This ensures that when you type `mywebsite.local` in your browser, it resolves to your local server instead of querying an external DNS. Similarly, you can block access to a website like `adsite.com` by mapping it to `0.0.0.0`, effectively redirecting it to an invalid address. These mappings take effect immediately and provide a simple way to control domain resolution without relying on external DNS servers.

To help you understand this better, let me start the Apache web service using the following command, and then we will see how we can map it.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ service apache2 start

┌──(kali㉿kali)-[~/Desktop]
└─$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                                                           
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255                                                  
  --snip--
```

The command `service apache2 start` is used to start the Apache2 web server on a Linux system, such as Kali Linux. The `service` command is a utility for managing system services, and `apache2` refers to the Apache HTTP server, a widely used open-source web server. By adding the `start` argument, the command initializes the Apache2 service, allowing it to serve web pages and handle HTTP requests. This is particularly useful for hosting websites locally, testing web applications, or setting up a development environment. After executing the command, you can verify that Apache2 is running by visiting `http://10.0.2.15/` in your web browser.

<figure><img src="/files/hi3Q9NEMBzUSCX4lNN3P" alt=""><figcaption></figcaption></figure>

```bash
┌──(kali㉿kali)-[~]
└─$ sudo cat /etc/hosts                                                           
127.0.0.1       localhost
127.0.1.1       kali
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters
10.0.2.15       nca.com
```

If you notice above, I added `10.0.2.15 nca.com` at the last line, which means I mapped `nca.com` to the IP address `10.0.2.15` in my `/etc/hosts` file.

Now, if you visit <http://nca.com/> in your browser, it will first resolve `nca.com` to the IP address `10.0.2.15` from the `/etc/hosts` file. You can think of that file as a <mark style="color:yellow;">**local DNS server**</mark> as well.

<figure><img src="/files/li13t4xtBkZDKumlqRgP" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.ncateam.xyz/fundamentals/linux/manage-and-analyze-network.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
