Ubuntu Server24.04 ; SSH , Firewall(UFW)

1. SSH Service Security Settings

The SSH service allows the root user to log in by default, and since the root user already knows the user name and can log in to the server with administrative privileges once the password is known, we will deny this setting.

1.1 Creating a General User

If you have created a general user when installing Ubuntu 24.04, this procedure is not necessary.
If you have already created a user at the time of OS installation, this procedure is not necessary. If you have already created a user during OS installation, this procedure is not necessary.
If you have already created a user during OS installation, this procedure is not necessary. 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 as follows

1.2 SSH service configuration file changes

To change the SSH service settings, modify the configuration file, which is located in "/etc/ssh/sshd_config".
This time, we will change the default SSH port 22 to 2244.

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

Uncomment line 17 #ListenAddress 0.0.0.0"

Change the "PermitRootLogin prohibit-password" parameter, which is found near line 34. The parameter "prohibit-password" means to disable password authentication for root.
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
↓ Remove comment out #.
PermitRootLogin prohibit-password

Restart SSH service

As it is, SSH connection cannot be made again, so the next firewall allows SSH port 2244, which has been changed

In Ubuntu 24.04 I could connect remotely by opening port 2244 in the firewall, but in Ubuntu 24.04 it does not work, so I edited the following file

Reload the daemon

Restart SSH service

Now I can connect remotely from Windows with Teraterm

2. Firewall Settings

Ubuntu often uses software called "ufw" to configure the firewall
UFW is installed when the OS is installed.
Here are the steps to configure minimal filter settings after installation.
Filter rules to be configured with ufw
-Deny all packets forwarded to the server
-Allow all packets sent from the server to the outside
-The first port allowed is the port for SSH (2244)
-Restrict packets coming into the server

2.1 Check ufw package

Check installed packages with the "dpkg" command to display packages

Installed "ufw packages" are displayed.
Run "systemctl status" command to check the status of ufw

ou 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 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 deny 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 Open SSH port

Allow SSH connections. The default SSH port is 22. Allow with the following command

If you have set your own 2244 port

2.4 Confirmation of ufw settings

Check the rules configured in the firewall after enabling." ufw status verbose".

2.5  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
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
The ssh connection is allowed to communicate to port 2244 because of the change to port 2244.
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.
Set "Do not allow connections from IP addresses that are accessing consecutively".

This will set the "Do not allow IP addresses with more than 6 connection attempts in a 30 second period" rule.
Confirm the setting. Display as follows.

2.5.2 Only allow ssh connections from specific networks

Even with the above settings, the ssh port is still open to the external Internet, so even if you set a limit on the number of connections, it is possible that someone will guess your password and connect to your computer in some way or another, or that a vulnerability attack will be used to connect to your computer.
Therefore, it is recommended that ssh connections be allowed only from internal networks, and that all external ssh connections be disabled.
As an example, there is a host in the local area network with an IP address of "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 Opening of the 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