SSH - CMD


Overview

The ssh command, short for Secure Shell, is a protocol used to securely connect and log into remote machines over a network. It enables secure system administration and file transfers over insecure networks, like the internet. The command is used primarily for managing servers, automating remote tasks, and executing commands on remote hosts without needing physical access.

Syntax

The basic syntax for the ssh command is:

ssh [options] user@hostname [command]
  • user: The username on the remote machine.
  • hostname: The address of the remote machine, which can be an IP address or a domain name.
  • command: Optional. A command to be executed on the remote machine.

Options/Flags

Here is a list of commonly used options in ssh:

  • -p <port>: Specifies the port to connect to on the remote host. The default is port 22.
  • -i <identity_file>: Specifies a file from which the identity (private key) for public key authentication is read.
  • -v, -vv, -vvv: Increase the verbosity of the output to help in debugging.
  • -C: Request compression of all data sent over the session.
  • -L [bind_address:]port:host:hostport: Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.
  • -X: Enables X11 forwarding.
  • -q: Quiet mode. Causes most warning and diagnostic messages to be suppressed.

Examples

Connecting to a Remote Server

ssh user@example.com

This will connect you to the server at example.com using the username user.

Running a Command Remotely

ssh user@example.com uname -a

This will execute the uname -a command on the remote host and display the system information.

Using a Specified Port

ssh -p 2222 user@example.com

Connect to example.com using port 2222 instead of the default SSH port 22.

Forwarding a Port

ssh -L 8080:127.0.0.1:80 user@example.com

This sets up local port forwarding from localhost’s port 8080 to port 80 on the remote host.

Common Issues

  • Permission Denied: Ensure the correct username and password are used, and that the user has appropriate rights.
  • Connection Timed Out: Verify the hostname and port number, ensure the server is reachable, and check for firewall restrictions.
  • Unable to authenticate using given keys: Check the format and permissions of the key files.

Integration

ssh can be used along with batch scripts or PowerShell to automate remote tasks. For example, using a simple batch file to collect system logs:

ssh user@server "cat /var/log/system.log" > local_system.log
  • scp: Securely copy files between hosts on a network.
  • sftp: Secure File Transfer Protocol, a secure file transfer program.
  • ssh-keygen: Generate, manage, and convert authentication keys for SSH.

For more detailed information, refer to the OpenSSH project or the man pages for specific commands.