CentOS is a UNIX like Operating System. CentOS stands for Community Enterprise Operating System. CentOS is a free and open source operating system. CentOS is basically used for building servers, mainframes, workstations etc.
So today, I will be showing you some of the basic things that you need to do before you build a working server on CentOS 6. You can simply install Nginx, Apache, Lighthttpd or any other webserver that you want to install, but I will strongly recommend you to follow some basic operations before the initial setup of your server. This practice is done in order to give your server a basic security protection and make it private. For example if you want to change the default root login, then you will have to change the default SSH port and so many things like this.
It is not compulsory for you to follow each and every step of this tutorial, but I will highly recommend it because, after all, it is only for your own good. So if you are reading this article, you probably own a dedicated server or a VPS and so you can follow this guide now. Right now I am using a VPS running on Cent OS 6. And it is hosted by Digital Ocean.
So now, you can follow the guide below:
The first step is that you will have to login to your remote server through an SSH connection. For this purpose, you can use Putty in Windows or the Terminal if you are using a Mac or Linux. Then you will have to login as root user. If you are logging in for the first time, then the Terminal or Putty will ask you to cache the host key of the server in the registry, and then remember the ras2 key fingerprint of the server. Now the following kind of window will appear. You don’t have to worry about it, just click on Yes.
The next step is that you have to change the default root password. Sometimes when a server or a VPS is created, a random password is generated by the provider’s management software. It will be convenient for you if you change the password to something that you can remember easily, but it should be hard enough so that no one else can break it. To change the root password of your CentOS system, use the following command:
After entering this command, you will be asked twice to enter your new password. You should have a screen similar to this in front of you:
After completing the above two steps, now you have to create a new username. Root is a very common user name so it will be very easy for the hackers to guess this username so you will have to disable the root login and create a new username. This is just like administrator or admin if you are using Windows. You can use the command below to create a new username:
You can change the newuser in above command with the username that you want. After that, you have to use the following command to setup password for that user:
After you press enter, you will be asked to type the password for that user. You should have a following kind of screen in front of you:
So now you have to login using the newuser. After you login, you have to setup root privileges for that user. Still, you will be able to perform any kind of root only tasks. You can perform this task by using the following commands:
Then you will have to look for the following section/line:
# User privilege specification root ALL=(ALL) ALL
In different version of CentOS release, the above section may also look like this:
## Allow root to run any commands anywhere root ALL=(ALL) ALL
After you have found any one of thr above section, then you will have to add the following lines right after the root line:
## Allow root to run any commands anywhere root ALL=(ALL) ALL newuser ALL=(ALL) ALL
Now the screen in front of you should look something like this:
If you don’t know how to edit, you have to install the Nano editor. You can do so by simply entering “a” (and without quotes). Once you are done with adding a new line, you have to press Esc to exit the editing mode. Now in order to save and exit vi editor, you have to press Shift key and ZZ simultaneously.
Now you have to disable the root login and you have to change the default SSH port. To perform this operation, you have to edit the “sshd_config” file. This is the main configuration file of SSH service in the CentOS server you own. You can either use the Nano or vi editor to edit it. Personally, I am using the Nano editor right now.
Then you have to enter the following line:
You have to remove the # symbol and change the default port that is 22 to any number you want between 1025 and 65536. I am using port 22000 as an example.
The following kind of screen should appear now:
Now you have to find this:
Now you have to remove the # symbol and then you have to change yes to no. it will look something like this:
The screen will now look something like this:
Now you will have to find this line too:
Now again, you have to remove the # symbol and then change yes to no. it will look something like this:
The screen will now look something like this:
In this step, you are going to give privileges to the newuser to login to your server through SSH. For this purpose, you simply have to add the following line at the bottom of that file:
The screen will look something like this:
Once it is done, you have to save it by pressing Control+O and then exit the Nano editor by pressing Control+X.
Now you will have to reload the SSH service. You can reload it by using the following command:
Now the OK message will be returned to you.
Now all of your basic CentOS settings are in place. Now you should login so that you can make sure all the settings you defined in the SSH config file really works. Launch another Terminal window or Putty and then login using the new username, password and SSH port.
Now you will see your new username instead of root. The screen will look like this:
So this is all you should do before building a working server on your CentOS system.
Thanks for reading this article. Hope to see you soon.