# SSH

# Local port forwarding

### Command explanation
To forward port of remote host use the following command
```sh
ssh -L <LOCAL_PORT>:<TUNNEL_IP>:<DESTINATION_PORT> <user>@<server_ip>
```
-L stands for local adress
because we are tunneling connection on local machine, TUNNEL_IP is 127.0.0.1

```sh
ssh -L <LOCAL_PORT>:127.0.0.1:<DESTINATION_PORT> <user>@<server_ip>
```

### Real life example
- I have a server with ip *69.69.69.69*
- I have user on a server called *gnumik*
- My public key is recognized by this server and I can establish ssh connection like this:
```ssh
ssh gnumik@69.69.69.69
```
- I have postgresql data base running on the server on port *5432*
- I want to have access to this postgre instance from my local machine using local port *5433*
- I need to execute the following command on my local machine:
```ssh
ssh -L 5433:127.0.0.1:5432 gnumik@69.69.69.69
```

***P.S.*** I use arch btw

# Скачивание и загрузка файлов по SSH

**Скачать с сервера**
```bash
scp -r -P 22 -i ~/.ssh/privkey user@192.168.1.7:/home/myuser/from "C:\localfolder"
```

**Залить на сервер**
```bash
scp -r -P 22 -i ~/.ssh/privkey "C:\localfolder" user@192.168.1.7:/home/myuser/to
```

В случае если нужно передать только один файл, уберите опцию `-r`

# SSH as proxy

**ssh-сервер в качестве прокси для браузера**
```bash
ssh -i ~/.ssh/privkey -p 22 myuser@192.168.1.7 -D 2141 -N

start chrome --proxy-server="socks5://localhost:2141"
```