Skip to content

SSH

Published:

Remote login

Simple login

To gain access to a remote server via SSH, you can identify yourself with a username. Then, you can use a password or an SSH key to verify your identity.

The first time you connect to a server, you will receive a warning message asking you to accept the server’s public key.

ssh <user>@<host>
# E.g.
# ssh user@192.168.1.1
# ssh user@remote.host

Running remote commands

If the goal is to run a simple command, you can run it directly without opening an interactive session.

ssh <user>@<host> <command>
# E.g.
ssh user@remote.host ls -la

If the command requires administrator privileges, you can use sudo to run the command, provided you add the -t option to force the allocation of a terminal.

ssh -t <user>@<host> sudo <command>
# E.g.
ssh -t user@remote.host sutp apt-get upgrade

Interacting with the server

Copying files from local to remote

scp <file> <user>@<host>:<path>
# E.g.
scp file.txt user@remote.host:/tmp

Copying files from remote to local

scp <user>@<host>:<path> <file>
# E.g.
scp user@remote.host:/tmp/file.txt .

Port forwarding

Feature that allows you to access remote services if they are blocked or not directly accessible. Requests forwarded to the local machine at the indicated port (local-port) will be forwarded to the desired address (remote-host:remote-port) by the remote machine.

ssh -L <local-port>:<remote-host>:<remote-port> <user>@<host>
# E.g.
ssh -L 8080:localhost:80 user@remote.host
Loading diagram...
Loading diagram...

Reverse port forwarding

Feature that allows you to expose local services to a remote machine. Requests forwarded to the remote machine at the indicated port (remote-port) will be forwarded to the desired address (local-host:local-port) by the local machine.

ssh -R <remote-port>:<local-host>:<local-port> <user>@<host>
# E.g.
ssh -R 8080:localhost:80 user@remote.host
Loading diagram...
Loading diagram...