Making Game: Setting an alias in an ssh session without using bashrc/profile

Original Source Link

How can I set an alias on a remote machine over SSH with local ssh config (not editing the remote bashrc/profile files).

Background: I have a remote dev build machine that is shared with a few devs. We only use it for building software and publishing it, but occasionally, we need to make small changes in the code and git commit those. I don’t want ssh keys on the remote machine, so we are using SSH agent forwarding so our local SSH keys are available on the remote machine for git push. But we still need to set the git user.name and user.email on the remote machine. Currently, just after logging in, I execute alias git='git -c "user.name=My Name" -c "[email protected]"' so the git commits are attributed to me. Since we share the same account with a few devs, I don’t want to set that alias in the remote ~/.bashrc file. So is there a way to set that command in my local ~/.ssh/config for the host so it gets executed in the shell when I login?

Ok, from a bunch of different questions I did manage to piece a solution together.
First of all, you can use RemoteCommand and LocalCommand in your ~/.ssh/config file to execute a local command before logging in, and then instead of just dropping to a shell, execute a different command (shell). I couldn’t make it work with only RemoteCommand (there is no way it seems to execute a remote command that includes setting an alias), but what you can do is provide a different .bashrc file.

So it works like this: copy a local .bashrc file to the remote machine under a different name in /tmp. Login, execute bash and specify the .bashrc file, and at logout, remove it again. Also, when you use RemoteCommand in ~/.ssh/config you need to tell ssh to force a pseudo-tty so you get a normal interactive shell.

This is my relevant section in ~/.ssh/config:

Host mymachine
    Hostname mymachine.mydomain.com
    ForwardAgent yes
    User someuser
    RequestTTY force
    PermitLocalCommand yes
    LocalCommand scp ~/.sshrc [email protected]:/tmp/.bashrc_someuser
    RemoteCommand bash --rcfile /tmp/.bashrc_someuser; rm /tmp/.bashrc_someuser

The ~/.sshrc filename is arbitrary. It’s just a file anywhere on your local machine. you can even make multiple versions for different remote ssh machines.

This is what my ~/.sshrc file looks like:

source ~/.bashrc
alias git='git -c "user.name=Me" -c "[email protected]"'

This will ensure the .bashrc of the remote machine is applied, but then you can add your own customizations afterwards. Now I just ssh there with ssh mymachine and the git command works and uses whatever keys I have available in my local machine.

Tagged : / / /

Leave a Reply

Your email address will not be published. Required fields are marked *