Inspect socket config with sysctl
In this step, we'll explore how to inspect socket configurations using the sysctl
command. sysctl
is a command-line utility that allows you to view and modify kernel parameters at runtime. It's a powerful tool for fine-tuning your system's behavior, including network settings.
sysctl
reads and writes values from the /proc/sys
virtual filesystem. In the previous step, we used cat
to read values directly from files in /proc/sys/net
. sysctl
provides a more convenient and structured way to access and modify these parameters.
To begin, open your terminal.
Let's start by listing all available network-related sysctl
parameters. To do this, we can use the sysctl -a
command and filter the output using grep
to focus on network settings.
Type the following command into your terminal and press Enter:
sysctl -a | grep net.
This command does the following:
sysctl -a
: Lists all kernel parameters.
grep net.
: Filters the output to show only parameters that start with net.
.
You should see a long list of network-related parameters and their current values. The output will be similar to this:
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
To view the value of a specific parameter, use the sysctl
command followed by the parameter name. For example, to view the value of net.ipv4.ip_forward
, type the following command and press Enter:
sysctl net.ipv4.ip_forward
This command will display the current value of the net.ipv4.ip_forward
parameter.
net.ipv4.ip_forward = 0
To modify a sysctl
parameter, you can use the -w
option followed by the parameter name and the new value. However, keep in mind that changes made with the -w
option are temporary and will be lost after a reboot.
For example, to temporarily enable IP forwarding (set net.ipv4.ip_forward
to 1), type the following command and press Enter:
sudo sysctl -w net.ipv4.ip_forward=1
You can verify that the value has been changed by running the sysctl
command again:
sysctl net.ipv4.ip_forward
The output should now show:
net.ipv4.ip_forward = 1
To make the change permanent, you need to edit the /etc/sysctl.conf
file or create a new configuration file in the /etc/sysctl.d/
directory.
For example, let's create a new file named 99-labex.conf
in the /etc/sysctl.d/
directory and add the net.ipv4.ip_forward=1
setting to it.
First, create the file using nano
:
sudo nano /etc/sysctl.d/99-labex.conf
Then, add the following line to the file:
net.ipv4.ip_forward=1
Press Ctrl+X
, then Y
, then Enter
to save the file.
Finally, apply the changes by running the following command:
sudo sysctl -p /etc/sysctl.d/99-labex.conf
This command will read the configuration file and apply the settings.
By using the sysctl
command and modifying the /etc/sysctl.conf
file, you can fine-tune your system's network settings to optimize performance and security.