taoCMS-基于php+sqlite最小巧的CMShttp://www.taocms.org/taoCMS是基于php+sqlite/mysql的国内最小(100Kb左右)的功能完善、开源免费的CMS管理系统2023-06-10taoCMS-基于php+sqlite最小巧的CMS1265自定义设置 Linux网络带宽、特定Linux进程网络带宽There may be cases when you need to limit network bandwidth in Linux. Learn how to limit the network bandwidth in Linux globally and application-specifically with two simple programs: wondershaper and trickle.
There may be multiple reasons why you need to limit network bandwidth in Linux.
One of our subscribers says that the internet data prices are high where he lives, he wants to limit his network bandwidth to reduce the internet spending. As a former Windows user, he used to run Netlimiter for that. Now, he looks for a Netlimiter alternative in Linux.
Traffic limiting is also useful when the internet speed is low. Applications will compete for the traffic, and setting traffic limits on some applications helps to prioritize how the traffic is used.
In another case, a server admin had to limit the outbound traffic, so that the server will not go down due to heavy activity at peak times.
In this article, you will learn how to limit the network bandwidth in Linux globally and application-specifically with two lightweight and user-friendly applications.
Limiting the bandwidth per network interface with Wondershaper
Wondershaper is a simple command-line utility that can limit the network bandwidth in Linux. It can be configured to limit download and upload speed for each network interface of your Linux machine. Let’s have a look.
Install Wondershaper
To install Wondershaper, search for it in your Linux package manager. It is an open-source application that should be available in all Linux distros. This is the installation I recommend. In Ubuntu and other Debian-based distros, you can run this command:
sudo apt install wondershaper
If you want the latest version of Wondershaper, you can install it from GitHub:
git clone https://github.com/magnific0/wondershaper.git
cd wondershaper
sudo make install
Hopefully, this installation goes without errors for you. It is a less reliable way to install Wondershaper than using the version provided with your distro. I have tested this installation on Ubuntu 20.04.3, it works fine. But if you have problems with the Github version, remove it by running this command from the wondershaper git-cloned directory:
sudo make uninstall
If you decide to keep the GitHub version of Wondershaper, keep in mind that unlike the version included with the distro, it will not update itself when the new version is released. You need to update it manually. You can do it by entering the wondershaper folder, pulling the updates from GitHub, and re-installing:
More recent versions require specifying the option names:
wondershaper -a [interface] -d [downlink] -u [uplink]
Find out the network interface name
Now, when you know the syntax of wondershaper, you only need to find out the network interface name to set the limits on it. In most modern Linux systems, you can list the available network interfaces with this command:
ip addr show
However, some older systems may require to run ifconfig from net-tools:
sudo apt install net-tools
ifconfig
In my system, I only have the Ethernet interface enp0s3:
You may also have the Wi-Fi interface listed as wlan0 or similar.
Test Wondershaper
Knowing the network interface name, we can test wondershaper. For example, let’s set the bandwidth limit to 6 MB/s for download, and 1 MB/s to upload on the Ethernet interface enp0s3:
sudo wondershaper enp0s3 6144 1024
Now, you can check if these settings have been applied by testing your internet speed at Speedtest.net. This is the results I get with these settings:
If I clear all the limits:
sudo wondershaper clear enp0s3
And test my internet speed again, I get these values:
Note, if you have both the Wi-Fi and Ethernet connections, make sure you set the limit and test the same interface.
Run Wondershaper persistently
The settings we tested above work only until you reboot your Linux system. To apply the limits persistently, you need to create a systemd configuration and service files for Wondershaper.
Open the configuration file:
sudo nano /etc/systemd/wondershaper.conf
And paste the following content with your interface and limit settings:
[wondershaper]
# Adapter
IFACE="enp0s3"
# Download rate in Kbps
DSPEED="6144"
# Upload rate in Kbps
USPEED="1024"
Note, if your Wondershaper uses option names, you need to edit the ExecStart and ExecStop lines to:
ExecStart=/usr/sbin/wondershaper -a $IFACE -d $DSPEED -u $USPEED
ExecStop=/usr/sbin/wondershaper -c -a $IFACE
FYI, if you are not familiar with the nano editor, to save the changes press Ctrl+O and to exit press Ctrl+X.
Now, activate Wondershaper to run persistently:
sudo systemctl enable --now wondershaper.service
The Wondershaper will now limit network bandwidth in your Linux even after you reboot your system.
If you want to disable the persistent run of Wondershaper, execute:
sudo systemctl disable --now wondershaper.service
Limiting the bandwidth per application with trickle
Trickle is a lightweight bandwidth shaper that can be used to set the data limits per application. By using the Unix loader preloading, it adds a new version of the data transferring functionality to an application through sockets and then limits traffic by delaying the sending and receiving of data over a socket. Unlike Wondershaper, trickle does not require root privileges, so you do not need to use sudo when you run it.
Install trickle
Trickle is available in most Linux distributions. In Ubuntu, you install it with this command:
sudo apt install trickle
How to use trickle
You can see all available options and how to use trickle with its help message:
trickle -h
In most cases, you only need to set the download/upload limits and the application name:
You can also launch bash shell with bandwidth limits for all commands:
trickle -d 6144 -u 1024 bash
To reset the bandwidth limits, simply close the bash with the exit command or Ctrl+Q.
You can also add some advanced settings to your trickle command. You can read the detailed description of each option with the man command:
man trickle
where you can find out, for example, what the smoothing time option does:
The smoothing time determines with what intervals trickle will try to let the application transceive data. Smaller values will result in a more continuous (smooth) session, while larger values may produce bursts in the sending and receiving data. Smaller values (0.1 - 1 s) are ideal for interactive applications, while slightly larger values (1 - 10 s) are better for applications that need a bulk transfer.
trickle with rsync
There is a small difference in using trickle with rsync to copy files over ssh. If you simply put trickle in front of rsync, it will not work because rsync forks the ssh process. Thus, ssh will run without trickle limits. To call rsync with trickle limits, run it like this:
We can test trickle similarly as we tested Wondershaper at Speedtest.net. Let’s set the download limits to Firefox and add a small smoothing value as it is an interactive application:
trickle -d 6144 -u 1024 -t 0.1 firefox
These settings result in the following network speed for me:
You will see that the speed is limited to 547KB/s:
Apply application-specific limits persistently
Trickle can also be configured to have permanent network bandwidth limits and these limits can be global e.g. for all applications or application-specific. It is done through the trickled daemon.
See its options with the -h or man:
You can set the default bandwidth limits for all applications launched with trickle:
trickled -d 6144 -u 1024 -s
Now, every application you start with trickle application will have these limits and you do not need to specify the maximum upload and download rates. To keep these options permanent after the reboot, you can add this command to your ~/.bashrc or /etc/profile if you want these settings to be active for all users.
To set how these limits are shared by each application, you need to add them to the trickled.conf file. Open the configuration file:
We have looked at the time smoothing parameter above. The purpose of the length smoothing is the same, the only difference is that it is defined in KB. It is a fallback of the smoothing time option.
The priority parameter defines how the traffic is prioritized across applications. A lower value has a higher priority, and thus applications with a lower value get more bandwidth than the one with the higher value.
Here is an example of how a real trickled.conf file may look like:
Running trickle application in the terminal may not be the most convenient for graphical applications. Luckily, there is also an option to add trickle to the graphical launchers.
Some applications may not have the application.desktop files in /usr/share/applications/. This is the case for AppImages, snap, and flatpak apps. You can easily create a custom launcher for them by following our tutorial on how to create a custom launcher in Linux.
Disable automatic updates
I would like to share with you just one more tip on how to limit the internet traffic in your Linux. You may want to disable automatic updates in your system, so you update less often and when you have access to the faster and cheaper internet connection.
In Ubuntu, you do that by enabling the “Metered connection”:
When you need to update your system, you do it manually in the Update Manager or the command line.
Conclusion
I tried to provide simple and clear instructions on how to limit network bandwidth in Linux with Wondershaper and trickle without technical details. I hope these instructions helped you to configure your Linux bandwidth, and it saved you a couple of backs.
There is also an option to use the tc command and iptables configuration for traffic shaping, but this is one of the advanced topics which we do not cover on this website. However, you can get an idea of how tc is configured from the Arch Linux Wiki.
If you know more user-friendly tools that can help to limit network bandwidth in Linux, please share them in the comments below.
———————————————— 版权声明:本文为CSDN博主「Tomcow2021」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Tomcow2021/article/details/121537634]]>taoCMS-基于php+sqlite最小巧的CMS2022-07-12 00:07:121252Telegram Bot - how to get a group chat id?
创建机器人:https://www.telegramstr.com/article/120.html In order to get the group chat id, do as follows: