• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Bring your .bashrc, .vimrc, etc. with you when you ssh

sshrc was originally written by Russell Stewart (Russell91), who left GitHub, and hasn't responded to requests to appoint a new maintainer. This is a maintained continuation of his work.

Usage

sshrc works just like ssh, but it also sources the ~/.sshrc on your local computer 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. This makes sshrc very useful if you share a server with multiple users and can't edit the server's ~/.bashrc without affecting them, or if you have several servers that you don't want to configure independently.

Installation

OS X

$ brew install sshrc

Ubuntu (12.04 or 14.04+)

$ sudo add-apt-repository ppa:russell-s-stewart/ppa
$ sudo apt-get update
$ sudo apt-get install sshrc

Everything else

$ wget https://raw.githubusercontent.com/Russell91/sshrc/master/sshrc && 
chmod +x sshrc && 
sudo mv sshrc /usr/local/bin #or anywhere else on your PATH

Advanced configuration

Your most import configuration files (e.g. vim, inputrc) may not be bash scripts. Put them in ~/.sshrc.d and sshrc will copy them to a (guaranteed) unique folder in the server's /tmp directory after login. You can find them at $SSHHOME/.sshrc.d. You can usually tell programs to load their configuration from the $SSHHOME/.sshrc.d directory by setting the right environment variables. Putting too much data in ~/.sshrc.d will slow down your login times. If the folder contents are > 64kB, the server may block your sshrc attempts.

Vim

$ mkdir -p ~/.sshrc.d
$ echo ':imap <special> jk <Esc>' >> ~/.sshrc.d/.vimrc
$ cat << 'EOF' >> ~/.sshrc
export VIMINIT="let \$MYVIMRC='$SSHHOME/.sshrc.d/.vimrc' | source \$MYVIMRC"
EOF
$ sshrc me@myserver
$ vim # jk -> normal mode will work

If you want to load your .vim folder as well, you can 1) put the .vim folder in ~/.sshrc.d, 2) move your .vimrc into the .vim folder, 3) edit the path above to reflect the new .vimrc location, and 4) add the following lines at the top of the moved .vimrc, which will notify vim of the .vim folder location:

" set default 'runtimepath' (without ~/.vim folders)
let &runtimepath = printf('%s/vimfiles,%s,%s/vimfiles/after', $VIM, $VIMRUNTIME, $VIM)
" what is the name of the directory containing this file?
let s:portable = expand('<sfile>:p:h')
" add the directory to 'runtimepath'
let &runtimepath = printf('%s,%s,%s/after', s:portable, &runtimepath, s:portable)

Tmux

If you use tmux frequently, you can make sshrc work there as well. The following seems complicated, but hopefully it should just work.

$ cat << 'EOF' >> ~/.sshrc
alias foo='echo I work with tmux, too'

tmuxrc() {
    local TMUXDIR=/tmp/russelltmuxserver
    if ! [ -d $TMUXDIR ]; then
        rm -rf $TMUXDIR
        mkdir -p $TMUXDIR
    fi
    rm -rf $TMUXDIR/.sshrc.d
    cp -r $SSHHOME/.sshrc $SSHHOME/bashsshrc $SSHHOME/sshrc $SSHHOME/.sshrc.d $TMUXDIR
    SSHHOME=$TMUXDIR SHELL=$TMUXDIR/bashsshrc /usr/bin/tmux -S $TMUXDIR/tmuxserver $@
}
export SHELL=`which bash`
EOF
$ sshrc me@myserver
$ tmuxrc
$ foo
I work with tmux, too

The -S option will start a separate tmux server. You can still safely access the vanilla tmux server with tmux. Tmux servers can persist for longer than your ssh session, so the above tmuxrc function copies your configs to the more permenant /tmp/russelltmuxserver, which won't be deleted when you close your ssh session. Starting tmux with the SHELL environment variable set to bashsshrc will take care of loading your configs with each new terminal. Setting SHELL back to /bin/bash when you're done is important to prevent quirks due to tmux sessions having a non-default SHELL variable.

Specializing .sshrc to individual servers

You may have different configurations for different servers. I recommend the following structure for your ~/.sshrc control flow:

if [ $(hostname | grep server1 | wc -l) == 1 ]; then
    echo 'server1'
fi
if [ $(hostname | grep server2 | wc -l) == 1 ]; then
    echo 'server2'
fi

Tips

  • I don't recommend trying to throw your entire .vim folder into ~/.sshrc.d. It will more than likely be too big.

  • You can avoid duplication of dotfiles using symlinks (e.g. $ cd ~/.sshrc.d && ln -s ../.tmux.conf .tmux.conf/ ).

  • For larger configurations, consider copying files to an obscure folder on the server and using ~/.sshrc to automatically source those configurations on login.

  • To enable tab completion in zsh, add compdef sshrc=ssh to your .zshrc file:

More Repositories

1

clipmenu

Clipboard management using dmenu
C
1,060
star
2

srt

A simple library and set of tools for parsing, modifying, and composing SRT files.
Python
430
star
3

yturl

YouTube videos on the command line
Python
223
star
4

psi-notify

Alert when your machine is becoming oversaturated, *before* it slows to a crawl
C
218
star
5

clipnotify

Notify on new X clipboard events
C
216
star
6

tzupdate

Set the system timezone based on IP geolocation.
Rust
183
star
7

zcfan

A zero-configuration fan daemon for ThinkPads.
C
74
star
8

ansible-desktop

Ansible playbook for my personal machines.
Shell
70
star
9

dwm

Dynamic window manager for X (with extensive customisation)
C
53
star
10

passmenu

dmenu interface to the pass password manager
43
star
11

mack

An opinionated, fast music organiser.
Rust
36
star
12

dotfiles

My configuration files
Shell
28
star
13

gh-mirror

Mirror all GitHub repositories for a user, maintaining metadata.
Python
26
star
14

mpdmenu

dmenu frontend to MPD.
Shell
25
star
15

openvz-to-lxc

Script to migrate OpenVZ containers to LXC.
Shell
23
star
16

ansible-aur

An Ansible module for the pacaur and yaourt package managers.
Python
22
star
17

nota

Simple note taking using your $EDITOR and git
Shell
22
star
18

aur

Arch User Repository API interface.
Python
21
star
19

mac-cel

Remove mouse acceleration on Mac OSX
C
21
star
20

btmenu

Control bluetooth devices with dmenu
Shell
18
star
21

cellout

Battery status in the console (Linux only).
Awk
16
star
22

xinput-toggle

A simple xinput toggler for X11 systems
Shell
15
star
23

travis-automerge

A script to automerge successful builds to a branch
Shell
15
star
24

rebuild-debian-iso

Rebuild a Debian ISO with preseed/custom files.
Shell
10
star
25

osmo

Digital signage for minimalists.
JavaScript
8
star
26

geoip-http

GeoIP lookup server over HTTP.
Rust
7
star
27

exifrename

Rename image files based on EXIF data.
Rust
7
star
28

chrisdown.name

The files behind chrisdown.name.
CSS
6
star
29

mkdirmutex

A deadlock-resistant mkdir based mutex
Shell
6
star
30

pinyin

A small library to manipulate Hanyu Pinyin.
Python
5
star
31

gmute

Gmail mute support for mutt and other email clients
Python
5
star
32

battery-notify

A simple battery notifier for Linux.
Rust
4
star
33

stopthread

A small program to stop individual threads from executing, instead of the whole thread group.
C
4
star
34

icopng

Directly embed PNGs into ICO files without reencoding.
Rust
3
star
35

rtorrent-cookbook

Chef cookbook to set up an rtorrent service inside tmux.
Ruby
3
star
36

habitual

Send notifications for things on a fixed, repeated schedule
Python
3
star
37

pkgbuilds

AUR packages
Shell
3
star
38

kernel-misc

Tools and other things from kernel development
Shell
3
star
39

komootgpx

Extract a GPX from any Komoot route
Rust
3
star
40

bats

Simple battery status tool
Shell
2
star
41

arch-install

A base install script for Arch Linux (base only, the rest is done by Ansible).
Shell
2
star
42

rym-export

Python
2
star
43

multidate

Print multiple timezones' dates/times with offsets from the local time.
Rust
2
star
44

filestruct

Derive macro to read from a directory into a Rust struct.
Rust
2
star
45

cargo-which-nightly

Find the latest Rust nightly with combinations of features present.
Rust
2
star
46

linux

upstream, with my dev branches
C
1
star
47

torrentdiff

Compare files encapsulated by torrents in a directory to the actual files on disk
Python
1
star
48

ahk_scripts

AutoHotkey scripts to make Windows life more tolerable
AutoHotkey
1
star
49

whensthebus

Get live UK bus times on stdout
Python
1
star
50

todobg

Todo list on X root window
Shell
1
star
51

seedbox-cookbook

Chef cookbook to create a minimal seedbox
Ruby
1
star
52

funcfmt

A simple templating crate that allows passing in custom data to functions through function traits.
Rust
1
star
53

checkupdates-cron

A tiny wrapper around the Arch checkupdates script for use in a cronjob.
Shell
1
star
54

jingui

A simple password manager designed to adhere to the Unix philosophy.
Python
1
star
55

prompter

Simple and/or dumb work prompter
Shell
1
star
56

imurl

Downloads the images from imgur albums.
Python
1
star
57

toggledesktop

Isolate windowed games without showing the desktop
AutoHotkey
1
star
58

rawtojpg

Fast JPEG extractor from RAW files
Rust
1
star