El comando Netstat muestra diversa información relacionada con la red, como conexiones de red, tablas de enrutamiento, estadísticas de interfaz, conexiones de enmascaramiento y multidifusión, etc.
En este artículo, revisaremos 10 ejemplos prácticos de comandos netstat de Unix.
1. Listar todos los puertos (puertos de escucha y no de escucha)
Para poder listar todos los puertos usando netstat –a, basta con ejecutar el siguiente comando.
1 2 3 4 5 6 7 8 9 10 |
# netstat -a | more Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN udp 0 0 *:bootpc *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6135 /tmp/.X11-unix/X0 unix 2 [ ACC ] STREAM LISTENING 5140 /var/run/acpid.socket |
Si deseas listar todos los puertos tcp debes usar netstat –at
1 2 3 4 5 6 7 |
# netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN |
Con el comando netstat –au puedes listar todos los puertos UDP.
1 2 3 4 5 6 |
# netstat -au Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:bootpc *:* udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* |
2. Listar los sockets que están en estado de escucha
netstat -l te permite listar solo los puertos de escucha.
1 2 3 4 5 6 |
# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN udp 0 0 *:49119 *:* |
Si quieres listar solo los puertos TCP que escuchan debes usar netstat -lt
1 2 3 4 5 6 |
# netstat -lt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 localhost:ipp [::]:* LISTEN |
Para listar solo los puertos UDP que escuchan lo puedes hacer con netstat -lu
1 2 3 4 5 |
# netstat -lu Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:49119 *:* udp 0 0 *:mdns *:* |
El comando netstat -lx te permite listar solo los puertos UNIX que escuchan.
1 2 3 4 5 6 7 |
# netstat -lx Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 6294 private/maildrop unix 2 [ ACC ] STREAM LISTENING 6203 public/cleanup unix 2 [ ACC ] STREAM LISTENING 6302 private/ifmail unix 2 [ ACC ] STREAM LISTENING 6306 private/bsmtp |
3. Mostrar las estadísticas de cada protocolo.
Puede ver las estadísticas para todos los puertos usando el comando netstat -s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# netstat -s Ip: 11150 total packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 incoming packets delivered 11635 requests sent out Icmp: 0 ICMP messages received 0 input ICMP message failed. Tcp: 582 active connections openings 2 failed connection attempts 25 connection resets received Udp: 1183 packets received 4 packets to unknown port received. ..... |
Si quieres mostrar estadísticas para los puertos TCP (o) UDP, hazlo usando netstat -st (o) -su
1 2 3 |
# netstat -st # netstat -su |
4. Mostrar los nombres de PID y programas en la salida de netstat usando netstat -p
La opción netstat -p se puede combinar con cualquier otra opción netstat. Esto agregará el “PID/Nombre del programa” a la salida de netstat. Esto es muy útil al depurar para identificar qué programa se está ejecutando en un puerto en particular.
1 2 3 4 5 |
# netstat -pt Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 1 0 ramesh-laptop.loc:47212 192.168.185.75:www CLOSE_WAIT 2109/firefox tcp 0 0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox |
5. No resolver el host, el puerto y el nombre de usuario en la salida de netstat
Cuando no desees que se muestre el nombre del host, puerto o usuario, tienes que usar la opción netstat -n. Esto se mostrará en números, en lugar de resolver el nombre de host, nombre de puerto, nombre de usuario.
Esto también acelera la salida, ya que netstat no está realizando ninguna búsqueda.
1 |
# netstat -an |
Si no deseas que solo se resuelva alguno de esos tres elementos (puertos, hosts o usuarios), debes usar los siguientes comandos.
1 2 3 4 5 |
# netsat -a --numeric-ports # netsat -a --numeric-hosts # netsat -a --numeric-users |
6. Imprimir la información netstat continuamente
netstat imprimirá información continuamente cada pocos segundos con el siguiente comando:
1 2 3 4 5 6 7 8 |
# netstat -c Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING tcp 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED tcp 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING ^C |
7. Encontrar las familias de direcciones que no son de soporte en tu sistema
1 |
netstat --verbose |
Al final, tendrás algo como esto:
1 2 3 4 |
netstat: no support for `AF IPX' on this system. netstat: no support for `AF AX25' on this system. netstat: no support for `AF X25' on this system. netstat: no support for `AF NETROM' on this system. |
8. Visualizar la información de enrutamiento del kernel usando netstat -r
1 2 3 4 5 6 |
# netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth2 link-local * 255.255.0.0 U 0 0 0 eth2 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2 |
Nota: puedes usar netstat -rn para mostrar las rutas en formato numérico sin resolver los nombres de host.
9. Encontrar en qué puerto se está ejecutando un programa.
1 2 3 4 5 |
# netstat -ap | grep ssh (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 1 0 dev-db:ssh 101.174.100.22:39213 CLOSE_WAIT - tcp 1 0 dev-db:ssh 101.174.100.22:57643 CLOSE_WAIT - |
Si deseas conocer qué proceso está utilizando un puerto particular, debes usar el siguiente comando:
1 |
# netstat -an | grep ':80' |
10. Mostrar la lista de interfaces de red
1 2 3 4 5 6 |
# netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 0 0 0 0 0 0 0 0 BMU eth2 1500 0 26196 0 0 0 26883 6 0 0 BMRU lo 16436 0 4 0 0 0 4 0 0 0 LRU |
Para mostrar información extendida en las interfaces (similar a ifconfig) lo puedes hacer usando netstat -ie:
1 2 3 4 5 6 7 8 9 |
# netstat -ie Kernel Interface table eth0 Link encap:Ethernet HWaddr 00:10:40:11:11:11 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Memory:f6ae0000-f6b00000 |