Linux修改默认22端口的方法 (linux 修改默认22)
Introduction
Linux is an open-source operating system that is widely used for servers and networking. The default SSH port for Linux systems is 22, which makes it a common target for hackers looking to gn unauthorized access to your system. By changing the default SSH port, you can make it harder for attackers to break in. In this article, we’ll explore different ways to change the default SSH port on a Linux system.
Method 1: Editing the SSH Configuration File
The most common method for changing the default SSH port on a Linux system is to edit the SSH configuration file. The configuration file is located at /etc/ssh/sshd_config. Open the file using your preferred text editor and look for the line #Port 22. Remove the “#” symbol to uncomment the line and change “22” to a port number of your choice. Save the file and exit the text editor.
Next, restart the SSH service for the changes to take effect. On most Linux systems, the command to restart the SSH service is systemctl restart sshd. If you’re using a different Linux distribution, check the documentation for the correct command.
Method 2: Using Firewall Rules
Another way to change the default SSH port on a Linux system is to use firewall rules. This method allows you to keep the default SSH port open for other services while using a different port for SSH.
First, create a new firewall rule to allow inbound traffic to the new SSH port. The command for this varies depending on the firewall you’re using. For example, on a system with UFW (Ubuntu Firewall), you can use the command ufw allow /tcp.
Next, create a port forwarding rule that redirects traffic from the new SSH port to the default SSH port. This ensures that external devices can still connect to your system using SSH. The command for this also varies depending on the firewall you’re using. For example, on a system with iptables, you can use the command iptables -A PREROUTING -t nat -i eth0 -p tcp –dport -j REDIRECT –to-port 22.
Finally, update the SSH configuration file to use the new SSH port. Follow the same steps as in Method 1 to edit the configuration file and change the default SSH port to the port you chose.
Method 3: Using SSH Tunneling
SSH tunneling is a technique that allows you to access network services securely. With SSH tunneling, you can create a secure connection between your local machine and a remote server. This method uses a different port for SSH, but it doesn’t require any changes to the SSH configuration file.
To use SSH tunneling, connect to the remote server using the command ssh -L :localhost: @. Replace , , , and with the appropriate values. For example, if you want to use port 2222 on your local machine for SSH, and the server is using port 22 for SSH, you would use the command ssh -L 2222:localhost:22 user@server_address.
Once you’re logged into the remote server, you can access any service on the server that is set up to listen on the default SSH port (port 22) using the tunnel port you specify. For example, if you want to access the web server running on the remote server, you can open a web browser on your local machine and navigate to localhost:2222, which will connect you securely to the web server running on the remote server.
Conclusion