loading local shell aliases to ssh session dynamicaly

23,305

Solution 1

You can temporarily copy your .bashrc to your remote machine with another name. For example, using .bashrc_temp:

user@local$ scp .bashrc user@remote:~/.bashrc_temp

Afterwards you can log into the remote machine:

user@local$ ssh user@remote

and source the file .bashrc_temp:

user@remote$ source ~/.bashrc_temp

Now you are able to use your .bashrc and your functions. When you are finished with your work you can remove the file ~/.bashrc_temp on the remote machine and logout.

The copying of the file and the login to the remote machine may be achieved with a bash function:

# copy the .bashrc to the remote machine 
# and log into the remote machine.
# parameter $1: user@remote
function s() {
  scp ~/.bashrc $1:~/.bashrc_temp
  ssh $1
}

Update:

You may also consider to copy the .bashrc to /tmp on your remote machine and source /tmp/.bashrc_temp.

Update 2:

You can log into the remote machine by using ssh -t. This will automatically use your temp .bashrc. Updated function s():

function s() {
  scp ~/.bashrc $1:/tmp/.bashrc_temp
  ssh -t $1 "bash --rcfile /tmp/.bashrc_temp ; rm /tmp/.bashrc_temp"
}

Solution 2

jens-na provided an excellent answer. I spent a bit of time and re-worked it a bit to work a teeny bit better. This way, you can pass along any parameter to SSH, such as port numbers. The difference is that it uses the ssh command to upload the .bashrc file, instead of scp, which uses different command parameter names.

You'll also notice that it uploads a different file, .bashrc_remote, so that you can select exactly what you want to source on remote servers, instead of everything

sshs() {
        ssh $@ "cat > /tmp/.bashrc_temp" < ~/.bashrc_remote
        ssh -t $@ "bash --rcfile /tmp/.bashrc_temp ; rm /tmp/.bashrc_temp"
}

Run it as follows:

sshs user@server

The name 'sshs' is for "SSH Source". Use ssh when you don't want to source, and use sshs when you do.

https://gist.github.com/jonahbron/5549848

Solution 3

I think sshrc is what you're looking for: https://github.com/Russell91/sshrc

sshrc works just like ssh, but it also sources ~/.sshrc after logging in remotely.

$ echo "echo welcome" > ~/.sshrc
$ sshrc me@myserver
welcome

$ echo "alias ..='cd ..'" > ~/.sshrc
$ sshrc me@myserver
$ type ..
.. is aliased to `cd ..'

You can use this to set environment variables, define functions, and run post-login commands. It's that simple, and it won't impact other users on the server - even if they use sshrc too. For more advanced configuration, continue reading.

Solution 4

Not sure of the limitations, but I was able to get this to work with something like:

function ssh_with_rc() {
   RC_DATA=`cat ${HOME}/.bashrc | base64 -w 0`
   ssh -t $@ "echo \"${RC_DATA}\" | base64 --decode > /tmp/${USER}_bashrc; bash --rcfile /tmp/${USER}_bashrc; rm /tmp/${USER}_bashrc"
}

alias ssh="ssh_with_rc"

Solution 5

This is what I came up with. It allows you to maintain a normal rc file, but also do everything in one ssh connection (ie only need to login once as opposed to doing an scp first).

#copies some environment over to the remote machine
function ssh() {
  /usr/bin/ssh -t $* "echo `base64 -i ~/bin/remote_ssh_env.sh` | base64 --decode > /tmp/remote_ssh_env.sh; bash --rcfile /tmp/remote_ssh_env.sh"
}

I'm not sure how big that rc file can be though, since it might max out at some point.

Share:
23,305

Related videos on Youtube

Tomek Wyderka
Author by

Tomek Wyderka

I'm software engineer specialized in: User Interfaces Architectures: my window manager: PLayWM my computer interface technology based on video: VUI Computer Usability: my set of terminal/shell tools: Terminal for Human Software Development Techniques: my approach to programming through real time execution: Conquer Currently working for my own company: Cofoh

Updated on September 18, 2022

Comments

  • Tomek Wyderka
    Tomek Wyderka over 1 year

    When I log in to some machine using ssh I wish my aliases and functions are there. In other words, log in to some machine I wish I can use my command shortcuts.

    I need it be dynamic, every time I'm log in I wish I have updated aliases.

    Notes: Very often it is first time log in, without knowing machine and files there. Sometimes it is single log in. Just one time for that machine. It need to be cleaned afterwards, previous configuration has to be restored too.

  • Admin
    Admin over 11 years
    This is definitely what I'm looking for. Thanks! But is it possible to do it more functional? In one step? Because now we need 3 steps: log in, source, delete before logout.
  • Admin
    Admin over 11 years
    OK, I see now after your update. Great. One more fix: "bash --rcfile /tmp/.bashrc_temp; rm /tmp/.bashrc_temp"
  • Tomek Wyderka
    Tomek Wyderka about 11 years
    That's useful. The same set of parameters, not need to add entry to ~/.ssh/config just to specify different port no!
  • Tomek Wyderka
    Tomek Wyderka about 11 years
    Since it is the same command name, maybe it is possible to do it in one run, and type password only once... Unfortunately joining your commands into one reports: Pseudo-terminal will not be allocated because stdin is not a terminal
  • Jonah
    Jonah about 11 years
    @TomekWyderka Yeah, I tried for about 20 minutes to get it down to one command, but didn't find a way. Perhaps a person more BASH-savvy than I could figure it out, but it doesn't appear to be possible.
  • RussellStewart
    RussellStewart over 9 years
    I've taken this answer and iterated on it over a few weeks. The result is a full blown tool that crushes the problem: github.com/Russell91/sshrc
  • Tomek Wyderka
    Tomek Wyderka over 9 years
    Nice. I like the xxd hack!
  • RussellStewart
    RussellStewart over 9 years
    @Tomek - yea the xxd hack is completely awesome. I really tried to get the job done with scp. Then with piping into ssh. It just can't be done without 2 calls to the server - 2 password entries - 2 round trips. It was unacceptable. So I did the unthinkable.
  • Tomek Wyderka
    Tomek Wyderka over 9 years
    I have a bug. sshrc hangs without response. Can you put some debug code (can be in the comments) into sshrc so I can trace it.
  • RussellStewart
    RussellStewart over 9 years
    Interesting. It's just a bash script so usually any errors or warnings thrown will be printed to the screen automatically. How many bytes are in your .sshrc.d? Also, just type vim $(which sshrc) and you can see the bash file. You can add echo commands after each line to see where it hangs.
  • Jonah
    Jonah over 9 years
    @singular Strictly ignorance :)
  • handicop
    handicop about 9 years
    I had to wrap part of the command in single quotes to get this working right. echo '`base64....sh`' -- after that, it worked beautifully. Thanks!!
  • handicop
    handicop about 9 years
    If size becomes an issue, you could pipe the file contents through a compression utility like gzip before base64 encoding.
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 7 years
    Lol, this never occurred to me either. It's quite a creative solution.
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 7 years
    This is the best answer for me because it's a single ssh command, no scp stuff. The only remaining improvement for me would be to find a way to avoid the RC_DATA variable.
  • TiMoch
    TiMoch over 6 years
    Almost all answers (including this one) use predictable filename in /tmp/. This can trivially be exploited by any other user to run any code as a user which logs in. This should use mktemp to make sure the temporary file has an unique name.
  • Ashark
    Ashark about 3 years
    The original link to the repository is dead. But I have found two forks: github.com/ezbik/sshrc and github.com/Cyberbeni/sshrc