Guide to Working with Remote Servers

Practical Bash commands for connecting to, transferring files with, and packaging data on remote servers.

1. Connect via SSH

Specify the user, host name, and port to open the session.

ssh admin@192.168.32.130 -p 22

The -p flag sets the remote port (the default is 22).

If you still need to configure an SSH service, see Creación de un Servidor SSH.

2. Test an SSH Connection

You can test the connection by appending a remote command.

ssh admin@192.168.32.130 -p 22 whoami
admin@192.168.32.130's password:
admin

When your SSH key is trusted, authentication completes without prompting for a password.

ssh admin@192.168.32.130 -p 22 whoami
admin

If you need to generate a key pair, follow Creación de llaves SSH.

3. Copy Files with SCP

Move files or folders between your machine and a remote server with scp.

Copy a remote file to the local machine.

scp -P 22 user@host:/backup.tar.gz backup.tar.gz

Push a local file to the server.

scp -P 22 backup.tar.gz user@host:/backup.tar.gz

Sync entire directories (use the recursive -r flag).

scp -P 22 -r user@host:/folder folder
scp -P 22 -r folder user@host:/folder

Options:

OptionDescription
-PPort exposed by the SSH server.
-rRecursive copy of subdirectories and files.

You can also transfer between two remotes; the -3 option routes traffic through your local machine.

scp -3 \
  user1@host1:/original.tar.gz \
  user2@host2:/copy.tar.gz

4. File Compression

Create and extract *.tar.gz archives.

tar -czvf project.tar.gz /path/to/project
tar -xzvf project.tar.gz

Work with *.zip archives.

zip -r carpeta.zip /ruta/de/la/carpeta
unzip carpeta.zip

If your distro doesn't include zip support, install the unzip package.

sudo apt-get install unzip

References

Published: June 1, 2020