Allow traffic to/from specific IP with iptables

i want to allow all traffic to specific ip, using iptables.

tryed by adding lines:

/sbin/iptables -A INPUT -p tcp -s XXX.XXX.XXX.XXX -j ACCEPT
/sbin/iptables -A OUTPUT -p tcp -s  XXX.XXX.XXX.XXX -j ACCEPT

... but still can not acess to remote ip (XXX.XXX.XXX.XXX).

any advice?

tnx in adv!

EDIT: with your advices i have changed iptables. still i can not connect to remote server. iptables status looks like this:

[root@myserver ~]# /etc/init.d/iptables status
Table: nat
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Table: mangle
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         

Table: filter
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
3    DROP       all  --  0.0.0.0/0            0.0.0.0/0           state INVALID 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpts:1024:65535 
6    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8443 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3718 
8    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:88 
9    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:88 
10   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
11   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8080 
12   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:873 
13   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:873 
14   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8880 
15   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 
16   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:443 
17   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
18   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
19   ACCEPT     tcp  --  XXX.XXX.XXX.XXX      0.0.0.0/0           
20   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
21   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:587 
22   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:25 
23   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:465 
24   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:110 
25   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:995 
26   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:143 
27   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:993 
28   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:106 
29   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3306 
30   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5432 
31   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:9008 
32   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:9080 
33   DROP       udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:137 
34   DROP       udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:138 
35   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:139 
36   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:445 
37   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:1194 
38   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:53 
39   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:53 
40   ACCEPT     icmp --  207.250.234.40       0.0.0.0/0           icmp type 8 code 0 
41   DROP       icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 8 code 0 
42   DROP       all  --  0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
3    DROP       all  --  0.0.0.0/0            0.0.0.0/0           state INVALID 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8443 
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3718 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:88 
8    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:88 
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
10   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8080 
11   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:873 
12   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:873 
13   DROP       all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
3    DROP       all  --  0.0.0.0/0            0.0.0.0/0           state INVALID 
4    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
5    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpts:1024:65535 
6    DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8443 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3718 
8    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
10   ACCEPT     tcp  --  0.0.0.0/0            XXX.XXX.XXX.XXX      
11   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:88 
12   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:88 
13   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
14   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8080 
15   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:873 
16   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:873 
17   ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

[root@myserver ~]# 

Solution 1:

I think you need change one of your -s flags to a -d flag. If your XXX.XXX.XXX.XXX address is outside the firewall, it should be

/sbin/iptables -A INPUT -p tcp -s XXX.XXX.XXX.XXX -j ACCEPT
/sbin/iptables -A OUTPUT -p tcp -d  XXX.XXX.XXX.XXX -j ACCEPT

otherwise, the other way around

EDIT:

AND refresh iptables:

Depending on your configuration:

/etc/init.d/iptables restart  

/etc/init.d/networking restart  

/etc/init.d/firewall restart

Solution 2:

-s indicates the source. For OUTPUT you're going to want it as the destination (-d).

Solution 3:

-A appends. This adds the rule in the end of the rules list, so incoming connection could be dropped by a rule higher in the list.

To make sure that all connections from or to an IP address are accepted, change -A to -I which inserts the rule at the top of the list:

iptables -I INPUT -p tcp -s XXX.XXX.XXX.XXX -j ACCEPT
iptables -I OUTPUT -p tcp -d  XXX.XXX.XXX.XXX -j ACCEPT`