Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Nov 10 '12 at 12:12

This question came from our site for professional and enthusiast programmers.

up vote 7 down vote accepted

You can temporarily copy your .bashrc to your remote mashine with another name. For example .bashrc_temp.

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

Afterwards you can log into the remote mashine

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 mashine and logout.

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

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


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

Update 2:

You can log into the remote mashine 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"
share|improve this answer
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. – Tomek Wyderka Nov 9 '12 at 3:22
OK, I see now after your update. Great. One more fix: "bash --rcfile /tmp/.bashrc_temp; rm /tmp/.bashrc_temp" – Tomek Wyderka Nov 9 '12 at 15:29
I have added your fix to my update 2. Thanks. – jens Nov 9 '12 at 16:23

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.

share|improve this answer
That's useful. The same set of parameters, not need to add entry to ~/.ssh/config just to specify different port no! – Tomek Wyderka May 10 '13 at 12:02
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 – Tomek Wyderka May 10 '13 at 12:04
@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. – Jonah May 10 '13 at 21:40
I've taken this answer and iterated on it over a few weeks. The result is a full blown tool that crushes the problem: – RussellStewart Sep 12 '14 at 7:35
@Jonah can you explain to me why you used the ${*:1} instead of $@. – RussellStewart Sep 21 '14 at 19:37

I think sshrc is what you're looking for:

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

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

$ 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.

share|improve this answer
Nice. I like the xxd hack! – Tomek Wyderka Sep 12 '14 at 17:55
@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. – RussellStewart Sep 12 '14 at 17:57
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. – Tomek Wyderka Sep 13 '14 at 5:31
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. – RussellStewart Sep 13 '14 at 5:34

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/` | base64 --decode > /tmp/; bash --rcfile /tmp/"

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

share|improve this answer
I had to wrap part of the command in single quotes to get this working right. echo '``' -- after that, it worked beautifully. Thanks!! – K Robinson May 12 '15 at 17:25
If size becomes an issue, you could pipe the file contents through a compression utility like gzip before base64 encoding. – K Robinson May 12 '15 at 17:29

I think that does what you need.

I wrote it long time ago before sshrc was born and it has more benefits compared to sshrc:

  • It does not require dependencies on xxd for both hosts (which can be unavailable on remote host)
  • Pearl-ssh uses a more efficient encoding algorithm
  • It is just ~20 lines of code (really easy to understand!)

For instance:

$> echo "alias q=exit" > ~/.config/pearl/sshrc
$> ssh_pearl $> q
share|improve this answer

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"
share|improve this answer

One option to make it work with a single SSH session is to use a variable to store your bash file in rather than copy it.

$ mybash=`cat mybash`
$ ssh -t "echo $mybash > /tmp/mybash; bash --rcfile /tmp/mybash ; rm /tmp/mybash"

Seems to work for me.

share|improve this answer

I was looking to solve a problem like yours, and realized what I was really looking for was sshfs. Maybe you can use it too?

My problem was that when ssh'ing, I wished to keep my colors, aliases, functions and scripts while working remote.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .