How to make your linux server more secure?

If you are a linux administrator or a developer you are always concerned about the security of your web server. We always follow the best practises and learn from other developers.

Basically, linux server has a default root user as all know. An attacker can easily take a guess about the root user and can try to break into a linux system however if we disable the root user he might not be able to guess the root user.

You have to make sure that only allowed person should know about this newly created sudo user. In this tutorial we will follow some of the steps to secure our linux server:

create a new admin user

First of all we have to log on to our web server using ssh and then create a new admin user with sudo access:

# ssh with root user
$ ssh root@YOUR_SERVER_IP

# create alternate user called admin
$ useradd -m -d /home/admin admin

# make sure home directory created and has user permissions
$ ls -lad /home/admin

# update new user password
$ passwd admin

# add sudoer permission to new user
$ echo 'admin ALL=(ALL) ALL' >> /etc/sudoers

# now exit the terminal and make sure you
# can ssh with new admin user before you disable root login
$ exit

Disable ssh login for the root user

Once we have a new sudo user created from above steps now we will disable ssh login for our root user.

$ ssh root@YOUR_SERVER_IP

# once confirmed log in as root and open following file
$ nano etc/ssh/sshd_config

# find the PermitRootLogin line and change it to
PermitRootLogin no

# save your changes and restart the ssh
$ sudo service sshd restart

# now logout and try to login with root user
# it should not allow ssh for root user
$ exit
$ ssh root@YOUR_SERVER_IP

# if above step works then try login with
# newly created admin user to login via ssh
$ ssh admin@YOUR_SERVER_IP

# exit the server
$ exit

Once you exit out from your webserver you wont be able to login using ssh root user. Let's try to login using our newly creted admin user and see if it works:

How to switch to sudo mode?

If you want to perform root level operations for our new admin user then we have to switch to sudo mode and run commands as root user.

Because we wont be able to log in using root anymore we have to login as one of the sudo user we created and switch to sudo mode if we want to run a command as root user.

# login using admin user
$ ssh admin@YOUR_SERVER_IP

# try to switch with root user
# enter root password
$ su