If you are a Linux administrator, you might have used SSH commands to carry out various tasks. It is a secure network protocol that will help establish secure connections between different machines connected over the internet. Many system administrators use the SSH utility to manage devices and securely transfer the files between the systems. When working with remote infrastructure such as a Linux VPS, mastering these commands is essential for day-to-day operations and management.

This article mentions various SSH commands in Linux that will help you perform multiple functions on the Linux system. You can run these commands using the command-line interface. Let’s go through a comprehensive list of useful commands.

πŸ“Œ Prerequisites

Before starting, you need to maintain some prerequisites for running the SSH commands via the command line. Whether you are using a virtual machine or configuring an unmanaged environment, ensure you have:

  • An SSH client that is suitable for working.
  • An SSH server is installed and configured on the remote machine (you can learn how to install and enable SSH on Ubuntu if it is not already running).
  • The IP address of the remote connecting server.

🌐 Accessing a Remote Server via SSH

If you want to establish a secure connection to a remote machine, then all you need is the IP address of the remote machine. Then it would be best if you opened the command-line terminal (SSH command line) or the SSH client for running the below command, followed by the IP address or the name of the client machine. For a comprehensive overview, check out this guide on how to SSH to external systems easily.

ssh 192.168.56.101

or name:

ssh test.server.com

If you are connecting to the remote machine for the first time, you will get the message below. If you want to proceed, then you need to type yes and hit enter. Then you will be asked to enter the password for the client machine.

πŸ‘€ Specifying a Username for SSH connection

The SSH will use the current user whenever you try to access the remote server. If you want to specify a particular user for an SSH connection, you can run the below-mentioned command from the terminal.

ssh username@hostname_or_ip

Example-

ssh testuser@10.0.0.52

πŸ”Œ Using a Different Port Number for establishing an SSH Connection

By default, any server on the network will listen to port 22 while establishing a connection. You can read more about why this standard exists in our article on what is SSH port. But, if there is a change in the port setting in the SSH configuration file, you have to mention the changed port in the command specifically; otherwise, you will get an error connection refused, as shown below.

If you want to establish a connection to a remote host with a specified SSH port number, then you can use the -p flag as shown below.

ssh test.server.com -p 3324

πŸ”‘ Generating the SSH Keys Using SSH Keygen

To improve the SSH connections security, you can even generate a key pair using the keygen utility via SSH command. The key pair will include both the public and the private keys. The public key will be shared while the private key is kept secure. For a detailed walkthrough on this setup, read our guide on how to generate SSH key files.

These keys are used for authenticating the connecting clients to the server. Whenever you create an SSH key pair, you do not have to enter the password each time you establish a connection to the server. Understanding what are SSH keys will help you protect your environment from unauthorized credential access.

For creating the key pair, you can run the following SSH keygen command on the terminal of the host machine. You can use the same command to create an SSH key in Ubuntu and other Linux distributions.

ssh-keygen -t rsa

If you want to have the default settings, you can hit enter whenever you get the prompt for the file location and passphrase.

πŸ“‹ Copying the Public SSH Key

To use the key pair authentication, you must copy the available public key to the server. The key is in the form of the file id_rsa.pub, which is created using the keygen utility in the previous command.

If you want to copy the key to the server, you can run the following command from the client machine.

ssh-copy-id hostname_or_IP

If you do not want to use the current user, then you can specify the particular user you want to use. You will be requested to enter the password for the first and last time then the connection will be made without any password requirement.

πŸ“ Copying a File Remotely over SSH using SCP

If you want to securely copy files from SSH to local over the SSH protocol, you can do it using the SCP tool. You can review the complete syntax details in our comprehensive SCP command documentation. Below is the basic syntax that you can use.

scp fileName user@remotehost:/home/username/destination

Suppose you want to copy a file named β€œsample3” to your desktop on the remote server with the specific username, then you can use the following command.

scp sample3 test@10.0.10.5:/home/test/Desktop

Make sure you use the uppercase -P flag for specifying the particular port when you want to copy file SSH.

βš™οΈ Editing the SSH Config File

By making the required changes to the SSH config file, you can control how the users can access a server using SSH. You can customize the SSH server options using the sshd_config file. Make sure to make the correct changes; else, the server will be inaccessible to you. If you need a simpler console editor alternatives, you might also look at the Nano Linux command line text editor.

For making the changes, you can use the Vim editor. But, you need to have superuser permission to make the required changes. You can run the following command on the remote host to proceed with the change.

sudo vim /etc/ssh/sshd_config

Now you need to provide the sudo password, and the shell will open the file in the editor you want to change.

πŸ”„ Restarting the SSH service

Once you make the SSH config changes, you need to restart the services in Linux to reflect the latest changes to the system. You can run one of the following commands depending on the type of Linux distro you are using.

sudo ssh service restart

or:

sudo sshd service restart

Then, you need to provide the password for completing the process.

πŸ› οΈ Basic SSH Commands

If you are about to work on the remote server, you must know some of the basic commands that will be beneficial to managing the remote host.

1. Checking Working Directory Path

You can use the following command if you want to check the current working directory of the user. The output will display the directory’s location.

2. Listing all the Files and Directories

