Click here for "Safe Air Conditioner Repair and Proper Freon Recovery".

Ubuntu Server 22.04 : SSH , Firewall

1. SSH Service Security Settings

The SSH service allows root user login by default.
The root user can log in to the server with administrator privileges if the password is known because the user name is already known.

1.1 Creating a General User

If you have created a general user when installing Ubuntu 22, this procedure is not necessary.
If the only user created on the server is root, remote login via SSH will not be possible, so if a user has not been created during OS installation, a user must be created in advance.

Users can be created with the "useradd" command. The "-m" option creates a home directory and the "-p" option specifies the password.
For example, to set "ubuntuuser" as the user account name and "123456" as the password, execute the following

 

1.2 SSH service configuration file changes

This time, we will proceed by changing the default SSH port from 22 to 2244.

#Add ssh connection port 2244 on line 15
# port 22
Port 2244

#Line17 #ListenAddress 0.0.0.0   Comments Unsubscribe

#Change the "PermitRootLogin prohibit-password" parameter, which is found near line 34.
The parameter "inhibit-password" implies that password authentication is disabled for root.
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
↓ 
PermitRootLogin prohibit-password

Restart SSH service

2. Firewall Settings

Since Ubuntu often uses software called "ufw" to configure firewalls, we will use ufw to configure firewall settings.
ufw is installed when the OS is installed.
Filter rules to be set in ufw
• All packets forwarded to the server are rejected
• All packets sent from the server to the outside are allowed
• The first port to allow is the port for SSH (2244)
• Limit packets coming into the server

2.1 Check ufw package

Confirmation by "dpkg" command

Run the "systemctl status" command to check the status of ufw

You can confirm that the ufw service is running by seeing "Active: inactive (exited)".

Enable ufw.

2.2 Basic firewall rule configuration

When ufw is enabled, default firewall rules are applied. If you enable it as is, you may lose communication with the server, so set up some basic rules before enabling ufw.

2.2.1 Incoming packets Default rule settings

First, set the rules for incoming packets.
The general rule is to reject all incoming packets except for specific communications.
Execute "ufw default deny incoming" to basically deny all incoming packets.

2.2.2 Outgoing packets Default rule settings

The general rule is to allow all outgoing packets. Execute "ufw default allow outgoing" to basically allow outgoing packets.

2.3 SSH Port Permissions

Configure SSH connection permissions. The default SSH port is 22. Use the following command to set permissions

If you have set your own 2244 port (e.g.)

2.4 Confirmation of ufw settings

Check the rules set in the firewall after enabling.

2.5 Permission to limit packets coming into the server

If you want to "allow communication coming to port number "◯◯" in ufw settings, use the following command
# ufw allow [port number]/tcp
On the other hand, if you want to "disallow communication coming to port number "◯◯", use the following command
# ufw deny [port number]/tcp

2.5.1 Do not allow connections from IP addresses that access continuously

Explained using the SSH port 2244 that was just configured as an example
They will try to gain access to port 2244 by typing in the appropriate password and attempting to match it by chance so that they can log in. This is also called a brute force attack.
As a countermeasure for this, set "Do not allow connections from IP addresses that access continuously". Type the following command

This will set the "do not allow IP addresses with more than 6 connection attempts in a 30 second period" rule.
Verify settings
2.5.2 Only allow ssh connections from specific networks

Even with the above settings, the ssh port is open to the external Internet, so even if you set a limit on the number of connections, the password could be guessed in some way and a connection could be made, or a vulnerability could be exploited to gain access.
Therefore, it is recommended that ssh connections be allowed only from internal networks and all external ssh connections be set to not be allowed.
As an example, there is a host in the local area network that is assigned the IP address "192.168.11.10". Allow ssh connections only from this host. Or, to allow ssh connections only from this network (192.168.11.0/24), type the following command

If you check the settings, you will see that
Delete the rule with LIMIT. View the rule number and confirm the setting.
Delete rule 1 by specifying its number.
2.5.3 Permission for web and other services

You can allow connections by specifying a port number, or you can specify an application.
You can see a list of applications with the following command.

For example, to enable http and https for web services
2.5.4 Disable ipv6 ufw
Restart the firewall after all work
Copied title and URL