site/wiki/pages/advanced-ssh.md

97 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

2018-06-04 06:18:28 +00:00
---
2018-08-09 14:28:14 +00:00
author: ~ubergeek
published: true
2018-06-04 06:18:28 +00:00
title: advanced ssh
2018-08-09 14:28:14 +00:00
description: advanced ssh tricks
2022-09-29 18:53:09 +00:00
category:
- technical
- guides
2018-06-04 06:18:28 +00:00
---
2018-08-09 12:18:22 +00:00
Some more advanced ssh topics.
## SSH Tunnels
2022-09-29 18:53:09 +00:00
SSH can be used as a sort of "poor man's VPN". For example, you want to get into IRC with your local client (mIRC,
Weechat, etc), but your local network blocks IRC ports.
2018-08-09 12:18:22 +00:00
2022-09-29 18:53:09 +00:00
However! Your local network will almost always allow SSH (sysadmins need this for most day to day work). You can connect
to tilde.team, and use port forwarding to get on.
2018-08-09 12:18:22 +00:00
If you are connecting from a Linux machine, you can do this:
2022-09-29 18:53:09 +00:00
2018-08-09 14:28:14 +00:00
```
2018-08-09 12:18:22 +00:00
ssh -L 6667:localhost:6667 tilde.team
2018-08-09 14:28:14 +00:00
```
2018-08-09 12:18:22 +00:00
2022-09-29 18:53:09 +00:00
After being logged in, open your local IRC client, and use 127.0.0.1:6667 for your server setting. Voila! You're now on
team's IRC server.
2018-08-09 12:18:22 +00:00
2022-09-29 18:53:09 +00:00
What that SSH command did was open a local port tunnel (-L), using local port 6667 (6667:) pointed at localhost (From
the remote's point of view), on remote port 6667 (Default IRC port).
2018-08-09 12:18:22 +00:00
Putty has the same ability (For Windows and Mac users), under Connection --> SSH --> Tunnels.
You can do this for any arbitrary port.
## File Copying
2022-09-29 18:53:09 +00:00
What if you don't want to edit files on the team server, but instead, you want to create it on your local machine? You
don't want to have to copy/paste or re-type all of that, right?
2018-08-09 12:18:22 +00:00
2020-06-14 06:53:56 +00:00
SCP to the rescue! SCP copies files over the ssh protocol. It works just like the cp command, but allows you to do this:
2018-08-09 12:18:22 +00:00
2018-08-09 14:28:14 +00:00
`scp MyNewFileILove.txt tilde.team:~/AhYesOnTheServerNow.txt`
2018-08-09 12:18:22 +00:00
2018-08-09 14:28:14 +00:00
As long as you can ssh, you can copy files to and from the remote side. It also works like this, to download a file:
2018-08-09 12:18:22 +00:00
2018-08-09 14:28:14 +00:00
`scp tilde.team:~/CrapINeedThisFileLocally.js ./AwesomeLocalJSFile.js`
2018-08-09 12:18:22 +00:00
## Mount tilde folders on your machine using sshfs
2022-09-29 18:53:09 +00:00
But what if manually downloading files, editing them, and uploading them again is too tedious? Wouldn't it be great to
just be able to edit a file on tilde.team from your home terminal?
With sshfs, you can mount a remote folder on your computer, and access it as if it were a local folder.
Refer to your distribution's package manager on how to install sshfs.
2022-09-29 18:53:09 +00:00
Once sshfs is installed, you can mount any folder on tilde.team to any folder on your machine. This example mounts your
homefolder to `/tmp/tilde`:
`mkdir -p /tmp/tilde && sshfs USERNAME@tilde.team:/home/USERNAME /tmp/tilde`
You can unmount the tilde from your machine by running `umount` on the directory you mounted the directory to:
`umount /tmp/tilde`
2018-08-09 12:18:22 +00:00
## Remote execution
2018-08-09 14:28:14 +00:00
What if you don't want to really log into team.tilde, but you just need to run a command. You can do that too, with ssh!
2018-08-09 12:18:22 +00:00
2018-08-09 14:28:14 +00:00
`ssh tilde.team ping google.com`
2018-08-09 12:18:22 +00:00
2022-09-29 18:53:09 +00:00
The above executes the ping command from the server side of the house. The one thing you need to be careful of here are
quotes and input redirection. It can have surprising affects, mixing remote and local pipes.
2018-08-09 12:18:22 +00:00
## SSH config
2022-09-29 18:53:09 +00:00
2018-08-09 14:28:14 +00:00
Each user has their own, personal configuration for ssh. The configuration files lives at `~/.ssh/config.`
2018-08-09 12:18:22 +00:00
A very common thing to do in this file is to create hosts aliases.
2018-08-09 14:28:14 +00:00
```
2018-08-09 12:18:22 +00:00
host tilde
hostname tilde.team
user ubergeek
2018-08-09 14:28:14 +00:00
LocalForward localhost:6667 localhost:6667
```
2022-09-29 18:53:09 +00:00
There are tons of other options, including this LocalForward line to automatically set up the tunnel as
show [above](#ssh-tunnels).
2018-08-09 14:28:14 +00:00
For more information about the available options, check the man page: `man ssh_config`.
2018-08-09 12:18:22 +00:00
2022-09-29 18:53:09 +00:00
You can set this up remotely, to make jumping to other hosts easier, or locally (If supported in your ssh setup) to make
connecting easier for you.