If you want to list the contents of the current working directory, you can use the ls command. You can use the below-mentioned flags with the ls command for different functionality.

  • -a will help display the hidden files and entries that start with a dot.
  • -l will help in showing the complete file details for directory contents such as permissions, ownership, date, etc.
  • -s it will list the size of files in blocks. You can even add the flag -h to display the size in a humanly readable form.

If you want to navigate from one folder to another, you can use the cd (change directory) command as shown below.

cd Desktop/Downloads/Test

Always remember that the names are case-sensitive.

3. Changing Directory

You can use the cd command carefully, and it comes in variants, as shown below.

  • cd .. it allows you to navigate to the directory one level higher than your current location.
  • cd - it will enable you to navigate back to the previous directory.
  • cd / it will enable you to navigate to the root directory.

4. Copying a File

If you want to copy a file from one location to another, you can use the cp command along with the file name, source, and destination as given below. For directories, you can read our deep dive on how to copy directory Linux parameters.

cp file_Name /directory/path/destination

If you want to copy the directory and its content, you need to use the -r flag with the command below, where r specifies the recursive.

cp -r Directory1 New_Location

5. Moving a File

The mv command will work almost in the same manner as the cp command. You can use the following syntax for the mv command. To read more about file transfers and reorganization, see our guide on how to move file in Linux partitions.

mv file_Name directory/path/destination

6. Creating a File

You can use the touch command along with the new file name to create a new file. You also need to specify the file extension to specify the type of file to be created. Run the following command to create a file. For further details on options, see the touch command in Linux manual.

touch new_file_Name

7. Creating a Directory

For creating a directory, you can use the mkdir command along with a new directory name or full path in the below-mentioned format:

mkdir New_Directory_Name

Or:

mkdir directory/path/New_Directory_Name

8. Delete a File or Directory

For deleting a Linux file, you can use the rm in the below-mentioned format:

rm file_Name

or

rm /home/user/dir1/file_Name

9. Checking Network Information

If you want to check the network adapters’ status, you can use the ipconfig command. If you do not specify any option along with the ipconfig, then the output will show the active interfaces only. For detailed monitoring, check out how to monitor network traffic in Linux settings.

10. Clearing the Terminal Screen

To clear the current working area of the bash screen, you can type clear on the screen and hit enter.

πŸ–₯️ Running a Command on a Remote Server from a Local Computer

This method will not create a new shell but run a command and then return the user to the local prompt. Then you can run the desired commands.

If you want to run a command remotely from the local machine, then you need t make changes to the SSH command, as shown below.

ssh test.server.com rm ~/Desktop/Dir1/sample4

Then, you need to enter the required password for the first time. Then the remote file will get deleted on the remote server without creating a new shell.

πŸš€ Advanced SSH Commands & Configuration Options

Once you have mastered the basic commands, implementing advanced configurations will elevate your command-line efficiency and secure your server access. Below are intermediate and advanced features that every administrator should utilize.

πŸ’‘ Using SSH Config Aliases

Instead of typing long commands like ssh testuser@10.0.0.52 -p 3324 every single time, you can define shortcuts in your local configuration file located at ~/.ssh/config. Open or create the file and add the following lines:

Host myserver
  HostName 10.0.0.52
  User testuser
  Port 3324

Once saved, you can connect to your remote host instantly by simply entering:

ssh myserver

πŸ›‘οΈ Enhancing Remote Security Settings

Securing your SSH infrastructure prevents brute-force login attempts. To optimize protection, consider editing your configuration file to disable password-based logins completely after your SSH keys are deployed. You can find out more instructions in our guide to Linux server security configurations. For example, modifying PermitRootLogin no ensures unauthorized automated scripts cannot hijack the main administrator identity.

πŸ“Š Comparison of Common SSH Actions and Alternatives

The table below details several crucial tasks performed during remote management sessions, the native SSH command used, and standard alternatives available in modern Linux environments.

Administrative Action Primary SSH/Linux Command Alternative / Tool Complement
Secure File Transfer scp file.txt user@host:/path/ SFTP / Rsync Utility
Interactive File Editing sudo vim /etc/ssh/sshd_config nano Editor
Port Configuration Changes ssh -p [port_number] user@host SSH Config Profile Aliases
Directory Synchronization scp -r dir/ user@host:/path/ Rsync with SSH wrapper

❌ Common Mistakes When Using SSH Commands

  • Wrong Case for Port Flags: Forgetting that ssh uses lowercase -p for specifying connection ports, whereas the scp command requires an uppercase -P flag.
  • Neglecting Permissions on Key Files: Setting lax file permissions on your local private key (such as id_rsa). If permissions are too open, SSH will reject the connection. Always run chmod 600 ~/.ssh/id_rsa.
  • Failing to Restart Services: Modifying parameters inside the sshd_config file and assuming they apply instantly without running a service restart command.

🏁 Conclusion

SSH is a common method for securely transferring data between devices over the network. You can use this command for executing various useful tasks that will ensure the security of the complete process. It allows you to manage your remote server with the appropriate access on the remote server.

We have mentioned some of the commonly used SSH commands that will be used daily by the system administrator. Go through these commands, and you will understand how SSH commands work.

People also read: