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.

3 Answers 3

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
}

Update:

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 ${*:1} "cat > /tmp/.bashrc_temp" < ~/.bashrc_remote
        ssh -t ${*:1} "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

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

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 127.0.0.1 "echo $mybash > /tmp/mybash; bash --rcfile /tmp/mybash ; rm /tmp/mybash"

Seems to work for me.

share|improve this answer

Your Answer

 
discard

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.