Cortafuegos Iptables
Recientemente estaba configurando un servidor web en centos con nginx y php. La instalación de nginx estuvo bien, pero el puerto http del sistema no era accesible desde el exterior.
Esto se debe a que, de forma predeterminada, centOS tiene en vigor algunas reglas de firewall de iptables. Solo se podía acceder al puerto ssh (22) y funcionaba el shell remoto. Por lo tanto, es necesario abrir el puerto 80 para que funcione un servidor web como nginx.
IPtables es el cortafuegos en Linux que se puede configurar para aceptar o rechazar el tráfico de red en función de varios tipos de conjuntos de reglas a nivel de paquete. Por lo tanto, es necesario configurar este firewall para habilitar las conexiones en los puertos de red.
Verificar las reglas de Iptables
Hay 2 formas de configurar iptables para abrir el puerto 80. La primera es usando el comando iptables y la segunda es creando un archivo de configuración. Primero verifique las reglas de iptables vigentes. El comando es bastante simple. Aquí hay una salida de muestra.
[[email protected] ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination [[email protected] ~]#
Como se puede ver en la salida, hay una línea de RECHAZO en la cadena de ENTRADA al final que dice, rechazar todo. Sin embargo, la línea anterior permite aceptar conexiones ssh para que ssh funcione. Se puede ver una lista un poco más detallada y numérica usando las opciones v y n junto con la opción L
[[email protected] ~]# iptables --line -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 273 22516 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 271 36456 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 172 packets, 24494 bytes) num pkts bytes target prot opt in out source destination [[email protected] ~]#
Abrir el puerto 80 en Iptables
Para aceptar conexiones http, debemos agregar una regla en la línea número 5 y presionar la línea REJECT a continuación. Aquí está el comando para hacerlo.
# iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
El comando anterior agregará una regla en la línea 5 que indica que el firewall debe aceptar conexiones entrantes en el puerto 80. Verifique las reglas de iptables nuevamente.
[[email protected] ~]# iptables --line -vnL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 291 23868 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 0 0 ACCEPT tcp -- eth0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 state NEW,ESTABLISHED 6 286 38524 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 4 packets, 608 bytes) num pkts bytes target prot opt in out source destination [[email protected] ~]#
Ahora tenemos la nueva regla del puerto tcp 80 en la línea n.° 5, por lo que ahora se puede acceder al puerto http desde la red externa.
Guardar las reglas de iptables
Con las nuevas reglas, el puerto 80 ahora está abierto; sin embargo, este cambio es temporal e iptables volvería a las reglas anteriores si se reinicia el servidor.
Para hacerlo permanente, ejecute el comando de guardado de iptables.
[[email protected] ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
Las nuevas reglas se guardan en el archivo /etc/sysconfig/iptables .
Así es como se ve el archivo:
# Generated by iptables-save v1.4.7 on Fri Oct 25 10:33:46 2013 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [39:6956] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -i eth0 -p tcp -m tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Fri Oct 25 10:33:46 2013
Ahora el cambio es permanente.
Alternativamente, puede editar directamente el archivo de configuración de iptables y reiniciar iptables y el mismo cambio tendrá efecto.
[[email protected] ~]# service iptables restart iptables: Flushing firewall rules: [ OK ] iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Unloading modules: [ OK ] iptables: Applying firewall rules: [ OK ] [[email protected] ~]#
Conclusión
Ese fue un ejemplo rápido de cómo abrir un determinado puerto en iptables para hacerlo accesible. Para obtener más información sobre iptables, consulte las páginas del manual ejecutando el comando "man iptables" en su terminal, o compruébelo en línea aquí:
https://linux.die.net/man/8/iptables