Původní balíček net-tools
je už dvacet let označen za zastaralý a s ním i řada známých utilit v něm obsažených. Většina běžných distribucí už také balíček ve výchozím stavu neinstaluje, ale nahradila jej moderním balíkem iproute2
. Tím také došlo k nahrazení utility netstat
moderní utilitou ss
(socket statistics).
Ta dělá podobnou práci: zjistí informace o socketech a vypíše je uživateli na standardní výstup. Dělá to ale rychleji a jednodušší formou. Zvyk je ale železná košile a spousta lidí ze zvyku stále ještě používá netstat
. Je čas se seznámit s ss
.
Krátké představení
Příkaz ss
slouží k zobrazení stavových informací o socketech typu PACKET, TCP, UDP, DCCP, RAW a Unix. Umožní vám jednoduše zjistit, jak váš počítač komunikuje s jinými počítači v síti či službami. Zobrazuje detaily týkající se spojení, protokolů a unixových socketů.
Jeho výhodou je jednoduchost proti původní utilitě netstat
. K ovládání souží jen několik přehledných přepínačů, přesto dokáže ss
zobrazit všechny potřebné informace. Hodí se zejména ve chvíli, kdy potřebujete odhalit zdroj nejrůznějších problémů.
Bez parametrů vypíše všechny informace o neposlouchajících socketech (TCP/UDP/Unix), které mají právě otevřená spojení:
# ss Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port u_seq ESTAB 0 0 @00004 13837 * 13838 u_str ESTAB 0 0 /run/systemd/journal/stdout 12445 * 12443 u_str ESTAB 0 0 * 20596 * 20597 u_str ESTAB 0 0 * 14046 * 14045 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 14418 * 13645 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13156 * 13155 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 13616 * 13615 u_str ESTAB 0 0 * 13615 * 13616 u_str ESTAB 0 0 * 14037 * 14038 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 13609 * 13293 u_str ESTAB 0 0 * 12985 * 13032 u_str ESTAB 0 0 * 14038 * 14037 u_str ESTAB 0 0 * 21865 * 21864 u_str ESTAB 0 0 * 20440 * 20441 u_str ESTAB 0 0 * 13155 * 13156 u_str ESTAB 0 0 * 19844 * 19845 u_str ESTAB 0 0 * 17816 * 17817 u_str ESTAB 0 0 * 21042 * 21041 u_str ESTAB 0 0 * 13360 * 13364 u_seq ESTAB 0 0 * 13835 * 13834 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13364 * 13360 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13031 * 12850 u_str ESTAB 0 0 * 12900 * 12901 u_str ESTAB 0 0 * 17778 * 17777 u_str ESTAB 0 0 * 20032 * 20031 u_str ESTAB 0 0 * 12850 * 13031 u_str ESTAB 0 0 * 20597 * 20596 u_str ESTAB 0 0 * 17777 * 17778 u_str ESTAB 0 0 /run/systemd/journal/stdout 12901 * 12900 u_str ESTAB 0 0 * 17782 * 17783 u_str ESTAB 0 0 * 12744 * 12745 u_str ESTAB 0 0 * 19155 * 19156 u_str ESTAB 0 0 * 13208 * 13209 u_str ESTAB 0 0 /run/systemd/journal/stdout 10119 * 10118 u_str ESTAB 0 0 * 21041 * 21042 u_str ESTAB 0 0 * 20441 * 20440 u_str ESTAB 0 0 /run/systemd/journal/stdout 12745 * 12744 u_str ESTAB 0 0 * 21864 * 21865 u_str ESTAB 0 0 * 14054 * 15352 u_str ESTAB 0 0 * 20787 * 20788 u_str ESTAB 0 0 * 19163 * 19164 u_str ESTAB 0 0 * 20031 * 20032 u_str ESTAB 0 0 * 20450 * 20449 u_str ESTAB 0 0 * 14214 * 14215 u_str ESTAB 0 0 /run/systemd/journal/stdout 12257 * 12256 u_str ESTAB 0 0 * 23374 * 23373 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 15349 * 14052 u_str ESTAB 0 0 * 13029 * 13030 u_str ESTAB 0 0 * 21052 * 21051 u_str ESTAB 0 0 * 14052 * 15349 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 15352 * 14054 u_str ESTAB 0 0 * 19156 * 19155 u_str ESTAB 0 0 * 14217 * 14218 u_seq ESTAB 0 0 * 13834 * 13835 u_str ESTAB 0 0 * 20449 * 20450 u_str ESTAB 0 0 * 20788 * 20787 u_str ESTAB 0 0 * 14218 * 14217 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 14837 * 14029 u_str ESTAB 0 0 * 21489 * 21490 u_seq ESTAB 0 0 * 13838 * 13837 u_str ESTAB 0 0 * 21051 * 21052 u_str ESTAB 0 0 * 10118 * 10119 u_str ESTAB 0 0 * 21490 * 21489 u_str ESTAB 0 0 * 17783 * 17782 u_str ESTAB 0 0 * 14029 * 14837 u_str ESTAB 0 0 * 17817 * 17816 u_str ESTAB 0 0 * 14816 * 14817 u_str ESTAB 0 0 * 13222 * 13223 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 14819 * 14024 u_str ESTAB 0 0 * 12256 * 12257 u_str ESTAB 0 0 * 14858 * 14859 u_seq ESTAB 0 0 * 14043 * 14044 u_str ESTAB 0 0 * 23373 * 23374 u_str ESTAB 0 0 * 14882 * 14881 u_seq ESTAB 0 0 * 14272 * 14273 u_str ESTAB 0 0 * 14881 * 14882 u_str ESTAB 0 0 * 12443 * 12445 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 13263 * 13578 u_str ESTAB 0 0 * 13030 * 13029 u_seq ESTAB 0 0 * 14273 * 14272 u_seq ESTAB 0 0 * 14044 * 14043 u_str ESTAB 0 0 * 13578 * 13263 u_str ESTAB 0 0 * 13293 * 13609 u_seq ESTAB 0 0 * 16981 * 16980 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13032 * 12985 u_str ESTAB 0 0 * 19158 * 19159 u_str ESTAB 0 0 * 14822 * 14821 u_str ESTAB 0 0 * 13254 * 13255 u_str ESTAB 0 0 * 23379 * 23380 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 14871 * 14870 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 13255 * 13254 u_str ESTAB 0 0 @/tmp/.X11-unix/X0 13223 * 13222 u_str ESTAB 0 0 * 14867 * 14868 u_str ESTAB 0 0 * 14045 * 14046 u_str ESTAB 0 0 * 14859 * 14858 u_str ESTAB 0 0 * 13645 * 14418 u_str ESTAB 0 0 * 19164 * 19163 u_str ESTAB 0 0 * 14024 * 14819 u_str ESTAB 0 0 * 12690 * 12691 u_str ESTAB 0 0 * 19348 * 19347 u_str ESTAB 0 0 * 19159 * 19158 u_str ESTAB 0 0 * 14870 * 14871 u_str ESTAB 0 0 * 14817 * 14816 u_str ESTAB 0 0 /run/systemd/journal/stdout 12691 * 12690 u_str ESTAB 0 0 * 19845 * 19844 u_str ESTAB 0 0 * 23380 * 23379 u_str ESTAB 0 0 * 14868 * 14867 u_str ESTAB 0 0 * 14821 * 14822 u_str ESTAB 0 0 /var/run/dbus/system_bus_socket 13209 * 13208 u_str ESTAB 0 0 * 19347 * 19348 u_str ESTAB 0 0 * 14215 * 14214 u_seq ESTAB 0 0 * 16980 * 16981 tcp ESTAB 0 0 10.0.2.15:55904 185.33.223.204:https tcp ESTAB 0 0 10.0.2.15:46428 52.214.253.204:https tcp ESTAB 120 0 10.0.2.15:41806 91.213.160.17:https tcp ESTAB 0 0 10.0.2.15:46858 2.18.69.88:https tcp ESTAB 0 0 10.0.2.15:36680 172.217.23.195:https tcp ESTAB 258 0 10.0.2.15:59420 2.18.69.21:https tcp ESTAB 0 0 10.0.2.15:33248 89.187.190.167:https tcp ESTAB 0 0 10.0.2.15:53736 178.77.214.142:https tcp ESTAB 0 0 10.0.2.15:50722 185.59.208.153:https tcp ESTAB 0 0 10.0.2.15:45536 69.173.144.143:https tcp ESTAB 0 0 10.0.2.15:45544 69.173.144.143:https tcp ESTAB 0 0 10.0.2.15:59680 172.217.23.238:https tcp ESTAB 0 0 10.0.2.15:41812 91.213.160.17:https tcp ESTAB 0 0 10.0.2.15:46430 52.214.253.204:https tcp ESTAB 0 0 10.0.2.15:45538 69.173.144.143:https tcp ESTAB 0 0 10.0.2.15:40266 99.80.111.158:https tcp ESTAB 0 0 10.0.2.15:36476 172.217.23.202:https tcp ESTAB 327 0 10.0.2.15:40270 99.80.111.158:https tcp ESTAB 0 0 10.0.2.15:36676 172.217.23.195:https tcp ESTAB 0 0 10.0.2.15:59396 149.202.197.102:https tcp ESTAB 0 0 10.0.2.15:59674 172.217.23.238:https tcp ESTAB 0 0 10.0.2.15:41382 91.213.160.188:http tcp ESTAB 0 0 10.0.2.15:43692 23.38.89.224:https tcp ESTAB 0 0 10.0.2.15:51644 172.217.23.228:https tcp ESTAB 327 0 10.0.2.15:40268 99.80.111.158:https tcp ESTAB 0 0 10.0.2.15:35504 172.217.23.200:https tcp ESTAB 0 0 10.0.2.15:50238 81.0.212.201:https tcp ESTAB 0 0 10.0.2.15:44168 178.250.2.130:https tcp ESTAB 120 0 10.0.2.15:41808 91.213.160.17:https tcp ESTAB 0 0 10.0.2.15:57714 216.58.201.98:https tcp ESTAB 0 0 10.0.2.15:55898 185.33.223.204:https tcp ESTAB 0 0 10.0.2.15:42878 91.213.160.175:https tcp CLOSE-WAIT 32 0 10.0.2.15:56434 52.218.64.100:https tcp ESTAB 0 0 10.0.2.15:35380 37.157.6.251:https tcp ESTAB 0 0 10.0.2.15:35464 172.217.23.234:https tcp ESTAB 0 0 10.0.2.15:44678 3.123.68.206:https tcp ESTAB 0 0 10.0.2.15:32964 217.16.188.162:https tcp ESTAB 0 0 10.0.2.15:58634 77.75.77.43:https tcp ESTAB 0 0 10.0.2.15:45430 199.232.17.108:https tcp ESTAB 0 0 10.0.2.15:45540 69.173.144.143:https tcp ESTAB 0 0 10.0.2.15:48660 216.58.201.110:https tcp ESTAB 120 0 10.0.2.15:41814 91.213.160.17:https tcp ESTAB 0 0 10.0.2.15:49086 37.252.173.27:https tcp ESTAB 0 0 10.0.2.15:57726 216.58.201.98:https tcp ESTAB 0 0 10.0.2.15:42896 91.213.160.175:https tcp ESTAB 0 0 10.0.2.15:49084 37.252.173.27:https tcp ESTAB 0 0 10.0.2.15:46498 91.213.160.188:https tcp ESTAB 0 0 10.0.2.15:45154 23.62.118.129:https tcp ESTAB 0 0 10.0.2.15:59672 185.152.65.78:https tcp ESTAB 120 0 10.0.2.15:41810 91.213.160.17:https tcp ESTAB 0 0 10.0.2.15:45534 69.173.144.143:https tcp ESTAB 0 0 10.0.2.15:49880 216.58.201.109:https tcp ESTAB 0 0 10.0.2.15:51648 172.217.23.228:https tcp ESTAB 0 0 10.0.2.15:35458 2.18.69.38:https tcp ESTAB 0 0 10.0.2.15:52116 77.75.79.9:https tcp ESTAB 0 0 10.0.2.15:36382 91.213.160.36:https tcp ESTAB 0 0 10.0.2.15:57728 216.58.201.98:https tcp ESTAB 0 0 10.0.2.15:45542 69.173.144.143:https
Protože příkaz často vypíše několik obrazovek textů, je dobré si výstup poslat například do prohlížečky less
nebo do souboru k dalšímu zpracování.
# ss | less
Pokud chceme vidět poslouchající sockety, použijeme parametr -l
, pro TCP pak -t
, UDP zobrazíme -u
a konečně unixové sockety pomocí -x
. Parametry můžeme samozřejmě kombinovat a zobrazit si například informace o tom, na jakých TCP portech náš počítač poslouchá:
# ss -tl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* LISTEN 0 128 :::ssh :::* LISTEN 0 64 :::http :::*
Pokud chceme vidět také poslouchající rozhraní, přidáme parametr -a
. V případě TCP se nám zobrazí sestavená spojení a jejich stav:
# ss -ta State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* CLOSE-WAIT 32 0 10.0.2.15:55904 185.33.223.204:https CLOSE-WAIT 32 0 10.0.2.15:46428 52.214.253.204:https CLOSE-WAIT 198 0 10.0.2.15:41806 91.213.160.17:https CLOSE-WAIT 290 0 10.0.2.15:59420 2.18.69.21:https CLOSE-WAIT 32 0 10.0.2.15:53736 178.77.214.142:https CLOSE-WAIT 1 0 10.0.2.15:45536 69.173.144.143:https CLOSE-WAIT 1 0 10.0.2.15:45544 69.173.144.143:https CLOSE-WAIT 1 0 10.0.2.15:46430 52.214.253.204:https CLOSE-WAIT 1 0 10.0.2.15:45538 69.173.144.143:https CLOSE-WAIT 405 0 10.0.2.15:40270 99.80.111.158:https ESTAB 0 0 10.0.2.15:34558 172.217.23.206:http ESTAB 0 0 10.0.2.15:36800 172.217.23.195:https CLOSE-WAIT 1 0 10.0.2.15:41382 91.213.160.188:http ESTAB 0 0 10.0.2.15:43692 23.38.89.224:https CLOSE-WAIT 1 0 10.0.2.15:34140 173.194.150.231:http CLOSE-WAIT 405 0 10.0.2.15:40268 99.80.111.158:https CLOSE-WAIT 198 0 10.0.2.15:41808 91.213.160.17:https CLOSE-WAIT 32 0 10.0.2.15:55898 185.33.223.204:https CLOSE-WAIT 32 0 10.0.2.15:42878 91.213.160.175:https CLOSE-WAIT 1 0 10.0.2.15:35320 173.194.150.249:http CLOSE-WAIT 32 0 10.0.2.15:56434 52.218.64.100:https CLOSE-WAIT 32 0 10.0.2.15:44678 3.123.68.206:https ESTAB 0 0 10.0.2.15:45430 199.232.17.108:https CLOSE-WAIT 1 0 10.0.2.15:45540 69.173.144.143:https CLOSE-WAIT 1 0 10.0.2.15:44220 172.217.130.71:http CLOSE-WAIT 198 0 10.0.2.15:41814 91.213.160.17:https CLOSE-WAIT 32 0 10.0.2.15:49086 37.252.173.27:https CLOSE-WAIT 32 0 10.0.2.15:42896 91.213.160.175:https CLOSE-WAIT 32 0 10.0.2.15:49084 37.252.173.27:https ESTAB 0 0 10.0.2.15:45154 23.62.118.129:https CLOSE-WAIT 198 0 10.0.2.15:41810 91.213.160.17:https CLOSE-WAIT 1 0 10.0.2.15:45534 69.173.144.143:https CLOSE-WAIT 1 0 10.0.2.15:49922 172.217.130.70:http ESTAB 0 0 10.0.2.15:35458 2.18.69.38:https CLOSE-WAIT 1 0 10.0.2.15:54528 173.194.150.236:http CLOSE-WAIT 1 0 10.0.2.15:36706 172.217.130.73:http CLOSE-WAIT 1 0 10.0.2.15:45542 69.173.144.143:https LISTEN 0 128 :::ssh :::* LISTEN 0 64 :::http :::*
Často nás zajímá také proces (PID), který je za dané spojení zodpovědný. Pro jeho zobrazení můžeme přidat parametr -p
.
# ss -tlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* users:(("sshd",pid=2105,fd=3)) LISTEN 0 128 :::ssh :::* users:(("sshd",pid=2105,fd=4)) LISTEN 0 64 :::http :::* users:(("webfsd",pid=4838,fd=3))
Filtrování stavu TCP
Užitečnou možností je také filtrování stavu existujících TCP spojení. Ty se v průběhu života každého spojení mění a pomocí ss
můžete jednoduše filtrovat výstup právě podle některého z těchto stavů:
- established
- syn-sent
- syn-recv
- fin-wait-1
- fin-wait-2
- time-wait
- closed
- close-wait
- last-ack
- listening
- closing
Kromě toho zná ss
ještě další varianty obsahující různé množiny výše uvedených stavů: all (vše), connected (vše kromě listen a closed), synchronized (vše kromě exception a syn-sent), bucket (stavy spravované jako minisockety: time-wait a syn-recv) a big (opak stavu bucket).
Filty je pak možné použít pomocí parametru state
:
# ss -ta state established Recv-Q Send-Q Local Address:Port Peer Address:Port 0 0 10.0.2.15:36608 172.217.23.202:https 0 0 10.0.2.15:58876 172.217.23.227:https 0 0 10.0.2.15:58868 172.217.23.227:https 0 0 10.0.2.15:58866 172.217.23.227:https 0 0 10.0.2.15:59826 172.217.23.238:https 0 0 10.0.2.15:59806 216.58.201.78:https 0 0 127.0.0.1:55816 127.0.0.1:http 0 0 10.0.2.15:36824 172.217.23.195:https 0 0 10.0.2.15:48806 216.58.201.110:https 0 0 ::ffff:127.0.0.1:http ::ffff:127.0.0.1:55816
Filtrace podle adres
Podobně je možné zobrazit si sockety podle zdrojových či cílových adres. Slouží k tomu parametry src
a dst
, které mohou být následovány jednou adresou či síťovou maskou ve formátu CIDR.
# ss -ta dst 127.0.0.1 State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTAB 0 0 127.0.0.1:55822 127.0.0.1:http CLOSE-WAIT 1 0 127.0.0.1:55820 127.0.0.1:http FIN-WAIT-2 0 0 ::ffff:127.0.0.1:http ::ffff:127.0.0.1:55820 ESTAB 0 0 ::ffff:127.0.0.1:http ::ffff:127.0.0.1:55822
Filtrace podle portů
Filtrovat můžeme také podle portů. Takto si můžeme vypsat například všechna existující SSH spojení:
# ss state established '( dport = :ssh or sport = :ssh )' Netid Recv-Q Send-Q Local Address:Port Peer Address:Port tcp 0 0 ::1:50860 ::1:ssh tcp 0 0 ::1:ssh ::1:50860
Souhrnné statistiky
Utilita ss umí také snadno a přehledně zobrazit souhrnné statistiky týkající se všech sledovaných socketů. Stačí k tomu zavolat ji s parametrem -s
. Hodí se to v případě, že máme na stroji hodně otevřených socketů a nechceme je všechny parsovat.
# ss -s Total: 249 (kernel 281) TCP: 7 (estab 2, closed 2, orphaned 0, synrecv 0, timewait 0/0), ports 0 Transport Total IP IPv6 * 281 - - RAW 0 0 0 UDP 3 3 0 TCP 5 1 4 INET 8 4 4 FRAG 0 0 0