Verhogen
Verhogen

Reputation: 28631

Using local settings through SSH

Is it possible to have an SSH session use all your local configuration files (.bash_profile, .vimrc, etc..) on login? That way you would have the same configuration for, say, editing files in vim in the remote session.

Upvotes: 12

Views: 10531

Answers (10)

user967489
user967489

Reputation: 49

I think that https://github.com/fsquillace/kyrat 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)
  • Kyrat uses a more efficient encoding algorithm
  • It is just ~20 lines of code (really easy to understand!)
  • No need of root access or any installations to the remote host

For instance:

$> echo "alias q=exit" > ~/.config/kyrat/bashrc
$> kyrat [email protected]
myserver.com $> q
exit

Upvotes: 0

glenn
glenn

Reputation: 1

I wrote an extremely simple tool for this that will allow you to natively transport your .vimrc file whenever you ssh, by using SSHd built-in config options in a non-standard way.

No additional svn,scp,copy/paste, etc required.

It is simple, lightweight, and works by default on all server configurations I have tested so far.

https://github.com/gWOLF3/viSSHous

Upvotes: 0

Brad Parks
Brad Parks

Reputation: 72201

Here's a simple bash script I've used for this purpose. It syncs over some folders I like to have copied over using rsync and then adds the ~/bin folder to the remote machines .bashrc if it's not there already. It works best if you have have copied your ssh keys to each server. I use this approach instead of a "dotfiles repo" as lots of the servers I connect to don't have git on them.

So to use it, you'd do something like this:

./bin_sync_to_machine.sh server1

bin_sync_to_machine.sh

function show_help()
{
  echo ""
  echo "usage: SERVER {SERVER2 SERVER3 etc...}"
  echo ""

  exit
}

if [ "$1" == "help" ]
then
  show_help
fi

if [ -z "$1" ]
then
  show_help
fi

# Sync ~/bin and some dot files to remote server using rsync
for SERVER in $*; do
  rsync -avrz --progress ~/bin/ -e ssh $SERVER:~/bin     
  rsync -avrz --progress ~/.vim/ -e ssh $SERVER:~/.vim
  rsync -avrz --progress ~/.vimrc -e ssh $SERVER:~/.vimrc
  rsync -avrz --progress ~/.aliases $SERVER:~/.aliases
  rsync -avrz --progress ~/.aliases $SERVER:~/.bash_aliases

  # Ensure remote server has ~/bin in the path
  ssh $SERVER '~/bin/path_add_to_path.sh'
done

path_add_to_path.sh

pathadd() {
    if [ -d "$1" ] && [[ ":$PATH:" != *":$1:"* ]]; then
        PATH="${PATH:+"$PATH:"}$1"
    fi
}

# Add to current path if running in a shell
pathadd ~/bin

# Add to ~/.bashrc 
if ! grep -q PATH:~/bin ~/.bashrc; then 
  echo "PATH=\$PATH:~/bin" >> ~/.bashrc
fi
if ! grep -q source ~/.aliases ~/.bashrc; then 
  echo "source ~/.aliases" >> ~/.bashrc
fi

Upvotes: 0

pynexj
pynexj

Reputation: 20798

I do this kind of things every day. I have about 15 bash rc files and .vimrc, a few vim plugin scripts, .screenrc and some other rc files. I have a sync script (written in bash) which uses the cool rsync command to sync all these files to remote servers. Every time I update some files on my main server, I would call the script to sync them to remote servers.

Setting up a svn/git/hg repository on the main server also works for me but my remote servers need to be repeatedly reinstalled for testing. So I find it's more convenient to use rsync.

A few years ago I also used the rdist tool which can also meet the requirement for most of the time. But now I prefer rsync as it supports incremental sync which is very efficient.

Upvotes: 2

thom_nic
thom_nic

Reputation: 8143

I just came across two alternatives to just doing a git clone of your dotfiles. I take no credit for either of these and can't say I've used either extensively so I don't know if there are pitfalls to either of these.

sshrc

sshrc is a tool (actually just a big bash function) that copies over local rc-files without permanently writing them to the remove user's $HOME - the idea being that might be a shared admin account that other people use. Appears to be customizable for different remote hosts as well.

.ssh/config and LocalCommand

This blog post suggests a way to automatically run a command when you login to a remote host. It tars and pipes a set of files to the remote, then un-tars them on the remote's $HOME:

Your local ~/.ssh/config would look like this:

Host *
   PermitLocalCommand yes
   LocalCommand tar c -C${HOME} .bashrc .bash_profile .exports .aliases .inputrc .vimrc .screenrc \
               | ssh -o PermitLocalCommand=no %n "tar mx -C${HOME}"

You could modify the above to only run the command on certain hosts (instead of the * wildcard) or customize for different hosts as well. There might be a fair amount of duplication per host with this method - although you could package the whole tar c ... | ssh .. "tar mx .." into a script maybe.

Note the above looks like it clobbers the same files on the remote when you connect, so use with caution.

Upvotes: 16

Chas. Owens
Chas. Owens

Reputation: 64939

You could always just copy the files to the machine before connecting with ssh:

#!/bin/bash

scp ~/.bash_profile ~/.vimrc user@host:
ssh user@host

This works best if you are using keys to login and no one else logs in as that user.

Upvotes: 1

ramanujan
ramanujan

Reputation: 5771

Use a dotfiles.git repo

What I do is keep all my config files in a dotfiles.git on a central server.

You can set it up so that when you ssh into a remote machine, you automatically pull the latest version of the dotfiles. I do something like this:

ssh myhost
cd ~/dotfiles
git pull --rebase
cd ~
ln -sf dotfiles/$username/linux/.* . 

Note:

  1. To put that in a shell script, you can automate the process of executing commands on a remote machine by piping to ssh.

  2. The "$username" is there so that you can share your config files with other people you're working with.

  3. The "ln -sf" creates symbolic links to all your dotfiles, overwriting any local ones, such that ~/.emacs is linked to the version controlled file ~/dotfiles/$username/.emacs.

  4. The use of a "linux" subdirectory is just to allow for configuration changes across platforms. I also have a mac directory under dotfiles/$username/mac. Most of the files in the /mac directory are symlinked from the linux directory as it's very similar, but there are some exceptions.

  5. Finally, note that you can make this even more sophisticated with hostnames and the like rather than just a generic 'linux'. With a dotfiles.git, you can also raid dotfiles from your friends, which is awesome -- everyone has their own set of little tricks and hacks.

Upvotes: 10

DragonFax
DragonFax

Reputation: 4805

ssh can be configured to pass certain environment variables through to the other (remote side). And since most shells will check some environment variables for additional settings to apply, you can hack that into applying some local settings remotely. But its a bit complicated and most administrators turn off the ssh environment variable pass-through in the sshd config anyways.

Upvotes: 1

NickZoic
NickZoic

Reputation: 7835

Well, no, because as Andy Lester says, the remote machine is the one doing the work, and it has no access back to your local machine to get .vimrc ...

On the other hand, you could use sshfs to mount the remote file system locally and edit the files locally. This doesn't require you to install anything on the remote machine. Not sure how efficient it is, maybe not great for editing big files over slow links.

Or Komodo IDE has a neat "Open >> Remote File" option which lets you edit files on remote machines by scping them back and forth automatically.

Upvotes: 2

Andy Lester
Andy Lester

Reputation: 93765

No, because it's not SSH using your config files, but the remote shell.

I suggest keeping your config files in Subversion or some other VCS. Here's how I do it.

Upvotes: 4

Related Questions