How to set up a port in a Linux server can only one IP access

Source: Internet
Author: User

How does the Linux firewall iptable to allow only one IP to access 80 ports, allowing only specific IPs to access a port? Refer to the following command to allow only 46.166.150.22 access to port 80 on this machine. If you want to set another IP or port, change it.
Iptables-i input-p TCP--dport 80-j DROP
Iptables-i input-s 46.166.150.22-p TCP--dport 80-j ACCEPT
After the root user executes the above 2 lines command, restart Iptables, service iptables restart
To see if iptables is in effect:
[[Email protected]]# iptables-l
Chain INPUT (Policy ACCEPT)
Target Prot opt source destination
ACCEPT TCP--46.166.150.22 anywhere TCP Dpt:http
DROP TCP--anywhere anywhere TCP dpt:http

Chain FORWARD (Policy ACCEPT)
Target Prot opt source destination

Chain OUTPUT (Policy ACCEPT)
Target Prot opt source destination
The above command is for the entire server (all IP) prohibit port 80, if you just need to prohibit the server on the 80 port of an IP address, what to do?
The following command is to allow only IP access from 174.140.3.190 to the 216.99.1.216 80 port on the server
Iptables-a forward-s 174.140.3.190-d 216.99.1.216-p tcp-m tcp--dport 80-j ACCEPT
Iptables-a forward-d 216.99.1.216-p tcp-m tcp--dport 80-j DROP
If you are unfamiliar with the SSH command for Linux, you can set it up in the Webmin/virtualmin panel to achieve the same effect. Reference: Webmin panel How to set allow specific IP access to port 80, prohibit 80 port

More iptables reference commands are as follows:
1. Back Up Iptables first

# cp/etc/sysconfig/iptables/var/tmp

Requires 80 ports, specify IP and LAN

The following three lines mean:

Shut down all 80 ports first.

Turn on 80 ports on the 192.168.1.0/24 end of the IP segment

Turn on IP segment 211.123.16.123/24 IP segment 80 port

# iptables-i input-p TCP--dport 80-j DROP
# iptables-i input-s 192.168.1.0/24-p tcp--dport 80-j ACCEPT
# iptables-i input-s 211.123.16.123/24-p tcp--dport 80-j ACCEPT

These are temporary settings.

2. Then save Iptables

# Service Iptables Save

3. Restart the firewall

#service iptables Restart

How to set up a port in a Linux server can only one IP access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.