Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Nov 10 '12 at 12:12

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

4 Answers 4

up vote 3 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: github.com/Russell91/sshrc –  singular Sep 12 at 7:35
@Jonah can you explain to me why you used the ${*:1} instead of $@. –  singular Sep 21 at 19:37

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

$ 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
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Chenmunka Sep 12 at 8:12
Nice. I like the xxd hack! –  Tomek Wyderka Sep 12 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. –  singular Sep 12 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 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. –  singular Sep 13 at 5:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.