How to set the color of terminal text in ssh session exact same as local session?
The main issue here is that the prompt command and colouring options are sourced from the computer you are logged into.  So when you ssh somewhere, you must change the colouring/prompts on that server as well.

There are a couple of options.


Depending on the OS of the server you are logging into you could reset the prompt colours in the login scripts there.  Or;
Add an exception in the preexec command you installed before so it doesn't change the foreground colour for ssh commands


Something like
    [ "$BASH_COMMAND" =~ "^ssh" ] &amp;&amp; return
in the preexec_invoke_exec function.