Linux ss指令查看套接字的统计信息
1. 指令例子
- 查看mysql的3306端口的状态
- 查看本地连接到X11的程序的连接状态
- 查看排除http和https端口或者目标不是193.233.7/24地址的fin-wait-1状态的连接
2. 官方参考文档
wikipedia介绍的ss指令和iproute2的关系,如下
https://en.wikipedia.org/wiki/Iproute2
iproute2 collection contains the following command-line utilities: ip
, ss
, bridge
, rtacct
, rtmon
, tc
, ctstat
, lnstat
, nstat
, routef
, routel
, rtstat
, tipc
and arpd
. tc
is used for traffic control. iproute2 utilities communicate with the Linux kernel using the netlink protocol. Some of the iproute2 utilities are often recommended over now-obsolete net-tools
utilities that provide the same functionality.
2.1 ss指令
https://www.kernel.org/doc/man-pages/
http://man7.org/linux/man-pages/man8/ss.8.html#top_of_page
2.2 ss指令的FILTER选项
http://www.cyberciti.biz/files/ss.html
2.2.1 STATE-FILTER
STATE-FILTER allows to construct arbitrary set of states to match. Its syntax is sequence of keywords state and exclude followed by identifier of state. Available identifiers are:
All standard TCP states: established, syn-sent, syn-recv, fin-wait-1, fin-wait-2, time-wait, closed, close-wait, last-ack, listen and closing.
all - for all the states
connected - all the states except for listen and closed
synchronized - all the connected states except for syn-sent
bucket - states, which are maintained as minisockets, i.e. time-wait and syn-recv.
big - opposite to bucket
2.2.2 ADDRESS_FILTER
ADDRESS_FILTER is boolean expression with operations and, or and not, which can be abbreviated in C style f.e. as &, &&.
Predicates check socket addresses, both local and remote. There are the following kinds of predicates:
dst ADDRESS_PATTERN - matches remote address and port
src ADDRESS_PATTERN - matches local address and port
dport RELOP PORT - compares remote port to a number
sport RELOP PORT - compares local port to a number
autobound - checks that socket is bound to an ephemeral port
The format and semantics of ADDRESS_PATTERN depends on address family.
inet - ADDRESS_PATTERN consists of IP prefix, optionally followed by colon and port. If prefix or port part is absent or replaced with *, this means wildcard match.
inet6 - The same as inet, only prefix refers to an IPv6 address. Unlike inet colon becomes ambiguous, so that ss allows to use scheme, like used in URLs, where address is suppounded with [ ... ].
unix - ADDRESS_PATTERN is shell-style wildcard.
packet - format looks like inet, only interface index stays instead of port and link layer protocol id instead of address.
netlink - format looks like inet, only socket pid stays instead of port and netlink channel instead of address.
PORT is syntactically ADDRESS_PATTERN with wildcard address part. Certainly, it is undefined for UNIX sockets.
2.2.3 ADDRESS_FILTER的更多细节
Option list may contain address/port filter. It is boolean expression which consists of boolean operation or, and, not and predicates. Actually, all the flavors of names for boolean operations are eaten: &, &&, |, ||, !, but do not forget about special sense given to these symbols by unix shells and escape them correctly, when used from command line.
Predicates may be of the folowing kinds:
-
A. Address/port match, where address is checked against mask and port is either wildcard or exact. It is one of:
dst prefix:port
src prefix:port
src unix:STRING
src link:protocol:ifindex
src nl:channel:pid
Both prefix and port may be absent or replaced with *, which means wildcard. UNIX socket use more powerful scheme matching to socket names by shell wildcards. Also, prefixes unix: and link: may be omitted, if address family is evident from context (with option -x or with -f unix or with unix keyword)
F.e.
dst 10.0.0.1
dst 10.0.0.1:
dst 10.0.0.1/32:
dst 10.0.0.1:*
are equivalent and mean socket connected to any port on host 10.0.0.1
dst 10.0.0.0/24:22
sockets connected to port 22 on network 10.0.0.0...255.
Note that port separated of address with colon, which creates troubles with IPv6 addresses. Generally, we interpret the last colon as splitting port. To allow to give IPv6 addresses, trick like used in IPv6 HTTP URLs may be used:
dst [::1]
are sockets connected to ::1 on any port
Another way is dst ::1128/. / helps to understand that colon is part of IPv6 address.
Now we can add another alias for dst 10.0.0.1: dst [10.0.0.1]. :-)
Address may be a DNS name. In this case all the addresses are looked up (in all the address families, if it is not limited by option -f or special address prefix inet:, inet6) and resulting expression is or over all of them.
-
B. Port expressions:
dport >= :1024
dport != :22
sport < :32000
etc. All the relations: <, >, =, >=, =, ==, !=, eq, ge, lt, ne... Use variant which you like more, but not forget to escape special characters when typing them in command line. :-) Note that port number syntactically coincides to the case A! You may even add an IP address, but it will not participate incomparison, except for == and !=, which are equivalent to corresponding predicates of type A. F.e.
dst 10.0.0.1:22 is equivalent to dport eq 10.0.0.1:22 and not dst 10.0.0.1:22 is equivalent to dport neq 10.0.0.1:22
2.3 ss指令的输出
2.3.1 参考netstat指令的部分
https://www.kernel.org/doc/man-pages/
http://man7.org/linux/man-pages/man8/netstat.8.html
2.3.2 大概的部分
http://www.cyberciti.biz/files/ss.html
Six columns. The first is Netid, it denotes socket type and transport protocol, when it is ambiguous: tcp, udp, raw, u_str is abbreviation for unix_stream, u_dgr for UNIX datagram sockets, nl for netlink, p_raw and p_dgr for raw and datagram packet sockets. This column is optional, it will be hidden, if filter selects an unique netid.
The second column is State. Socket state is displayed here. The names are standard TCP names, except for UNCONN, which cannot happen for TCP, but normal for not connected sockets of another types. Again, this column can be hidden.
Then two columns (Recv-Q and Send-Q) showing amount of data queued for receive and transmit.
And the last two columns display local address and port of the socket and its peer address, if the socket is connected.
If options -o, -e or -p were given, options are displayed not in fixed positions but separated by spaces pairs: option:value. If value is not a single number, it is presented as list of values, enclosed to ( ... ) and separated with commas. F.e.
timer:(keepalive,111min,0)
is typical format for TCP timer (option -o).
users:((X,113,3))
is typical for list of users (option -p).