Remote Working
The ability to work remotely is a critical skill for software engineers. The shell provides essential tools for connecting to remote systems, transferring files, and managing remote resources. This section covers key commands for remote work.
Important Note: To practice these commands (except wget
and tar
), you’ll need access to a remote server. This usually involves having an account on a remote machine and knowing its IP address or hostname.
tar
(Tape Archive)
- Description:
tar
is a versatile utility for creating and extracting archives (often called “tarballs”). It’s commonly used for compressing and bundling files for transfer or backup. It does not compress files by default. -
Example Usage:
tar -cvf archive.tar directory/ # Create an archive named 'archive.tar' containing 'directory/' tar -xvf archive.tar # Extract the contents of 'archive.tar' tar -czvf archive.tar.gz directory/ # Create a *compressed* archive (using gzip) tar -xzvf archive.tar.gz # Extract a *compressed* archive (using gzip) tar -cjvf archive.tar.bz2 directory/ # Create a compressed archive (using bzip2 - better compression, slower) tar -xjvf archive.tar.bz2 # Extract a compressed archive (using bzip2) tar -tvf archive.tar # List the contents of an archive (without extracting)
Key Options:
-c
: Create an archive.-x
: Extract an archive.-v
: Verbose mode (show files being processed).-f FILENAME
: Specify the archive filename. This must come last in the option list.-z
: Use gzip compression (.tar.gz
or.tgz
).-j
: Use bzip2 compression (.tar.bz2
or.tbz2
).-t
: List archive contents.
wget
- Description: Downloads files from the internet using HTTP, HTTPS, and FTP.
-
Example Usage:
wget [https://example.com/file.zip](https://example.com/file.zip) # Download 'file.zip' wget -O new_name.zip [https://example.com/file.zip](https://example.com/file.zip) # Download and save as 'new_name.zip' wget -c [https://example.com/large_file.zip](https://www.google.com/search?q=https://example.com/large_file.zip) # Resume a partially downloaded file wget --user=username --password=password [https://example.com/private_file.txt](https://www.google.com/search?q=https://example.com/private_file.txt) # Download with authentication
Key Options:
-O FILE
: Save the downloaded file as FILE.-c
: Continue a partially downloaded file.--user
and--password
: Provide credentials for HTTP authentication (use with caution - consider using a.netrc
file for storing credentials securely).
ssh
(Secure Shell)
- Description: Securely connects to a remote system’s shell.
ssh
encrypts all traffic between your local machine and the remote server, making it safe to use even over untrusted networks. -
Example Usage:
ssh username@remote_host # Connect to 'remote_host' as 'username' (you'll be prompted for a password) ssh -p 2222 username@remote_host # Connect to port 2222 (the default SSH port is 22) ssh -i my_key.pem username@remote_host # Connect using an SSH key (more secure than passwords) ssh user@host 'ls -l' # run a command remotely
Key Options:
-p PORT
: Specify the port number to connect to.-i IDENTITY_FILE
: Use an SSH key file (private key) for authentication. This is much more secure than using passwords.-L local_port:remote_host:remote_port
: Creates a local port forward.-R local_port:remote_host:remote_port
: Creates a remote port forward.-X
: Enables X11 forwarding (allows you to run graphical applications on the remote server and display them locally).
Generating SSH Keys:
ssh-keygen -t rsa -b 4096 # Generate a new RSA key pair (recommended) # You'll be prompted for a file to save the key in (default is ~/.ssh/id_rsa) and a passphrase (highly recommended). # This creates two files: id_rsa (private key - keep it secret!) and id_rsa.pub (public key). ssh-copy-id username@remote_host # Copy your *public* key to the remote server (you'll be prompted for the password)
Note: After using
ssh-copy-id
, you should be able tossh
to the remote server without entering a password (as long as you’ve entered the passphrase for your key, if you set one).
scp
(Secure Copy)
- Description: Securely copies files between your local machine and a remote machine (or between two remote machines).
scp
usesssh
for secure data transfer. -
Example Usage:
scp username@remote_host:/path/to/remote/file.txt /local/path/ # Copy *from* the remote server *to* your local machine scp /local/path/to/local_file.txt username@remote_host:/path/to/remote/ # Copy *from* your local machine *to* the remote server scp -r username@remote_host:/path/to/remote/directory /local/path/ # Recursively copy a directory (-r) scp -P 2222 username@remote_host:/path/to/file.txt /local/path/ # Use a non-standard SSH port (-P, capital P) scp -i my_key.pem username@remote_host:/path/to/file.txt /local/path/ # Use an SSH key
Key Options:
-r
: Recursively copy directories.-P PORT
(uppercase P): Specify the SSH port number.-i IDENTITY_FILE
: Use an SSH key file.-p
: Preserves modification times, access times, and modes from the original file.