Vai al contenuto

SSH

Pubblicato:

Remote login

Simple login

It is possible to access a remote server via SSH, identifying oneself with a username. To verify one’s identity, one can use a password or an SSH key.

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 root@192.168.1.1
# ssh user@remote.host

Running remote commands

If the task to be performed is simple, you can run the remote command directly without opening an interactive session.

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

If administrator privileges are required, 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 root@remote.host:/tmp

Copying files from remote to local

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

Port forwarding

Useful to access remote services if they are blocked or not directly accessible. Requests sent 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 root@remote.host
Loading diagram...
Loading diagram...

Reverse port forwarding

Useful to expose local services to a remote machine. Requests sent 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 root@remote.host
Loading diagram...
Loading diagram...