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.

9 Answers 9


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


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"
  • 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, 2012 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, 2012 at 15:29
  • I have added your fix to my update 2. Thanks.
    – jens
    Nov 9, 2012 at 16:23
  • 6
    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.
    – Tometzky
    Jan 22, 2018 at 23:57

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.


  • That's useful. The same set of parameters, not need to add entry to ~/.ssh/config just to specify different port no! May 10, 2013 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 May 10, 2013 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, 2013 at 21:40
  • 2
    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 Sep 12, 2014 at 7:35
  • 1
    @Jonah can you explain to me why you used the ${*:1} instead of $@. Sep 21, 2014 at 19:37

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.

  • Nice. I like the xxd hack! Sep 12, 2014 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. Sep 12, 2014 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. Sep 13, 2014 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. Sep 13, 2014 at 5:34
  • The original link to the repository is dead. But I have found two forks: github.com/ezbik/sshrc and github.com/Cyberbeni/sshrc
    – Ashark
    Apr 13, 2021 at 9:13

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"
  • 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. Jul 15, 2017 at 1:26

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.

  • 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!!
    – K Robinson
    May 12, 2015 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, 2015 at 17:29

I think that https://github.com/fsquillace/pearl-ssh 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 myuser@myserver.com
myserver.com $> q

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.


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.


  • Lol, this never occurred to me either. It's quite a creative solution. Jul 15, 2017 at 1:17

for zsh users

set -x
# use only one method

# with scp - takes less time
scp ~/.sshrc "${@:$#}":/tmp/.sshrc
ssh -t $* "bash --rcfile /tmp/.sshrc
rm -rf /tmp/.sshrc"

# without scp - takes more less time 😀
ssh -t $* "echo $'"$(cat ~/.sshrc | openssl enc -base64)"' | tr -s ' ' $'\n' | openssl enc -base64 -d > /tmp/.sshrc
bash --rcfile /tmp/.sshrc
rm -rf /tmp/.sshrc"

scp command assumes last argumement as server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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