🟤SSH Tricks
Run Single Command
SSH without input password
Diff local file with remote one
Diff two remote SSH files
SCP with timestamps/permissions kept
Ansible
This is actually the basis of how some server provisioning tools work. In fact, Ansible is very similar - it will run commands over SSH on groups of servers (in series and in parallel).
Let's see how that works on Ubuntu really quickly.
First install Ansible on a server that will be doing provisioning (not the one being provisioned):
Then, configure one or more servers in the /etc/ansible/hosts
directory:
Save that file and then let's run a command on all three servers!
This will run "ping" on each server. You'll get some JSON output saying if they were successful or not.
The flags of that command:
-k
- Ask for passwordall
- All servers configured in/etc/ansible/hosts
-m ping
- Use the ping module-u vagrant
- Login with user "vagrant", which will work if the hosts defined are other vagrant servers. Change the username as needed.
You can actually run any command using the "shell" module:
Here, the -a "apt-get install nginx
will run the given command using the "shell" module.
SSH control sequences
When in an SSH connection, press the tilde character (shift + backtick) followed by another character to use the control sequence options (shift + ~). to get help about commands press ? after ~.
Supported escape sequences:
(Note that escapes are only recognized immediately after newline.
Mounting Filesystems
NFS, while venerable, is pretty shoddy when it comes to securely sharing directories. You can share to an entire subnet, but anything beyond that gets tricky. Enter sshfs, the ssh filesystem.
I’ve got a machine called wonka
trying to share the directory /srv/factory
with the host bucket
. Assuming that the user charlie
on bucket
has keys set up on wonka
, and assuming that I’ve got sshfs available (install it via your package manager of choice if which sshfs
returns nothing), try this out:
Everything is pretty self-explanatory except for the filesystem mount option idmap
. That just tries to map the unix numeric IDs from the server to the user you’re mounting the filesystem as (it’s the least troublesome option.)
And presto, you’re sharing a filesystem quickly, easily, and securely. The caveat here is that filesystem changes can take a little longer to propagate than with similar file-sharing schemes like SMB or NFS.
Remote File Editing
This trick relies on the netrw capabilities of vim (a directory browsing function) and scp (a sister program of ssh.)
Netrw allows vim to browse the filesystem on the local filesystem easily (try it in a terminal with $ vim [path]
, such as $ vim .
. However, we can pass vim a remote path using scp://
to achieve remote file editing:
You’re now browsing your home directory’s contents on wonka
within vim. Any file edits you make will be synced over on write via scp.
Accessing NAT-Ed Hosts Directly
ssh config files follow this syntax:
For example, you can reduce the command**:**
to
by adding the following to ~/.ssh/config
:
Now that we’ve dabbled in the config, look at this configuration stanza:
ProxyCommand
directs ssh how to connect to behind.bar
: connect to bar
(previously defined) and use netcat to connect to the port that ssh would normally connect to. ssh’s -W
flag intelligently forwards traffic to the interpolated %h (hostname) and %p (port) variable placeholders. The following diagram demonstrates.
you can essentially treat the NAT-ed host as if there were no intermediate hosts between the ssh client and daemon.
Sharing Connections
While you could negotiate another asymmetric ssh handshake, why not use your pre-existing connection to make speedier file copies or logins?
Use Controlpath
:
This means upon first connection to busyserver
, ssh will create a socket in ~/.ssh/
which can be shared by other commands. If you’re using commands like rsync or scp, this can make repetitive copy tasks much quicker.
Sidedoor
sidedoor maintains an SSH connection or tunnel with a shell script daemon.
Last updated