DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Beginners’ Guide to Run a Linux Server Securely

Beginners’ Guide to Run a Linux Server Securely

This article explains what you need to take some essential considerations for tackling common security risks with Linux Server.

Hadi Samadzad user avatar by
Hadi Samadzad
·
Jan. 25, 23 · Tutorial
Like (3)
Save
Tweet
Share
3.99K Views

Join the DZone community and get the full member experience.

Join For Free

Linux could be a fantastic choice for your next cloud server. Imagine you can benefit from an up-to-date and fully-loaded operating system on a 90s hardware configuration of 512 MB and 1-core CPU. Apart from technical benefits, it is the cheapest option to have, so you may have decided to run your services on it. Although connecting to a server just using a single line command, you are keeping it secure could be a bit tricky. I will go through what you need to take some essential considerations for tackling common security risks with server hardening.

Choosing a Distro to Start

Unlike Windows and macOS, Linux is a family of open-source operating systems, and many different distros have been published. Some of the most popular Linux distros are Red Hat, CentOS, Fedora, Debian, Ubuntu, Kali, Mint, etc. However, from a high-level point of view, there are two major families of distros: Red Hat-based and Debian-based.

For many Linux beginners, it is a matter of high significance to choose a distro as a starting point. Although you can take your time to thoroughly investigate different options, the best course would be just starting with one of them and be sure you will enjoy the taste of Linux with all of them. If you have no idea and want to start, I recommend choosing Ubuntu, thanks to its community and its myriad of available documents.

Ubuntu is a Debian-based Linux distribution introduced by Canonical in 2004. There are three editions of Ubuntu: Desktop, Server, and Core. The second edition, as its name shows, is considered to be used for servers. Against the Desktop edition, Ubuntu Server doesn't comprise any graphical user interface, and you may use a command-line tool named bash to manage the server.

Connecting to Server

No matter which provider you choose to buy a server from, after ordering a VPS, you need to acquire its connection info. Generally, it should be dropped in your inbox as soon as you check out your order. Then, all you need to connect and set up your machine are two things: the server's address and its root password.

To connect to the server, you may need an application that supports SSH, which is a protocol for communication between two computers. It doesn't matter if you are using Windows or macOS; you can connect to your server using the ssh command in a command-line tool like this:

Shell
 
ssh [USER]@[SERVER_IP_Address]


Possibly, providers do not mention the username, but you should know the username is the root. By executing the ssh command, you will be prompted to insert the password, and by providing that, you will log in to your fantastic server.

Ubuntu Server Terminal

Ubuntu Server Terminal

Once you log in, you can see a couple of information about the Ubuntu version and allocated hard disk capacity and can start loading your services; however, you may need to do further steps to secure your server as there are others intended to use your resources illegally without taking its responsibility. Therefore, it is crucial for you to apply at least some essential security measures on your newly bought machine.

Knowing the Threats

Suppose an evil wants to use your machine without your allowance; what do they need? Yeah, the server IP address and the password.

Although finding a combination of an unknown IP address and a random password might seem to be impossible, believe me, it is viable via a brute force tool, as I have been a victim a couple of times. To have a clue check the server's IP address using ping, and you will see how a valid IP can be identified among many other invalid IP addresses.

Shell
 
ping [SERVER_IP_ADDRESS]

ping Command

As soon as unauthorized access is attained, it is not predictable in which way they will use your resources with your responsibility. So let's dive into some simple steps you can do to minimize the risks.

Adding Some Security

Step 1 — Strong Password: This is the first way that comes to mind. Based on password strength checker tools like this, if you use a random password, it should be at least ten characters in length so that it takes one day to be found by bots. As well it may be much easier if you use a pattern to create your password, so go for a longer password in length.

Step 2 — Remove root Access: You can simply add another complexity to the SSH login by removing root access. This way, plus the IP address and password, the username must be provided to log in, as the default username which is root is blocked. To this end, you must add a new sudoer user to the server before blocking root access.

Shell
 
# Add a user

sudo useradd -m {username}

    

# Set a password for new user

sudo passwd {username}



# Add the new user to sudoers' list

usermod -aG sudo {username}


Next, you should disable root login from ssh_config (you may need to install nano using apt install nano):

Shell
 
# Open SSH config file

nano /etc/ssh/sshd_config


Find PermitRootLogin, uncomment the line by removing # and set to no. The final state of this parameter should be as shown.

Removing root login from sshd config file

Then, after saving the file, you need to restart ssh service.

Shell
 
systemctl restart sshd


Note: For many operations on the server, you need an sudo access; so after login using the new user, you can switch back to root using su command.

Shell
 
su root


Step 3 — Change ssh Port Number: SSH uses a default port 22 that can be modified in the sshd_config config file. This change adds another complexity for connecting to the server because the port number has to be provided in the login. To this end, you must open the file again and change the value  Port to another number like 12345. Likewise, you need to restart ssh the service again. Remember that after changing the default port number, you should provide that in the login.

Shell
 
ssh -p [NEW_PORT_NUMBER] [USER]@[SERVER_IP_Address]

# example: ssh -p 12345 admin@8.8.8.8


Conclusion

It is worth understanding how to tackle security risks. Although it is difficult to guarantee the server's safety, adding complexity is a reasonable and effective approach for server hardening. Crawlers and bots must make much more effort to do their evil meaning.

Linux (operating system) Computing Engineer Bash (Unix shell) Interface (computing)

Published at DZone with permission of Hadi Samadzad. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • DevOps for Developers — Introduction and Version Control
  • A Deep Dive Into AIOps and MLOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: