SSH (Secure Shell) is a network protocol that allows you to securely connect to a remote server over an unsecured network. It is used to execute commands on a remote server and to transfer files to and from a remote server.
If you are exposing your linux server to the internet, it is important to set up a firewall to protect it from unauthorized access. Ubuntu Linux ships with a firewall called ufw (Uncomplicated FireWall) that is easy to configure.
To enable ufw firewall, run:
sudo ufw enableYou can check the status of the firewall with this command:
sudo ufw statusTo enable remote login with SSH, you need to install the openssh-server package.
To install the openssh-server package, run:
sudo apt update
sudo apt install openssh-serverAllow SSH traffic through 'ufw' firewall:
sudo ufw allow sshIt is recommended to disable logging in as the root user because it is a common target for brute-force attacks.
To disable root login, open the SSH daemon configuration file:
sudo nano /etc/ssh/sshd_configFind the line that contains PermitRootLogin and modify it to ensure that users can only connect with their own credentials:
PermitRootLogin noRestart the SSH daemon:
sudo systemctl restart sshdSSH key-based authentication is more secure than password-based authentication because it is not vulnerable to brute-force attacks.
Setting up ssh key-based authentication involves generating a public-private key pair on your local machine and copying the public key to the remote server.
To generate a new SSH key pair, run (on your local machine):
ssh-keygenTo copy the public key to the remote host, run (on your local machine):
ssh-copy-id username@remote_hostTo disable password authentication, open the SSH daemon configuration file (on the remote server):
sudo nano /etc/ssh/sshd_configFind the line that contains PasswordAuthentication and modify it to ensure that users can only connect with their own credentials:
PasswordAuthentication noRestart the SSH daemon (on the remote server):
sudo systemctl restart sshdTest the new configuration by logging in without a password (on your local machine):
ssh username@remote_hostChanging the SSH port is a good idea because it makes it harder for attackers to find your SSH port and launch brute-force attacks.
To change the SSH port, open the SSH daemon configuration file (on the remote server):
sudo nano /etc/ssh/sshd_configFind the line that contains Port and modify it to the desired port number:
Port 7777Restart the SSH daemon (on the remote server):
sudo systemctl restart sshdTest the new configuration by logging in with the new port number (on your local machine):
ssh username@remote_host -p 7777