Server Administration

The Definitive Guide to Linux SSH Command Line Tools

An SSH terminal session using Putty client

The Definitive Guide to Linux SSH Command Line Tools

Secure Shell (SSH) is a powerful protocol used for secure remote administration of Linux and Unix-like systems. This guide covers essential SSH command-line tools, their use cases, and advanced features to help you manage remote systems efficiently.

READ ALSO: Advanced SSH Techniques: A Deep Dive Into Secure Remote Access

  1. Basic SSH Commands

    1. Connecting to a Remote Server

      ssh user@remote_host
      

      This command establishes an SSH connection with the specified remote host. Replace user with the actual username and remote_host with the IP address or hostname of the remote server.

    2. Specifying a Port

      ssh -p 2222 user@remote_host
      

      If the SSH server is running on a non-default port (default is 22), use the -p flag to specify the correct port.

    3. Running a Remote Command

      ssh user@remote_host "ls -la"
      

      Executes ls -la on the remote server and returns the output.

    4. SSH Key-Based Authentication

      ssh-keygen -t rsa -b 4096
      ssh-copy-id user@remote_host
      

      Generating and copying SSH keys allows password-less authentication, improving security and convenience.

  2. File Transfer Over SSH

    1. Copying Files with SCP

      scp file.txt user@remote_host:/remote/path/
      scp -r directory/ user@remote_host:/remote/path/
      

      SCP (Secure Copy Protocol) allows secure file transfer between local and remote machines.

    2. Using SFTP

      sftp user@remote_host
      sftp> put localfile.txt
      sftp> get remotefile.txt
      

      SFTP (SSH File Transfer Protocol) provides an interactive mode for transferring files securely.

  3. SSH Tunneling and Port Forwarding

    1. Local Port Forwarding

      ssh -L 8080:localhost:80 user@remote_host
      

      Forwards local port 8080 to the remote server’s port 80, useful for accessing web services securely.

    2. Remote Port Forwarding

      ssh -R 9090:localhost:22 user@remote_host
      

      Allows remote users to connect to the local SSH service via port 9090 on the remote machine.

    3. Dynamic Port Forwarding

      ssh -D 1080 user@remote_host
      

      Creates a SOCKS proxy on port 1080, useful for browsing securely through the SSH tunnel.

  4. Managing SSH Sessions

    1. Keeping SSH Sessions Alive

      ssh -o ServerAliveInterval=60 user@remote_host
      

      Prevents SSH connections from timing out due to inactivity.

    2. Using Multiplexing to Speed Up Connections

      ssh -M -S ~/.ssh/controlmastersocket -o ControlPersist=10m user@remote_host
      

      Allows multiple SSH connections to the same host without re-authenticating.

    3. Backgrounding an SSH Session

      ssh -f user@remote_host -N
      

      Runs SSH in the background without executing a command, useful for port forwarding.

  5. Advanced SSH Configuration

    1. Configuring ~/.ssh/config

      Customize SSH behavior per host:

      Host myserver
          HostName remote_host
          User user
          Port 2222
          IdentityFile ~/.ssh/id_rsa
      

      This simplifies SSH connections by allowing ssh myserver instead of typing full details each time.

    2. Enabling X11 Forwarding

      ssh -X user@remote_host
      

      Allows GUI applications to run on a remote server and display on the local machine.

  6. SSH Security Best Practices

    • Disable Root Login: Edit /etc/ssh/sshd_config and set PermitRootLogin no.
    • Change Default Port: Modify Port 2222 in /etc/ssh/sshd_config to reduce automated attacks.
    • Use Fail2Ban: Install and configure Fail2Ban to block IPs after multiple failed login attempts.
    • Disable Password Authentication: Enforce SSH key authentication by setting PasswordAuthentication no in sshd_config.
  7. Troubleshooting SSH Issues

    1. Checking SSH Service Status

      systemctl status sshd
      

      Ensures the SSH daemon is running.

    2. Debugging SSH Connections

      ssh -vvv user@remote_host
      

      Enables verbose output to diagnose connection issues.

    3. Restarting the SSH Service

      sudo systemctl restart sshd
      

      Applies configuration changes and restarts the SSH service.

Conclusion

Mastering Linux SSH command-line tools enhances system administration efficiency, security, and automation. Whether you need basic connectivity, secure file transfers, or advanced tunneling, SSH provides a robust set of features for managing remote systems effectively.

Yum Update

Recent Posts

Advanced SSH Techniques: A Deep Dive Into Secure Remote Access

Advanced SSH Techniques: A Deep Dive into Secure Remote Access Secure Shell (SSH) is a…

11 months ago

cPanel DNS Cluster Guide

Introduction The DNS Cluster feature set is flexible to allow for an array of configurations.…

1 year ago

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

1 year ago

This website uses cookies.