Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using local settings through SSH

Tags:

shell

unix

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.

like image 447
Verhogen Avatar asked Apr 19 '09 06:04

Verhogen


People also ask

How does SSH config file work?

The contents of the SSH client config file is organized into stanzas (sections). Each stanza starts with the Host directive and contains specific SSH options used when establishing a connection with the remote SSH server. Indentation is not required but is recommended since it makes the file easier to read.

What is SSH config?

~/. ssh/config or $HOME/. ssh/config – is the user-specific/custom configuration file. It has configurations that apply to a specific user. It therefore overrides default settings in the system-wide config file.

Where do I put SSH config?

The ssh program on a host receives its configuration from either the command line or from configuration files ~/. ssh/config and /etc/ssh/ssh_config .


4 Answers

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.

like image 89
thom_nic Avatar answered Oct 15 '22 22:10

thom_nic


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.

like image 36
ramanujan Avatar answered Oct 15 '22 22:10

ramanujan


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.

like image 23
Andy Lester Avatar answered Oct 15 '22 23:10

Andy Lester


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.

like image 33
NickZoic Avatar answered Oct 16 '22 00:10

NickZoic