Categories
How To

Turning a Linux Server into a NAT Gateway

Introduction:
In this blog post, we will explore how to transform a Linux server into a NAT (Network Address Translation) gateway with DNS (Domain Name System) forwarding. By configuring DHCPD (DHCP Daemon) properly, we can establish IP range management, IP reservation, and DNS forwarding to a popular DNS server like 8.8.8.8. This setup allows the Linux server to act as a central point of network connectivity, enabling other devices on the network to access the internet seamlessly.

Prerequisites:
Before we begin, make sure you have the following:

1. A Linux server with two network interfaces—one connected to the internet (WAN) and the other connected to the local network (LAN).
2. Administrative access to the Linux server.
3. Basic knowledge of Linux terminal commands.

Step 1: Install and Configure DHCPD:
1. Open the terminal on your Linux server.
2. Install `dhcpd` using the package manager appropriate for your Linux distribution. For example, on Ubuntu, you can use the following command:
sudo apt-get install dhcpd
3. Once `dhcpd` is installed, configure it by editing the `/etc/dhcpd.conf` file using your preferred text editor (e.g., `nano` or `vi`).
4. Inside the `dhcpd.conf` file, locate the `subnet` declaration and modify it to match your network configuration. Specify the IP range for the local network, subnet mask, gateway IP address (your Linux server’s LAN interface), and DNS server IP address (e.g., 8.8.8.8). Here’s an example:

subnet 192.168.1.0 netmask 255.255.255.0 {
   range 192.168.1.100 192.168.1.200;
   option routers 192.168.1.1;
   option domain-name-servers 8.8.8.8;
}

5. Save and exit the `dhcpd.conf` file.

Please note that `dhcpd` is the correct DHCP server software to use in this context.

Step 2: Configure IP Reservation:
To ensure specific devices receive consistent IP addresses, you can reserve IP addresses within DHCPD. Follow these steps:

1. Open the `/etc/dhcp/dhcpd.conf` file again.
2. Locate the `host` declaration and define the MAC address and IP address you want to reserve for a particular device. For example:

host device1 {
hardware ethernet 00:11:22:33:44:55;
fixed-address 192.168.1.50;
}

3. Save and exit the `dhcpd.conf` file.

Step 3: Enable IP Forwarding:
To enable IP forwarding on your Linux server, perform the following steps:

1. Open the `/etc/sysctl.conf` file using your preferred text editor.
2. Locate the line `net.ipv4.ip_forward` and uncomment it by removing the `#` at the beginning.
3. Save and exit the `sysctl.conf` file.
4. Apply the changes by running the following command in the terminal:
sudo sysctl -p

Step 4: Configure NAT (Network Address Translation):
To enable NAT on your Linux server, use the following steps:

1. Set up iptables rules to forward traffic between interfaces. Run the following commands:

sudo iptables -t nat -A POSTROUTING -o <WAN_INTERFACE> -j MASQUERADE
sudo iptables -A FORWARD -i <LAN_INTERFACE> -o <WAN_INTERFACE> -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i <WAN_INTERFACE> -o <LAN_INTERFACE> -j ACCEPT

Replace `<WAN_INTERFACE>` with the name of your WAN interface (e.g., eth0) and `<LAN_INTERFACE>` with the name of your LAN interface (e.g., eth1).

2. Save your iptables rules to persist across reboots. Depending on your Linux distribution, you may need to use different methods (e.g., iptables-persistent, iptables-save).

Step 5: Start and Enable DHCPD:
1. Start the DHCPD service using the following command:
sudo systemctl start isc-dhcp-server

2. Enable DHCPD to start automatically on system boot:
sudo systemctl enable isc-dhcp-server

Conclusion:
By following the steps outlined in this guide, you can successfully turn your Linux server into a NAT gateway with DNS forwarding. The DHCPD configuration allows you to manage IP ranges, reserve specific IP addresses for devices, and forward DNS queries to external DNS servers like 8.8.8.8. This setup empowers your Linux server to act as a central point of network connectivity, enabling seamless internet access for devices on your local network.

Remember to adapt the instructions to your specific Linux distribution and network configuration.

Categories
shellinfo tips

SORT – Sorting content of files

The ‘sort’ is a commonly used Linux command for sorting lines of text files.

Sorting a file in alphabetical order

sort filename.txt

This command will sort the contents of the ‘filename.txt’ file in alphabetical order and display it on the terminal.

Sorting a file in reverse alphabetical order

sort -r filename.txt

The ‘-r’ flag is used to reverse the order. So, this command will sort the contents of the ‘filename.txt’ file in reverse alphabetical order and display it on the terminal.

Sorting a file numerically

sort -n numbers.txt

If the file contains numbers, you can use the ‘-n’ flag to sort them numerically.

Sorting a file in reverse numerical order

sort -nr numbers.txt

This will sort the contents of the ‘numbers.txt’ file in reverse numerical order.

Sorting a file based on the second column

sort -k 2 filename.txt

If the file contains multiple columns or fields, you can sort based on a particular field by specifying the column number or character position using the ‘-k’ flag. In this example, the file will be sorted based on the second column.

Sorting a file based on a delimited field

sort -t , -k 2 filename.csv

If the file is comma-separated, you can specify the delimiter using the ‘-t’ flag. In this example, the file will be sorted based on the second column which is delimited by commas.

Sorting only unique lines from a file

sort -u filename.txt

