Parece que a DNAT para tráfego de loopback não é possível.
O tráfego de loopback pula as cadeias PREROUTING e OUTPUT.
RFC 5735 (página 3) diz que a rede 127.0.0.0/8 não pôde ser encaminhada para fora do próprio host:
127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher-level protocol to an address anywhere within this block loops back inside the host. This is ordinarily implemented using only 127.0.0.1/32 for loopback. As described in [RFC1122], Section 3.2.1.3, addresses within the entire 127.0.0.0/8 block do not legitimately appear on any network anywhere.
Além disso, o tráfego para loopback interface é considerado como Martian Packets :
these packets cannot actually originate as claimed, or be delivered
Solução alternativa:
Uma alternativa simples é usar um servidor inted no lado do servidor e seu recurso redirect .
Desta forma, você pode definir um serviço dentro de inetd e definir a porta onde este serviço irá escutar. Em seguida, defina a diretiva redirect para vincular essa porta a 127.0.0.1:port .
No meu exemplo abaixo, usarei xinetd (no Ubuntu 12.04 LTS) para ligar um servidor mysql em execução em 127.0.0.1:10000 :
Etapa 1: Instale o pacote: apt-get install xinetd
Passo 2: Edite o arquivo de configuração /etc/xinetd.conf
Adicione uma definição de serviço semelhante à abaixo:
service my_redirector
{
type = UNLISTED
disable = no
socket_type = stream
protocol = tcp
user = root
wait = no
port = 15000
redirect = 127.0.0.1 10000
log_type = FILE /tmp/somefile.log
}
Etapa 3: Reinicie o daemon xinetd : service xinetd restart
Passo 4: vamos verificar o ouvinte na porta 15000 :
# netstat -anp | grep 15000
tcp 0 0 0.0.0.0:15000 0.0.0.0:* LISTEN 4654/xinetd
Passo 5: Adicione as suas regras iptables :
iptables -A INPUT -i eth0 -p tcp -d 192.168.0.60 --dport 15000 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -s 192.168.0.60 --sport 15000 -j ACCEPT
Vamos testá-lo:
Para testar, eu configurei mysql para ouvir em 127.0.0.1:10000 . Tentarei acessá-lo por meio do xinetd service na porta 15000 .
Primeiro, no lado do servidor, certifico-me de que mysql server está apenas ouvindo 127.0.0.1:10000 :
# netstat -anp | grep :10000
tcp 0 0 127.0.0.1:10000 0.0.0.0:* LISTEN 4247/mysqld
Em seguida, no lado do cliente, vamos verificar se podemos nos conectar usando a porta 15000 :
# telnet 192.168.0.60 15000
Trying 192.168.0.60...
Connected to 192.168.0.60.
Escape character is '^]'.
_
5.5.35-0ubuntu0.12.04.2-logD46S}<P'.6cr4ITIQ<wcmysql_native_password
Parece que podemos! :)
Vamos tentar nos conectar ao mysql server:
# mysql -h 192.168.0.60 --port=15000 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 67
Server version: 5.5.35-0ubuntu0.12.04.2-log (Ubuntu)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Feito!