Now that host A has two interfaces (192.168.1.15 and 192.168.2.1) in addition to the loopback interface (127.0.0.1), we can observe how Linux will choose a source IP address to communicate with.
When an application starts, it has the option to bind to an IP address. If the application does not explicitly do so, Linux will automatically choose the IP address on behalf of the application on a connection-by-connection basis. When Linux is making the decision, it examines a connection’s destination IP address, makes a routing decision based on the current route table, and then selects the IP address corresponding to the interface that the connection will go out of. For example, if an application on host A makes a connection to 192.168.1.100, Linux will find that the packet should go out of the eth0 interface, and thus, the source IP address for the connection will be 192.168.1.15.
Let us assume that the application does choose to bind to an IP address. If the application were to bind to 192.168.2.1, Linux will use that as the source IP address, regardless of which interface the connection will leave from. For example, if the application is bound to 192.168.2.1 and a connection is made to 192.168.1.100, the connection will leave out of eth0 (192.168.1.15) with the source IP address of 192.168.2.1. It is now the responsibility of the remote host (192.168.1.100) to know how to send a packet back to 192.168.2.1. (Presumably, the default route for 192.168.1.100 will know how to deal with that case.) For hosts that have aliased IP addresses, a single interface may have many IP addresses. For example, we can assign eth0:0 to 192.168.1.16, eth0:1 to 192.168.1.17, and eth0:2 to 192.168.1.18. In this case, if the connection leaves from the eth0 interface and the application did not bind to a specific interface, Linux will always choose the nonaliased IP address, that is, 192.168.1.15 for eth0. If the application did choose to bind to an IP address, say, 192.168.1.17, Linux will use that IP address as the source IP, regardless of whether the connection leaves from eth0 or eth1.