When using curl to initiate network requests, you may need to specify a particular source IP address, especially when the host has multiple IP addresses. curl provides a convenient option --interface to achieve this.
Consider a server with multiple IP addresses, such as 192.168.1.100 and 192.168.1.101. To initiate an HTTP request using the IP address 192.168.1.101, you can use the following curl command:
bashcurl --interface 192.168.1.101 http://example.com
The --interface option is followed by the source IP address you want to use. This command causes curl to access http://example.com through the specified IP address 192.168.1.101.
Beyond directly specifying an IP address, the --interface option can also accept network interface names (such as eth0, eth1, etc.). For example, if 192.168.1.101 is assigned to the network interface eth1, you can specify:
bashcurl --interface eth1 http://example.com
This approach to specifying the source IP address is highly practical, especially when performing IP address-related tests or when sending requests via specific network interfaces on servers with multiple network interfaces. This functionality helps network administrators and developers better manage network traffic and troubleshoot network issues.