You can use the ‘-u’ flag to sort only the unique lines from a file.

Sorting a file without considering case

sort -f filename.txt

The ‘-f’ flag can be used to sort a file without considering case.

Merging two sorted files

sort -m file1.txt file2.txt

You can merge two sorted files into one sorted file using the ‘-m’ flag.

Sorting files in a directory based on filename

ls | sort

You can sort the contents of a directory based on filename by piping the output of the ‘ls’ command to the ‘sort’ command.

Categories
shellinfo tips

AWK

The awk command in Linux is a powerful tool for processing text files, particularly those formatted as columns of data. It’s a scripting language that’s designed for text processing and is included by default in most Unix-like operating systems.

Here are some of the things you can do with awk:

  • Print Columns: The most basic use of awk is to print columns of data. For example, if you have a file called data.txt with the following content:
  • John 25 Engineer
    Jane 28 Doctor
  • You can print the first column (names) with the following command:
  • awk '{print $1}' data.txt

    Output:

    John
    Jane
  • Filter Rows: You can use awk to filter rows based on some condition. For example, to print only the rows where the second column (age) is greater than 26:
  • awk '$2 > 26' data.txt

    Output:

    Jane 28 Doctor
  • Perform Calculations: awk can perform calculations on the data. For example, to add 5 to the age of each person:
  • awk '{$2 = $2 + 5; print}' data.txt

    Output:

    John 30 Engineer
    Jane 33 Doctor
  • Text Substitution: You can use awk to substitute text. For example, to replace “Engineer” with “Software Engineer”:
  • awk '{gsub("Engineer","Software Engineer"); print}' data.txt

    Output:

    John 25 Software Engineer
    Jane 28 Doctor
  • Pattern Matching: awk can also perform pattern matching. For example, to print lines that contain “Doctor”:
  • awk '/Doctor/ {print}' data.txt

    Output:

    Jane 28 Doctor
  • Multiple Commands: You can use multiple commands in a single awk script. For example, to print the names of people who are not doctors:
  • awk '!/Doctor/ {print $1}' data.txt

    Output:

    John
  • Built-in Variables: awk has several built-in variables. For example, NF (number of fields) represents the number of columns. To print the last column of each row:
  • awk '{print $NF}' data.txt

    Output:

    Engineer
    Doctor
  • User-Defined Variables: You can define your own variables in awk. For example, to calculate the average age:
  • awk '{total += $2; count++} END {print total/count}' data.txt

    Output:

    26.5
  • Functions: awk supports several built-in functions. For example, length returns the length of a string. To print the length of each name:
  • awk '{print length($1)}' data.txt

    Output:

    4
    4
  • Passing Variables: You can pass variables to awk using the -v option. For example, to print rows where the age is greater than a certain value:
  • awk -v age=26 '$2 > age' data.txt

    Output:

    Jane 28 Doctor
  • File Processing: awk can process multiple files. For example, if you have another file data2.txt:
  • Alice 30 Lawyer
    Bob 35 Engineer

    You can print the names from both files:

    awk '{print $1}' data.txt data2.txt

    Output:

    John
    Jane
    Alice
    Bob
  • Complex Conditions: awk supports complex conditions. For example, to print rows where the name starts with ‘J’ and the age is less than 30:
  • awk '/^J/ && $2 < 30' data.txt

    Output:

    John 25 Engineer
    Jane 28 Doctor

    These examples should give you a good idea of the power and flexibility of awk. It’s a very versatile tool for text processing in Linux.

    Categories
    shellinfo tips

    MDADM – Managing RAID in Linux

    mdadm is a Linux utility used to manage and monitor software RAID devices. The name is derived from the term “multiple device administrator”. It is a powerful tool that can be used for a variety of tasks related to RAID arrays, such as creating, managing, and monitoring them.

    Here are some examples of what you can do with mdadm:

    Create a RAID array: You can use mdadm to create a new RAID array. Here’s an example of how you might create a RAID 5 array with three devices:

    mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1

    In this example, /dev/md0 is the name of the new RAID device, –level=5 specifies that it should be a RAID 5 array, and –raid-devices=3 indicates that there should be three devices in the array. The devices /dev/sda1, /dev/sdb1, and /dev/sdc1 are the partitions that will be included in the array.

    Monitor a RAID array: mdadm can also be used to monitor the status of a RAID array. For example, you can use the following command to check the status of the /dev/md0 array:

    mdadm --detail /dev/md0

    This command will display detailed information about the array, such as its level, size, and the status of each device in the array.

    Add a new device to an existing RAID array: If you want to add a new device to an existing RAID array, you can use the –add option. For example, to add a new device /dev/sdd1 to the /dev/md0 array, you would use the following command:

    mdadm --manage /dev/md0 --add /dev/sdd1

    Remove a device from a RAID array: Similarly, you can remove a device from an array using the –remove option. For example, to remove the device /dev/sdd1 from the /dev/md0 array, you would use the following command:

    mdadm --manage /dev/md0 --remove /dev/sdd1

    Stop and delete a RAID array: If you no longer need a RAID array, you can stop it and then delete it using mdadm. Here’s how you might do that:

    mdadm --stop /dev/md0
    mdadm --remove /dev/md0

    These are just a few examples of what you can do with mdadm. It’s a very powerful tool with many more options and capabilities. Always make sure to check the man page (man mdadm) or other documentation for more information and to understand the implications of any command before you run it.