More and more webmasters begin to use independent hosts (DedicatedHost) and VPS. In order to save costs or improve performance, many independent machines and VPS are all unmanaged bare-metal machines, and everything needs to be DIY. At this time, the implementation of security policies is still important. In the following article, I will take CentOS as an example to briefly summarize how to configure
More and more webmasters begin to use independent hosts (DEdICatEd Host) and VPS. In order to save costs or improve performance, many independent machines and VPS are all unmanaged bare-metal machines, and everything needs to be DIY. At this time, the implementation of security policies is still important. In the following article, I will take CentOS as an example to briefly summarize how to configure Secure SSH access.
Linux SSH Security Policy 1: Disable irrelevant ports
Most hosts attacked on the network are targeted by hackers who use scanning tools for a wide range of scans. Therefore, in order to avoid being scanned, all ports, such as Web, FTP, and SSH, should be disabled. I strongly recommend that you disableCmpPort, and set rules to discard the icmp packet. In this way, if someone else cannot Ping your server, the threat will be reduced by half. To discard an icmp packet, add the following to iptables:
Linux SSH Security Policy 2: Change the SSH port
The default SSH port is 22. We strongly recommend that you change it to 10000 or above. In this way, the chances of other users scanning ports are also greatly reduced. Modification method:
# Edit/etc/ssh/ssh_configvi/etc/ssh/ssh_config # Add a new Port value under Host. Take 18439 as an example (the same below): Port 22 Port 18439 # edit/etc/ssh/sshd_configvi/etc/ssh/sshd_config # Add a new Port value Port 22 Port 18439 # Save, restart the SSH service: service sshd restart
|
Here, I set two ports to prevent SSH from being unable to be accessed again due to modification errors. Change your SSH client (for example:Putty), Test the connection,If the new Port can be connected successfully, edit the above two files and delete the configuration of Port 22.. If the connection fails, use Port 22 to connect and then reconfigure it.
After the port is successfully set, note that port 22 should be deleted from iptables, port 18439 of the new configuration should be added, and iptables should be restarted.
If the SSH logon password is weak, you should set a complicated password. On the Google Blog, I wrote an article emphasizing password security: Does your password pass the test?
Linux SSH Security Policy 3: Restrict IP Login
If you can connect to your server using a fixed IP address, you can set to allow only a specific IP address to log on to the server. For example, I log on to the server through my own VPN. The settings are as follows:
# Edit/etc/hosts. allow
Vi/etc/hosts. allow
# For example, you can only log on to 123.45.67.89.
Sshd: 123.45.67.89 |
Linux SSH Security Policy 4: Use a certificate to log on to SSH
It is safer to use a certificate than to log on with a password.Tap water brewed coffeeI have written a detailed tutorial and obtained the consent of it:
Configure SSH certificate login verification for CentOS
Help the company's network administrator remotely detect the email server, a CentOS 5.1 instance, and use OpenSSH for remote management. When checking the security log, we found that there were a bunch of IP addresses every day to guess the password over the past few days. It seems that it is better to change the logon authentication method to certificate authentication.
In case of protection, a VNC is enabled temporarily, so that sshd cannot be restarted without configuration. (Later I found it redundant, as long as I open a putty first and don't close it)
Follow these steps:
1) Add a maintenance account: msa2) andSu-Msa3) ssh-keygen-t rsa: After specifying the Key Path and entering the password, generate the public key and private key in/home/msa/. ssh:Id_ Rsa id_rsa.pub4) cat id_rsa.pub> authorized_keys: Why is this file generated? This is what is written in sshd_config. ThenChmod400 authorized_keys: a little protection. 5) Use psFtpPull id_rsa back to the local machine, and then take out id_rsa and id_rsa.pub on the server. 6) Configure/etc/ssh/sshd_configProtoCol2 ServerKeyBits 1024PeRmItRootLogin no # prohibit root login. It has nothing to do with this article. In addition, it is more secure. # There is nothing to change in the following three lines. You can remove the default # comment to RSAAuthentication yesPubkeyAuthentication yesAuthorizedKeysFile. ssh/authorized_keysPasswordAuthentication noPermitEmptyPasswords no7) Restart sshd/sbin/service sshd restart8) convert the Certificate Format, migrate putty to run puttygen, and convert id_rsa to putty's ppk Certificate file 9) configure putty to log on to connection -- SSH -- Auth, Click Browse, and select the certificate you just converted. Then fill in the auto login username in connection-Data. For example, if you are happy, you can save the Server IP address in the session of the msa. 10) When you do this, it is very likely that there will be an empty joy. At this time, I am eager to log on. Maybe I cannot log on: No supported authentication methods available. Now I can modify sshd_config and temporarily change PasswordAuthentication no: passwordAuthentication yes and Restart sshd to log on successfully. After logging out, change the value of PasswordAuthentication to no and Restart sshd. After you log on, you will be asked about the password of your key file, and you will be able to log in happily. For psftpCommandAnd add the-I parameter to specify the Certificate file path.
|
If you modify the preceding configuration on a remote server, be sure to exercise caution in each step and avoid errors. If the configuration is incorrect and the SSH connection fails, the problem may be solved.
Basically, after the above four points are configured, SSH access in Linux is safer. Of course, security and insecurity are relative. You should regularly check the server log to detect and eliminate potential risks in time.
Article