简介
在这个实验中,你将学习如何检查 Linux 中是否配置了网络套接字选项。我们将探讨使用带有 -o
选项的 ss
命令来显示扩展的套接字信息,使你能够查看各种套接字选项,如 TCP 窗口大小和最大段大小(MSS)。
本实验将指导你使用 ss -no state all
来检查套接字详细信息,通过不进行服务名称解析来加快输出速度。你还将学习如何解读输出内容,其中包括套接字状态、地址和对诊断网络性能问题至关重要的扩展信息。
在这个实验中,你将学习如何检查 Linux 中是否配置了网络套接字选项。我们将探讨使用带有 -o
选项的 ss
命令来显示扩展的套接字信息,使你能够查看各种套接字选项,如 TCP 窗口大小和最大段大小(MSS)。
本实验将指导你使用 ss -no state all
来检查套接字详细信息,通过不进行服务名称解析来加快输出速度。你还将学习如何解读输出内容,其中包括套接字状态、地址和对诊断网络性能问题至关重要的扩展信息。
在这一步中,我们将探讨如何使用 ss
命令来检查套接字选项。ss
命令是一个用于调查网络套接字的强大工具。它类似于 netstat
,但 ss
通常更快,并且能提供更详细的信息。我们将重点介绍使用 -o
选项来显示扩展的套接字信息。
首先,让我们了解一下什么是套接字。套接字是网络上运行的两个程序之间双向通信链路的端点。套接字是网络编程的基础,理解它们对于网络故障排除和优化至关重要。
带有 -o
选项的 ss
命令允许你查看各种套接字选项,例如 TCP 窗口大小、TCP MSS(最大段大小)以及其他与 TCP 相关的参数。这些信息对于诊断网络性能问题非常有价值。
首先,打开你的终端。
现在,让我们运行带有 -o
选项的 ss
命令来查看套接字信息。我们还将使用 -n
选项来防止 ss
尝试解析服务名称,因为这可能会减慢输出速度。
在终端中输入以下命令并按回车键:
ss -no state all
此命令的作用如下:
ss
:调用套接字统计工具。-n
:防止服务名称解析。-o
:显示扩展信息。state all
:显示所有套接字,无论其状态如何。你应该会看到类似以下的输出:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
nl UNCONN 0 0 rtnl:kernel
nl UNCONN 0 0 fib_trie:kernel
nl UNCONN 0 0 xfrm:kernel
tcp LISTEN 0 4096 0.0.0.0:22 0.0.0.0:*
skmem:(r0,rb369280,t0,tb87380) tclass 0 pclass 0
tcp ESTAB 0 0 127.0.0.1:38768 127.0.0.1:22
skmem:(r0,rb87380,t0,tb87380) tclass 0 pclass 0 rcv_space:2048,4194304,65535 rcv_qlen:0 snd_space:32768,4194304,65535 snd_qlen:0
tcp LISTEN 0 4096 [::]:22 [::]:*
skmem:(r0,rb369280,t0,tb87380) tclass 0 pclass 0
输出显示了各种套接字连接、它们的状态以及其他相关信息。skmem
字段显示了与套接字相关的内存使用情况。rcv_space
和 snd_space
显示了接收和发送缓冲区的大小。
现在,让我们过滤输出,只关注 TCP 套接字并包含进程信息。这将帮助我们识别哪些进程正在使用特定的套接字。
在终端中输入以下命令并按回车键:
sudo ss -tnpo state all
此命令添加了以下选项:
-t
:过滤 TCP 套接字。-p
:显示使用套接字的进程。你应该会看到类似以下的输出:
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=942,fd=3))
ESTAB 0 0 127.0.0.1:22 127.0.0.1:49134 users:(("sshd",pid=1147,fd=5))
LISTEN 0 4096 [::]:22 [::]:* users:(("sshd",pid=942,fd=4))
Process
列现在显示了与每个套接字关联的进程名称和 PID(进程 ID)。这对于识别哪些应用程序正在使用特定的网络连接非常有用。
通过使用 ss -o
,你可以深入了解系统上网络套接字的配置和状态。这些信息可用于排查网络问题、优化网络性能以及了解应用程序如何使用网络资源。
在这一步中,我们将探讨如何使用 /proc/sys/net
目录来验证套接字设置。/proc
文件系统是一个虚拟文件系统,它提供有关正在运行的进程和内核配置的信息。/proc/sys/net
目录包含各种控制网络参数的文件,其中包括套接字设置。
理解这些设置对于优化网络性能和解决网络相关问题至关重要。这些设置会影响内核处理网络连接、缓冲区大小、超时时间和其他关键参数的方式。
首先,打开你的终端。
让我们从列出 /proc/sys/net/ipv4
目录的内容开始。该目录包含特定于 IPv4 网络的设置。
在终端中输入以下命令并按回车键:
ls /proc/sys/net/ipv4
此命令将列出 /proc/sys/net/ipv4
目录中的文件。每个文件代表一个特定的网络参数。
你应该会看到类似以下的输出:
conf icmp_echo_ignore_all icmp_ignore_bogus_error_responses icmp_timestamp_ignore_replies ip_default_ttl ip_forward ip_local_port_range neigh route tcp_abc tcp_adv_win_scale tcp_allowed_congestion_control tcp_congestion_control tcp_dsack tcp_ecn tcp_fack tcp_fastopen tcp_fin_timeout tcp_frto tcp_keepalive_intvl tcp_keepalive_probes tcp_keepalive_time tcp_low_latency tcp_max_syn_backlog tcp_max_tw_buckets tcp_moderate_rcvbuf tcp_mtu_probing tcp_no_metrics_save tcp_orphan_retries tcp_reordering tcp_retries1 tcp_retries2 tcp_rfc1337 tcp_rmem tcp_sack tcp_slow_start_after_idle tcp_syn_retries tcp_syncookies tcp_timestamps tcp_tw_recycle tcp_tw_reuse tcp_wmem
现在,让我们检查一个特定设置的值。例如,我们来查看 tcp_syn_retries
的值。此设置决定了内核在建立 TCP 连接时尝试重传 SYN 数据包的次数。
要查看 tcp_syn_retries
的值,请使用 cat
命令:
cat /proc/sys/net/ipv4/tcp_syn_retries
你应该会看到一个数字作为输出,通常是 6
。这意味着内核将最多尝试重传 SYN 数据包 6 次。
6
你还可以查看 /proc/sys/net/ipv4
目录中其他文件的内容,以了解它们的当前值。例如,要查看系统可以使用的本地端口范围,你可以查看 ip_local_port_range
文件:
cat /proc/sys/net/ipv4/ip_local_port_range
输出将显示本地端口范围的下限和上限:
32768 60999
要探索与所有 IP 版本相关的设置,你还可以检查 /proc/sys/net/core
。
ls /proc/sys/net/core
default_qdisc dev_weight message_burst message_cost netdev_max_backlog optmem_max rmem_default rmem_max somaxconn warnings wmem_default wmem_max
并检查 rmem_default
的值:
cat /proc/sys/net/core/rmem_default
212992
通过检查 /proc/sys/net
目录中的文件,你可以更好地了解系统的网络配置情况。这些知识对于解决网络问题和优化网络性能非常有价值。
在这一步中,我们将探讨如何使用 sysctl
命令来检查套接字配置。sysctl
是一个命令行工具,允许你在运行时查看和修改内核参数。它是一个强大的工具,可用于微调系统行为,包括网络设置。
sysctl
从 /proc/sys
虚拟文件系统中读取和写入值。在上一步中,我们使用 cat
直接从 /proc/sys/net
中的文件读取值。sysctl
提供了一种更方便、更结构化的方式来访问和修改这些参数。
首先,打开你的终端。
让我们从列出所有可用的与网络相关的 sysctl
参数开始。为此,我们可以使用 sysctl -a
命令,并使用 grep
过滤输出,以专注于网络设置。
在终端中输入以下命令并按回车键:
sysctl -a | grep net.
此命令的作用如下:
sysctl -a
:列出所有内核参数。grep net.
:过滤输出,只显示以 net.
开头的参数。你应该会看到一长串与网络相关的参数及其当前值。输出将类似于以下内容:
net.bridge.bridge-nf-call-arptables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-ip6tables = 0
net.core.bpf_jit_enable = 0
net.core.busy_poll = 50
net.core.busy_read = 50
net.core.default_qdisc = fq_codel
net.core.dev_weight = 64
net.core.message_burst = 10
net.core.message_cost = 5
net.core.netdev_max_backlog = 1000
net.core.optmem_max = 20480
net.core.rmem_default = 212992
net.core.rmem_max = 1703936
net.core.somaxconn = 128
net.core.warnings = 1
net.core.wmem_default = 212992
net.core.wmem_max = 1703936
net.ipv4.conf.all.accept_redirects = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_notify = 0
net.ipv4.conf.all.bootp_relay = 0
net.ipv4.conf.all.forwarding = 0
net.ipv4.conf.all.ignore_routes_with_linkdown = 0
net.ipv4.conf.all.log_martians = 0
net.ipv4.conf.all.mc_forwarding = 0
net.ipv4.conf.all.medium_id = 0
net.ipv4.conf.all.promote_secondaries = 1
net.ipv4.conf.all.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.all.secure_redirects = 1
net.ipv4.conf.all.send_redirects = 1
net.ipv4.conf.default.accept_redirects = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.arp_filter = 0
net.ipv4.conf.default.arp_ignore = 0
net.ipv4.conf.default.arp_notify = 0
net.ipv4.conf.default.bootp_relay = 0
net.ipv4.conf.default.forwarding = 0
net.ipv4.conf.default.ignore_routes_with_linkdown = 0
net.ipv4.conf.default.log_martians = 0
net.ipv4.conf.default.mc_forwarding = 0
net.ipv4.conf.default.medium_id = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.secure_redirects = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.lo.accept_redirects = 1
net.ipv4.conf.lo.accept_source_route = 0
net.ipv4.conf.lo.arp_filter = 0
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_notify = 0
net.ipv4.conf.lo.bootp_relay = 0
net.ipv4.conf.lo.forwarding = 0
net.ipv4.conf.lo.ignore_routes_with_linkdown = 0
net.ipv4.conf.lo.log_martians = 0
net.ipv4.conf.lo.mc_forwarding = 0
net.ipv4.conf.lo.medium_id = 0
net.ipv4.conf.lo.promote_secondaries = 1
net.ipv4.conf.lo.proxy_arp = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.lo.secure_redirects = 1
net.ipv4.conf.lo.send_redirects = 1
net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.icmp_timestamp_ignore_replies = 0
net.ipv4.ip_default_ttl = 64
net.ipv4.ip_forward = 0
net.ipv4.ip_local_port_range = 32768 60999
net.ipv4.neigh.default.anycast_delay = 1
net.ipv4.neigh.default.app_solicit = 0
net.ipv4.neigh.default.base_reachable_time_ms = 30000
net.ipv4.neigh.default.gc_interval = 30
net.ipv4.neigh.default.gc_stale_time = 60
net.ipv4.neigh.default.locktime = 0
net.ipv4.neigh.default.mcast_solicit = 3
net.ipv4.neigh.default.proxy_qlen = 64
net.ipv4.neigh.default.retrans_time_ms = 1000
net.ipv4.neigh.default.ucast_solicit = 3
net.ipv4.route.flush = 0
net.ipv4.route.gc_elasticity = 9
net.ipv4.route.gc_interval = 30
net.ipv4.route.gc_min_interval = 0
net.ipv4.route.gc_timeout = 210
net.ipv4.route.max_size = 4096
net.ipv4.route.min_adv_mss = 536
net.ipv4.route.mtu_expires = 600
net.ipv4.tcp_abc = 0
net.ipv4.tcp_adv_win_scale = 1
net.ipv4.tcp_allowed_congestion_control = reno
net.ipv4.tcp_congestion_control = reno
net.ipv4.tcp_dsack = 1
net.ipv4.tcp_ecn = 2
net.ipv4.tcp_fack = 1
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_frto = 2
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_low_latency = 0
net.ipv4.tcp_max_syn_backlog = 256
net.ipv4.tcp_max_tw_buckets = 180000
net.ipv4.tcp_moderate_rcvbuf = 1
net.ipv4.tcp_mtu_probing = 0
net.ipv4.tcp_no_metrics_save = 0
net.ipv4.tcp_orphan_retries = 8
net.ipv4.tcp_reordering = 3
net.ipv4.tcp_retries1 = 8
net.ipv4.tcp_retries2 = 15
net.ipv4.tcp_rfc1337 = 0
net.ipv4.tcp_rmem = 4096 212992 6291456
net.ipv4.tcp_sack = 1
net.ipv4.tcp_slow_start_after_idle = 1
net.ipv4.tcp_syn_retries = 6
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 0
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv6.conf.all.accept_dad = 1
net.ipv6.conf.all.accept_ra = 1
net.ipv6.conf.all.accept_redirects = 1
net.ipv6.conf.all.autoconf = 1
net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.all.force_mld_version = 0
net.ipv6.conf.all.hop_limit = 64
net.ipv6.conf.all.ignore_routes_with_linkdown = 0
net.ipv6.conf.all.max_addresses = 16
net.ipv6.conf.all.mldv1_unsolicited_report_interval = 1000
net.ipv6.conf.all.mldv2_unsolicited_report_interval = 100
net.ipv6.conf.all.mtu = 0
net.ipv6.conf.all.proxy_ndp = 0
net.ipv6.conf.all.regen_max_random_factor = 600
net.ipv6.conf.all.router_solicitation_delay = 1
net.ipv6.conf.all.router_solicitation_interval = 4
net.ipv6.conf.all.router_solicitations = -1
net.ipv6.conf.all.seg6_enabled = 1
net.ipv6.conf.all.seg6_require_hmac = 0
net.ipv6.conf.all.use_optimistic_dad = 0
net.ipv6.conf.all.use_tempaddr = 0
net.ipv6.conf.default.accept_dad = 1
net.ipv6.conf.default.accept_ra = 1
net.ipv6.conf.default.accept_redirects = 1
net.ipv6.conf.default.autoconf = 1
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.default.force_mld_version = 0
net.ipv6.conf.default.hop_limit = 64
net.ipv6.conf.default.ignore_routes_with_linkdown = 0
net.ipv6.conf.default.max_addresses = 16
net.ipv6.conf.default.mldv1_unsolicited_report_interval = 1000
net.ipv6.conf.default.mldv2_unsolicited_report_interval = 100
net.ipv6.conf.default.mtu = 0
net.ipv6.conf.default.proxy_ndp = 0
net.ipv6.conf.default.regen_max_random_factor = 600
net.ipv6.conf.default.router_solicitation_delay = 1
net.ipv6.conf.default.router_solicitation_interval = 4
net.ipv6.conf.default.router_solicitations = -1
net.ipv6.conf.default.seg6_enabled = 1
net.ipv6.conf.default.seg6_require_hmac = 0
net.ipv6.conf.default.use_optimistic_dad = 0
net.ipv6.conf.default.use_tempaddr = 0
net.ipv6.conf.eth0.accept_dad = 1
net.ipv6.conf.eth0.accept_ra = 1
net.ipv6.conf.eth0.accept_redirects = 1
net.ipv6.conf.eth0.autoconf = 1
net.ipv6.conf.eth0.disable_ipv6 = 0
net.ipv6.conf.eth0.force_mld_version = 0
net.ipv6.conf.eth0.hop_limit = 64
net.ipv6.conf.eth0.ignore_routes_with_linkdown = 0
net.ipv6.conf.eth0.max_addresses = 16
net.ipv6.conf.eth0.mldv1_unsolicited_report_interval = 1000
net.ipv6.conf.eth0.mldv2_unsolicited_report_interval = 100
net.ipv6.conf.eth0.mtu = 1500
net.ipv6.conf.eth0.proxy_ndp = 0
net.ipv6.conf.eth0.regen_max_random_factor = 600
net.ipv6.conf.eth0.router_solicitation_delay = 1
net.ipv6.conf.eth0.router_solicitation_interval = 4
net.ipv6.conf.eth0.router_solicitations = -1
net.ipv6.conf.eth0.seg6_enabled = 1
net.ipv6.conf.eth0.seg6_require_hmac = 0
net.ipv6.conf.eth0.use_optimistic_dad = 0
net.ipv6.conf.eth0.use_tempaddr = 0
net.ipv6.conf.lo.accept_dad = 1
net.ipv6.conf.lo.accept_ra = 1
net.ipv6.conf.lo.accept_redirects = 1
net.ipv6.conf.lo.autoconf = 1
net.ipv6.conf.lo.disable_ipv6 = 0
net.ipv6.conf.lo.force_mld_version = 0
net.ipv6.conf.lo.hop_limit = 64
net.ipv6.conf.lo.ignore_routes_with_linkdown = 0
net.ipv6.conf.lo.max_addresses = 16
net.ipv6.conf.lo.mldv1_unsolicited_report_interval = 1000
net.ipv6.conf.lo.mldv2_unsolicited_report_interval = 100
net.ipv6.conf.lo.mtu = 65536
net.ipv6.conf.lo.proxy_ndp = 0
net.ipv6.conf.lo.regen_max_random_factor = 600
net.ipv6.conf.lo.router_solicitation_delay = 1
net.ipv6.conf.lo.router_solicitation_interval = 4
net.ipv6.conf.lo.router_solicitations = -1
net.ipv6.conf.lo.seg6_enabled = 1
net.ipv6.conf.lo.seg6_require_hmac = 0
net.ipv6.conf.lo.use_optimistic_dad = 0
net.ipv6.conf.lo.use_tempaddr = 0
net.netfilter.nf_conntrack_acct = 0
net.netfilter.nf_conntrack_buckets = 65536
net.netfilter.nf_conntrack_checksum = 0
net.netfilter.nf_conntrack_count = 256
net.netfilter.nf_conntrack_expect_max = 128
net.netfilter.nf_conntrack_frag6_high_thresh = 1048576
net.netfilter.nf_conntrack_frag6_low_thresh = 524288
net.netfilter.nf_conntrack_frag6_timeout = 60
net.netfilter.nf_conntrack_frag_high_thresh = 1048576
net.netfilter.nf_conntrack_frag_low_thresh = 524288
net.netfilter.nf_conntrack_frag_timeout = 60
net.netfilter.nf_conntrack_generic_timeout = 3600
net.netfilter.nf_conntrack_helper = 1
net.netfilter.nf_conntrack_icmpv6_timeout = 10
net.netfilter.nf_conntrack_icmp_timeout = 30
net.netfilter.nf_conntrack_log_invalid = 0
net.netfilter.nf_conntrack_max = 262144
net.netfilter.nf_conntrack_sctp_timeout_closed = 0
net.netfilter.nf_conntrack_sctp_timeout_cookie_echoed = 120
net.netfilter.nf_conntrack_sctp_timeout_cookie_wait = 120
net.netfilter.nf_conntrack_sctp_timeout_heartbeat_ack = 0
net.netfilter.nf_conntrack_sctp_timeout_heartbeat_sent = 30
net.netfilter.nf_conntrack_sctp_timeout_shutdown_ack_sent = 0
net.netfilter.nf_conntrack_sctp_timeout_shutdown_recd = 0
net.netfilter.nf_conntrack_sctp_timeout_shutdown_sent = 0
net.netfilter.nf_conntrack_tcp_loose = 1
net.netfilter.nf_conntrack_tcp_max_retrans = 3
net.netfilter.nf_conntrack_tcp_timeout_close = 10
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_established = 86400
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_last_ack = 30
net.netfilter.nf_conntrack_tcp_timeout_max_retrans = 300
net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 60
net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 120
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.unix.max_dgram_qlen = 512
要查看特定参数的值,请使用 sysctl
命令,后跟参数名称。例如,要查看 net.ipv4.ip_forward
的值,请输入以下命令并按回车键:
sysctl net.ipv4.ip_forward
此命令将显示 net.ipv4.ip_forward
参数的当前值。
net.ipv4.ip_forward = 0
要修改 sysctl
参数,你可以使用 -w
选项,后跟参数名称和新值。但是,请记住,使用 -w
选项所做的更改是临时的,重启后将丢失。
例如,要临时启用 IP 转发(将 net.ipv4.ip_forward
设置为 1),请输入以下命令并按回车键:
sudo sysctl -w net.ipv4.ip_forward=1
你可以通过再次运行 sysctl
命令来验证值是否已更改:
sysctl net.ipv4.ip_forward
输出现在应该显示:
net.ipv4.ip_forward = 1
要使更改永久生效,你需要编辑 /etc/sysctl.conf
文件,或者在 /etc/sysctl.d/
目录中创建一个新的配置文件。
例如,让我们在 /etc/sysctl.d/
目录中创建一个名为 99-labex.conf
的新文件,并将 net.ipv4.ip_forward=1
设置添加到其中。
首先,使用 nano
创建文件:
sudo nano /etc/sysctl.d/99-labex.conf
然后,将以下行添加到文件中:
net.ipv4.ip_forward=1
按 Ctrl+X
,然后按 Y
,再按 Enter
保存文件。
最后,通过运行以下命令应用更改:
sudo sysctl -p /etc/sysctl.d/99-labex.conf
此命令将读取配置文件并应用设置。
通过使用 sysctl
命令并修改 /etc/sysctl.conf
文件,你可以微调系统的网络设置,以优化性能和安全性。
在本次实验中,我们探讨了如何使用 ss
命令在 Linux 系统中检查网络套接字选项。关键要点在于理解如何使用 ss -no state all
来显示扩展的套接字信息,包括 TCP 窗口大小和最大段大小(MSS),这些信息对于诊断网络性能问题至关重要。
本次实验强调了套接字作为网络通信端点的重要性,并展示了 ss
命令如何提供有关套接字参数的详细信息,从而实现有效的网络故障排除和优化。