The Ultimate SSH Guide: How to Connect and Use Commands with Examples

Sourabh Mourya

Sourabh Mourya

Sr. Software Developer
Share on facebook
Share on twitter
Share on pinterest
Share on linkedin
SSH Connect and Use Commands with Examples

Introduction Secure Shell (SSH) is a network protocol that allows users to securely connect to remote systems or servers over an unsecured network. It provides secure encrypted communications between two untrusted hosts over an insecure network, such as the internet.

SSH is widely used by system administrators, developers, and power users for managing remote systems, running commands, transferring files, and executing scripts. In this article, we will provide you with an ultimate guide on how to connect and use commands with examples.

Prerequisites Before we begin, you need to have an SSH client installed on your local computer. Most Linux and macOS systems come with a pre-installed SSH client.

SSH Connect and Use Commands with Examples
SSH Connect and Use Commands with Examples

However, Windows users need to download and install an SSH client like PuTTY or OpenSSH. You also need to have access to a remote server with SSH enabled and a valid username and password or an SSH key pair.

Connecting to an SSH Server To connect to an SSH server, you need to open your terminal or command prompt and type the following command:

ssh username@server_address

Replace username with your username and server_address with the IP address or hostname of the remote server. If the server uses a different SSH port than the default (port 22), you can specify it with the -p option.

ssh -p 2222 username@server_address

If this is your first time connecting to the server, you will see a message asking you to confirm the server’s authenticity. Type yes to continue. You will also be prompted for your password or SSH key passphrase if you use one.

Using SSH Commands Once you are connected to the SSH server, you can start using SSH commands. SSH commands are similar to local commands, but they are executed on the remote server.

Here are some commonly used SSH commands:

1. ls – List files and directories in the current directory.

ls

2. cd – Change the current directory.

cd /path/to/directory

3. mkdir – Create a new directory.

mkdir new_directory

4. rm – Remove a file or directory.

rm filename
rm -r directory_name

5. mv – Move or rename a file or directory.

mv old_filename new_filename
mv old_directory_name new_directory_name

6. cp – Copy a file or directory.

cp file destination
cp -r directory destination

7. nano – Open a text editor.

nano filename

8. cat – Display the contents of a file.

cat filename

9. chmod – Change the permissions of a file or directory.

chmod 755 filename

10. chown – Change the owner of a file or directory.

chown username:groupname filename

Transferring Files with SCP SCP (Secure Copy) is a command-line tool for securely transferring files between local and remote systems over an SSH connection. SCP is similar to the cp command, but it uses SSH for secure data transfer. Here’s how to transfer a file from your local system to a remote server:

scp local_file username@server_address:/remote/directory/

Replace local_file with the path and filename of the local file, username with your username, server_address with the IP address or hostname of the remote server, and /remote/directory/ with the destination directory on the remote server.

Here’s how to transfer a file from a remote server to your local system:

scp username@server_address:/remote/file /local/directory/

Replace username with your username, server_address with the IP address or hostname of the remote server, /remote/file with the path and filename of the remote file, and /local/directory/ with the destination directory on your local system.

Conclusion

SSH is a powerful tool for securely managing remote systems and executing commands on remote servers. In this article, we covered the basics of how to connect to an SSH server and use common SSH commands with examples.

We also discussed how to transfer files securely using the SCP command. With this knowledge, you should be able to use SSH effectively for managing your remote systems and performing remote tasks securely.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Stories