Configuring SSH Access
Begin by adding an incoming rule on the development server that permits SSH connections solely from the designated client. Execute the following command:-A INPUT: Appends a rule to the INPUT chain.-p tcp: Specifies the TCP protocol.-s 172.16.238.187: Restricts the rule to connections coming from the client laptop.--dport 22: Indicates that the rule applies to the SSH port (22).-j ACCEPT: Accepts the connection when all conditions are met.
Blocking Unauthorized SSH Attempts
By default, if another client tries to access SSH without a specific allow rule, the connection would follow the default policy (typically accepting all connections). Since our requirement is to restrict SSH access to the specified client, add a rule that drops SSH traffic from all other sources:Configuring Outbound Traffic
On the development application server, additional configurations are needed to manage outbound connections:- Allow connections to the DB server on port 5432.
- Permit connections to the software repository server on port 80.
- Drop general HTTP (port 80) and HTTPS (port 443) traffic to the Internet.
- Explicitly allow HTTP access on port 80 from the client laptop.
Allowing Specific HTTPS Traffic
If you need to access a particular site using HTTPS from the devapp-01 server (e.g., to 172.16.238.100), the general DROP rule for port 443 would normally block this connection. To allow HTTPS access to this specific destination, insert an ACCEPT rule at the top of the OUTPUT chain:-I option inserts the rule at the top of the chain, making sure it takes precedence over the subsequent DROP rules.
Deleting a Rule
If you need to remove an iptables rule by its position (for example, deleting the rule at position 5 from the OUTPUT chain), use the-D option:
Securing the Database Server
To ensure that only the development application server can access the PostgreSQL service on the database server (port 5432), configure the following rules on the DB server:Summary of DB Server Security
-
Application Server Output Rule:
The development application server actively allows connections to the DB server on port 5432: -
Database Server Accept Rule:
An input rule on the DB server accepts connections originating from the application server on port 5432. -
Database Server Drop Rule:
A subsequent rule then drops any other connection attempts on port 5432.
Handling Return Traffic
When the dev application server initiates a database connection, an ephemeral source port (for example, 44060) is assigned. Once the connection is established, the database server sends the return traffic back to this ephemeral port. Since the overall inbound traffic is accepted (except for explicitly dropped SSH connections), there is no need for additional rules to handle the returning traffic. You can confirm the connection with:Linux typically assigns ephemeral ports in the range of 32768 to 60999. This dynamic assignment facilitates proper handling of return traffic without manual configuration.
Now it’s your turn to create custom iptables rules based on these examples. Experiment and adjust the rules to suit your specific network security requirements.
For further reading on iptables and network security best practices, consider checking out: