ssh / scp / rsync¶
there's a lot of stuff you can do!
- see also rsync
ssh¶
add ssh-key to ssh-agent¶
motd (message of the day) for user¶
create this file with whatever you want to execute when logging in as the user:
the file rc
will be executed during login, this means if you want to print something out you need to echo
the contents, like this:
ssh error commandline disabled¶
when trying to open an interactive shell with ~C
. add this to your ~/.ssh/config
for the host
or use -o EnableEscapeCommandline=yes
with the ssh
command.
ssh jumphost¶
this is quite new in openssh, use -J
to connect to your target host through (multiple) jump host(s):
ssh -J user@first-hop user@final-destination # one hop
ssh -J user@first-hop,user@second-hop user@final-destination # two hops to get to 3rd server
ssh -J user@first-hope,user@second-hop:22022 user@final-destination -p2222 # two hops, second hop with custom port, third one as well (check for the : and -p difference... wtf ssh what am I doing wrong?)
ssh -J user@first-hope,user@second-hop:22022 user@final-destination -p2222 -L8384:localhost:8384 # same as above just with port forwarding from the last hop!
use a jumphost in ~/.ssh/config
example: I want to run ssh nibbler
on my local machine and connect through bender
to it. nibbler is using autossh to connect to bender (I hope this makes sense, future Jonas):
Host bender
User local-user
Hostname bender.hostname.com
Host nibbler
User root
Hostname localhost
Port 2222
ProxyJump bender
socks proxy¶
create a socks proxy, route traffic using the proxy encrypted through your destination host:
now configure your browser/client to use localhost
with port 8080
using a socks5 proxy.
to drop the whole thing into the background:
if you put it in the background you might want to consider autossh (see autossh.md
).
limit ssh connection to creating a tunnel¶
put this in your server's ~/.ssh/authorized_keys
file:
then, create a tunnel like shown above.
add / remove passphrase from key¶
forward credentials to host¶
if you need to login through a middle man it might come in handy:
interactive ssh session¶
forward a port then, for example:
profit:
restrict agent, x11, port forwarding in ssh for clients¶
there's a handy way to restrict all of the above and more with a single option: restrict
, use it like so in your ~/.ssh/authorized_keys
file
git-shell¶
limit shell (git-shell) to certain users¶
use-case: you have one user on a linux system and multiple applications / users ssh'ing in. I want to limit the public ssh key of my phone's git client to only be able to use git-shell
and not be able to login with bash or zsh.
- put git-shell in
/etc/shells
- as described here
cat /etc/shells # see if `git-shell` is already in there. If not...
which git-shell # make sure git-shell is installed on your system.
sudo -e /etc/shells # and add the path to git-shell from last command
- limit the public ssh key in
authorized_keys
to only run git-shell
this one key can only use git-shell commands now, like git clone
but not execute bash or something like that.
ssh-keygen¶
convert ssh key from openssh format to RFC4716:
scp / rsync¶
scp between two servers¶
copy files between two servers, use the system executing the command as the connection in between.
scp -3 between two systems with different ports¶
scp /rsync through extra hosts (middleman)¶
we'll connect to the middleman with the -J
flag and custom port 1337
and connect to your dest
using port 2222
rsync exclude muliple folders / directories¶
… m( - exclude node_modules
and dist
folder from rsync
limit rsync to only allow downloading / pulling data¶
for this, you need to use rrsync
, it's a script usually part of the rsync
package and can be found in /usr/share/doc/rsync/scripts
on ubuntu/debian, but also directly on the web.
unpack it, put it into your local bin directory, or somewhere else:
restrict rsync for specific ssh keys to only allow pulling from ~/downloads
, this downloads folder will also be the new entry point for the clients to rsync. so if they pull from ~/
, it'll be the downloads folder.
put this in your ~/.ssh/authorized_keys
file:
poor man's ngrok or make-my-dev-machine-available-from-outside¶
- enable
GatewayPorts
in your sshd config:
- use the
bind_address
feature in ssh to open up the port on the remote machine. we're just going to useautossh
here. so log in to your source machine and executeautossh
like this:
autossh -M 0 -q -f -N -o "ConnectTimeout 10" -o "ServerAliveCountMax 3" -o "ServerAliveInterval 60" -o "Port=22022" -o "ExitOnForwardFailure=yes" -R2224:localhost:22 tunnel@target-server.com
important bit from the man page:
An empty bind_address, or the address
*'
- maybe you need to open up port
2224
(my example) in your firewall on the target-server as well and then you can just connect to your target server using port 2224 like this:
enjoy!
do not offer public keys to server¶
for reasons.