乐闻世界logo
搜索文章和话题

How to view and edit the ephemeral port range on Linux?

1个答案

1

Viewing and editing the ephemeral port range on Linux can be accomplished through multiple methods, but the most common approach involves inspecting and modifying the file /proc/sys/net/ipv4/ip_local_port_range. The following details the step-by-step instructions and commands:

Viewing the Current Ephemeral Port Range

  1. Viewing the Current Port Range with the cat Command

Open a terminal and execute the following command:

bash
cat /proc/sys/net/ipv4/ip_local_port_range

This command displays the current port range; for instance, the output 32768 60999 indicates that the ephemeral port range spans from 32768 to 60999.

Editing the Ephemeral Port Range

  1. Temporarily Changing the Port Range with the echo Command

To make a temporary adjustment to the port range (which reverts after a reboot), use the echo command to directly write the new range to the file. For example, to set the port range to 20000 65000, run:

bash
echo "20000 65000" > /proc/sys/net/ipv4/ip_local_port_range
  1. Permanently Changing the Port Range with the sysctl Command

For a persistent change, it is recommended to use the sysctl tool and update the system configuration file /etc/sysctl.conf. First, open the sysctl.conf file with a text editor:

bash
sudo nano /etc/sysctl.conf

Add or modify the following line in the file:

shell
net.ipv4.ip_local_port_range = 20000 65000

After saving and closing the file, apply the changes by executing:

bash
sudo sysctl -p

In this manner, you can view and adjust the ephemeral port range on your Linux system. By doing so, you can tailor port allocation to meet the requirements of your network applications, thereby optimizing performance and resource management.

2024年8月5日 10:03 回复

你的答案