Posted on
Advanced

Creating a simple HTTP server with netcat and Bash

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

How to Create a Simple HTTP Server Using Netcat and Bash on Linux

Whether you're testing, developing, or simply sharing files over a network, setting up a quick HTTP server can be incredibly useful. While there are many tools available to serve files over HTTP, few can beat the simplicity and minimal dependency needs of Netcat and Bash. In this blog post, we'll walk you through creating a lightweight and straightforward HTTP server using these tools.

What You'll Need

  1. Linux environment - Any major Linux distribution will do (Ubuntu, Fedora, openSUSE, etc.).
  2. Netcat (nc) - The networking utility used for setting up the server.
  3. Bash - The shell scripting language we will use to handle requests.

Installation of Netcat

Before you set up your HTTP server, you need to ensure that Netcat is installed on your system. Netcat often comes pre-installed on many Linux distributions, but if it's not, you can easily install it using your distribution’s package manager.

  • Debian/Ubuntu:

    sudo apt update
    sudo apt install netcat
    
  • Fedora:

    sudo dnf install nc
    
  • openSUSE:

    sudo zypper install netcat
    

Creating Your HTTP Server

Now, let’s create a simple Bash script to handle HTTP requests with Netcat. Create a new file called simple_http_server.sh and open it with a text editor.

#!/bin/bash
# Simple HTTP server with Netcat

while true; do
  # Listen on port 8080 and capture incoming request into a variable
  REQUEST=$(echo -e "HTTP/1.1 200 OK\n\n $(date)" | nc -l -p 8080 -q 1)
  echo "Received request: $REQUEST"
done

Understanding the Script

  1. Infinite Loop - The while true loop ensures the server continues to listen for requests indefinitely.
  2. Netcat Command:
    • nc -l -p 8080: Listen on port 8080.
    • -q 1: Close the connection one second after the client has finished transmitting.
  3. Request Handling - The script currently just responds with HTTP 200 OK status and the current date. The request details are printed on the server for inspection.

Testing Your Server

  1. Make the script executable:

    chmod +x simple_http_server.sh
    
  2. Run the script:

    ./simple_http_server.sh
    
  3. Open a browser and navigate to http://<your_server_ip>:8080. You should see the current date displayed on the page and the request details in the terminal where your script is running.

Expanding Functionality

To serve actual files or create more complex interactions, you can extend the script functionality. For instance, you might want to parse the HTTP request and serve different files based on the URL requested. However, remember that Bash and Netcat are quite rudimentary for handling HTTP and might not be suitable for production environments or handling sensitive data.

Conclusion

Setting up an HTTP server with Bash and Netcat demonstrates the power and flexibility of Linux command-line tools. This setup is excellent for testing, quick file sharing, or learning more about how HTTP works at a basic level. As always, ensure you understand the security implications, especially if deploying on a public network or the internet.

By keeping it lightweight and minimalistic, you can harness the simplicity of Linux tools to serve your files and data efficiently and effectively.