SSH, or Secure Shell, is a network protocol that allows users to securely connect to a remote computer over an unsecured network. It provides a secure channel for data transmission by encrypting the data exchanged between the client and the server. SSH is widely used for remote administration, file transfer, and tunneling.
SSH uses a client - server model. The SSH client initiates a connection to the SSH server. When the connection is established, the client and the server perform a key exchange to generate a shared secret key. This key is used to encrypt all the data transmitted between them. The authentication process can be based on passwords or SSH keys.
Remote management using SSH allows system administrators to perform various tasks on a remote server without being physically present at the server’s location. These tasks can include system configuration, software installation, and monitoring system resources.
To connect to a remote server using SSH, you can use the following command:
ssh username@server_ip_address
For example, if your username is john
and the server’s IP address is 192.168.1.100
, you would run:
You will be prompted to enter the password for the specified user account.
SSH provides two common tools for file transfer: scp
(Secure Copy) and sftp
(Secure File Transfer Protocol).
scp
To copy a file from the local machine to the remote server:
scp local_file_path username@server_ip_address:remote_file_path
To copy a file from the remote server to the local machine:
scp username@server_ip_address:remote_file_path local_file_path
sftp
To start an sftp
session:
sftp username@server_ip_address
Once in the sftp
session, you can use commands like put
to upload files and get
to download files.
You can run commands on a remote server without starting an interactive shell session. For example:
ssh username@server_ip_address 'ls -l'
This command will run the ls -l
command on the remote server and display the output on the local machine.
By default, SSH runs on port 22. Changing the SSH port can enhance security by reducing the risk of brute - force attacks. To change the SSH port, edit the /etc/ssh/sshd_config
file on the server and modify the Port
line:
Port 2222
Then restart the SSH service:
sudo systemctl restart sshd
When connecting to the server, you need to specify the new port using the -p
option:
ssh -p 2222 username@server_ip_address
SSH keys provide a more secure and convenient way to authenticate with a remote server compared to passwords.
On the client machine, you can generate an SSH key pair using the ssh-keygen
command:
ssh-keygen -t rsa -b 4096
This will generate a private key (~/.ssh/id_rsa
) and a public key (~/.ssh/id_rsa.pub
).
You can use the ssh-copy-id
command to copy the public key to the remote server:
ssh-copy-id username@server_ip_address
SSH tunneling allows you to create a secure connection between two hosts through an intermediate SSH server. For example, to create a local port forwarding:
ssh -L local_port:destination_host:destination_port username@server_ip_address
This will forward traffic from the local port to the destination host and port through the SSH server.
/etc/ssh/sshd_config
file and set PermitRootLogin no
.AllowUsers
directive in the sshd_config
file to specify which users are allowed to connect via SSH.fail2ban
is a tool that can block IP addresses that make multiple failed login attempts./var/log/auth.log
or /var/log/secure
.The SSH configuration file (~/.ssh/config
) allows you to define shortcuts for SSH connections. For example:
Host myserver
HostName server_ip_address
User username
Port 2222
Then you can connect to the server using:
ssh myserver
SSH is a powerful and essential tool for remote management on Linux systems. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can use SSH more effectively and securely. Whether it’s for system administration, file transfer, or tunneling, SSH provides a secure and reliable way to interact with remote servers.
ssh
, scp
, sftp