Initial setup of Ubuntu 16.04 server

Recently, I suffered a bit on the issue of server security, so I decided to write a few articles related to Linux server initialization to record the results of this pitfall. Setting up as early as possible will make the server more secure, improve its availability, and lay the foundation for subsequent deployment procedures.

One, root login##

After we buy a [cloud server] (https://cloud.tencent.com/product/cvm?from=10680), we can get the server's ip and account password from the console. At this time we can pass

ssh root@SERVER_IP_ADDRESS

This command is used to connect to the server. If there is a warning about accepting the authenticity of the host, choose to agree and after entering the password, the login process can be completed.

What is root###

In Unix and Unix-like systems, root is the common name of a user (that is, super user) who has all rights to all files and programs in all modes (single/multi-user).

Two, create a new user##

The permissions of the root account are very large, so after we log in to the root account for the first time, we should prepare a new account, and use this new account to log in later. So in the next step we are going to create a new user.

For example, I want to create a new user named "leon".

adduser leon

After confirmation, we need to set a password for the account first. After that, the system will ask us for some information, including full name, home phone number, work phone number and so on. If possible, set a strong password for the account and remember it. You can also optionally leave your other information.

Three, give root permissions##

The leon we just created is an account with regular account permissions, but we may need super administrator permissions in daily use. The way to use this permission is to add sudo before the command, for example:

sudo vi /etc/hosts

In order to avoid insufficient permissions, we need to give some trustworthy accounts with root permissions as appropriate. This is done when we log in with the root account.

usermod -aG sudo leon

Now our user leon has the command to run superuser privileges.

Four, use ssh to log in to the server##

Using the ssh key to log in to the server can improve the security of the server. I have already written about this operation in my previous blog, so I will post a link here.

[ Use ssh-key to log in to the server without password](http://originalix.github.io/2018/04/25/%E5%9C%A8Mac%E4%B8%8A%E4%BD%BF%E7%94%A8ssh -key%E5%85%8D%E5%AF%86%E7%A0%81%E7%99%BB%E5%BD%95%E6%9C%8D%E5%8A%A1%E5%99%A8 /)

After completing the SSH login server configuration, let's talk about how to disable the password authentication method to improve the security of the server.

Five, set to disable password login##

If we allow the server to log in through password authentication, then any bad guy who gets our account password may log in to the server to perform destructive operations. Therefore, for the security of the server, I recommend that you prohibit the use of password authentication to log in to the server. After all, we have configured ssh-key on the trusted computer.

Warning⚠️: You must configure the ssh-key to be able to log in to the server before performing this step, otherwise your server will never be able to log in, and you can only reinstall and restore it, which may cause data loss.

Use the root account or sudo command to open the ssh configuration file:

sudo vi /etc/ssh/sshd_config

Look for the Passwordauthentication option, delete the comment # in front of him, and then change its value to no.

# sshd_config disable password login settings
PasswordAuthentication no

Next, find the following two configurations, if they are consistent with mine, do not change, otherwise change to the same, the default configuration is the following value:

PubkeyAuthentication yes

ChallengeResponseAuthentication no

When you finish these changes, save and use, use :wq to save and exit.

Restart the ssh program

sudo systemctl reload sshd

Password authentication is now closed, you can only use ssh for authentication.

Before you log out of this login connection, it is best to open a new window to verify your configuration, first confirm that ssh can successfully log in, so as not to wait for a while to log in, try to log in to the server with another account, if the password access is denied , Then our configuration is successful.

Six, set up firewall##

Ubuntu 16.04 server can use UFW firewall to ensure that only connections with certain services are allowed. We can easily use this application to set up a basic firewall.

Different applications can register their configuration files with UFW during installation. These configuration files allow UFW to manage these applications by name. OpenSSH, the service that allows us to connect to our server now, has registered a configuration file with UFW.

You can view this by typing:

sudo ufw app list

You can see the following

Available applications:
 Apache
 Apache Full
 Apache Secure
 OpenSSH

We need to ensure that the firewall allows ssh connections, so we allow these connections through the following command:

sudo ufw allow OpenSSH

Use the following command to enable the firewall:

sudo ufw enable

We can also check the status of the firewall by ourselves:

sudo ufw status

can be seen:

Status: active

To                         Action      From
------------
Apache Full                ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
Apache Full(v6)           ALLOW       Anywhere(v6)OpenSSH(v6)               ALLOW       Anywhere(v6)

For example, the api interface of iOS, the configuration of WeChat applet needs to use the https protocol, then we need to open the port 443 to access, we can use the following command to open the port:

sudo ufw allow 443 allows external access to port 443

At this point, the basic configuration of our server has been completed, and then we can create a separate configuration for secure access to the database.

Recommended Posts

Initial setup of Ubuntu 16.04 server
Use Ubuntu 16.04 for initial server setup
Ubuntu Touch environment setup
3D effect of ubuntu
Summary of ubuntu usage
Deploy FTP server under ubuntu
[Linux] Build Samba server (ubuntu16.04)
Install OpenSSL 1.0.2 on Ubuntu Server 14.04
Ubuntu Server Chapter 8 DNS Service
ubuntu 16.04 build pptpd V** server
Ubuntu Server Chapter 3 Package Management
Use of Anaconda in Ubuntu
Ubuntu Server Chapter 7 Remote Management
Server upgrade Ubuntu 20.04 LTS record
ufw of ubuntu firewall rules
Build Ubuntu 12.04 cross compilation server
Use of mediawiki under ubuntu
Server upgrade Ubuntu 20.04 LTS record
Ubuntu server builds Java web server
Method of setting up PHP operating environment under Ubuntu server
Summary of various ubuntu problems
Ubuntu deploys squid proxy server
ubuntu root default password (initial password)
Ubuntu16.04 build GitLab server tutorial
Happy installation of Jenkins in ubuntu
Detailed explanation of ubuntu using gpg2
Install Chef server workstation on Ubuntu 18.04
Ubuntu16.04 build php5.6 Web server environment
Summary of Ubuntu commonly used commands
Overview of the Ubuntu file system
MySQL connected to remote Ubuntu server
Install Ubuntu 18.04 server with kvm virtualization
Build a file server on ubuntu
Installation of Docker CE under Ubuntu
Coexistence of CUDA8.0 and CUDA9.0 under Ubuntu 16.04
Install Oracle 11gR2 on Ubuntu Server 12.4.0