More information about your network setup would be needed to answer this fully, so I will be making a few assumptions here. You need to adjust the answer to your case.
First question is: you say you have a ssh connection from VM1 to VM2. Do you have only the ssh connection, or do you have regular, full network connection between VM1 and VM2? If you do have a full connection, you don't need to use a ssh tunnel for the proxy, it just overcomplicates things.
Assume for the moment you have a full connection and your VM1 IP address is 192.168.1.100
and your VM2 IP address is 192.168.1.200
. You should configure a proxy on VM1 to listen for example on port 3128 (that's usually a common port number used for proxy, although you can use any unused port number), and configure VM2 to use proxy 192.168.1.100:3128
. That's basically all.
If you have only the ssh connection (for example there is a firewall between the two VMs that passes only the ssh traffic), you need to forward ports over ssh connection. Assume you are still running proxy on VM1 on port 3128, and you are connecting via ssh from VM1 to VM2 (as you wrote in your question). In that case, you need to use the following command on VM1 to connect to VM2:
ssh -R 3128:localhost:3128 192.168.1.200
After you establish the connection, you can set VM2 to use proxy at localhost:3128
.
You can also connect the other way, ie. from VM2 to VM1, using the command (on VM2):
ssh -L 3128:localhost:3128 192.168.1.100
Similarly, after the connection is established, set VM2 to use proxy at localhost:3128
.