loading local shell aliases to ssh session dynamicaly
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.