- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Initial Server Setup with Ubuntu 20.04 18.04 and 16.04
Introduction
Setting up a server is an essential step in creating a reliable and secure environment for hosting websites, applications, or managing data. In this article, we will explore the process of setting up an Ubuntu server, specifically versions 20.04, 18.04, and 16.04. We will cover the necessary steps, along with practical examples and their corresponding outputs, to help you navigate through the initial server setup process seamlessly.
Prerequisites
Before diving into the server setup, ensure that you have the following prerequisites in place −
An Ubuntu 20.04, 18.04, or 16.04 server instance.
A user account with administrative privileges (or root access).
An SSH client to connect to the server remotely (e.g., OpenSSH).
Updating and Upgrading the System
To begin, it is crucial to update and upgrade the system to ensure that you have the latest software packages and security patches. Execute the following commands −
$ sudo apt update
This command updates the package lists for upgrades and new installations.
$ sudo apt upgrade
This command upgrades all installed packages to their latest versions.
Creating a New User
While it is advisable to avoid using the root account for day-to-day tasks, creating a new user with sudo privileges is considered a best practice. Follow the example below −
$ sudo adduser example_user
Replace "example_user" with your desired username. The system will prompt you to set a password and provide additional information for the new user.
Granting Administrative Privileges
To grant administrative privileges to the newly created user, you need to add them to the sudo group. Execute the following command −
$ sudo usermod -aG sudo example_user
This command adds the user to the sudo group, allowing them to execute commands with administrative privileges.
Configuring SSH Access
Secure Shell (SSH) allows secure remote access to your server. You can configure SSH to use key-based authentication instead of passwords for enhanced security. Perform the following steps −
Generate an SSH key pair on your local machine (if you don't have one) −
$ ssh-keygen -t rsa -b 4096
Copy the public key to the server −
$ ssh-copy-id example_user@your_server_ip
Disable password authentication (optional but recommended) −
Edit the SSH configuration file −
$ sudo nano /etc/ssh/sshd_config
Change the following line −
PasswordAuthentication no
Save the file and restart the SSH service −
$ sudo systemctl restart sshd
Firewall Configuration
Enabling a firewall helps protect your server from unauthorized access. Ubuntu provides UFW (Uncomplicated Firewall) as a straightforward way to manage firewall rules. Follow the example below −
Allow SSH connections −
$ sudo ufw allow OpenSSH
Enable the firewall −
$ sudo ufw enable
Verify the firewall status −
$ sudo ufw status
Setting Up a Basic Firewall Rule
Sometimes, you may need to allow or deny access to specific ports. The following example demonstrates how to allow HTTP traffic −
$ sudo ufw allow 80/tcp
This command allows incoming TCP traffic on port 80, which is used for HTTP.
Enabling Automatic Security Updates
Keeping your server up to date is crucial for security. Ubuntu provides the "unattended-upgrades" package for automating security updates. Execute the following steps −
Install the package −
$ sudo apt install unattended-upgrades
Enable automatic updates −
$ sudo dpkg-reconfigure --priority=low unattended-upgrades
Follow the prompts to configure the update preferences.
Securing SSH Access with Key-Based Authentication
To further enhance the security of your SSH access, you can disable password authentication and rely solely on key-based authentication. Follow the steps below −
Edit the SSH configuration file −
$ sudo nano /etc/ssh/sshd_config
Locate the line that specifies PasswordAuthentication and change its value to no −
PasswordAuthentication no
Uncomment the line that specifies PubkeyAuthentication to enable key-based authentication −
PubkeyAuthentication yes
Save the file and restart the SSH service −
$ sudo systemctl restart sshd
Now, you can only access the server using the private key associated with the public key you added earlier.
Configuring Timezone and NTP
Properly setting the timezone and ensuring accurate time synchronization is important for various server functions and applications. To configure the timezone and enable NTP (Network Time Protocol), follow these steps −
Check the current timezone −
$ timedatectl show
Set the desired timezone by using the timedatectl command with the set-timezone option. For example, to set the timezone to "America/New_York" −
$ sudo timedatectl set-timezone America/New_York
Install NTP for time synchronization −
$ sudo apt install ntp
Start and enable the NTP service −
$ sudo systemctl start ntp $ sudo systemctl enable ntp
Installing and Configuring a Web Server (Apache)
If you intend to host websites or web applications on your Ubuntu server, installing a web server is essential. In this example, we will install and configure Apache, one of the most popular web servers. Execute the following steps −
Install Apache −
$ sudo apt install apache2
Adjust the firewall to allow incoming HTTP and HTTPS traffic −
$ sudo ufw allow 'Apache'
Verify the status of the Apache service −
$ sudo systemctl status apache2
Open a web browser and enter your server's IP address. You should see the default Apache page, confirming a successful installation.
Installing and Configuring a Database Server (MySQL)
If your server requires a database management system, MySQL is a powerful and widely used option. Follow these steps to install and configure MySQL −
Install MySQL Server −
$ sudo apt install mysql-server
Secure the MySQL installation −
$ sudo mysql_secure_installation
Follow the prompts to set the root password and improve the security of your MySQL installation.
Verify the status of the MySQL service −
$ sudo systemctl status mysql
Updating the System Regularly
To ensure the ongoing security and stability of your server, it is important to keep the system up to date with the latest software updates and security patches. Execute the following command to update the system regularly −
$ sudo apt update && sudo apt upgrade -y
This command will update the package lists and upgrade all installed packages, applying any available updates.
Conclusion
By following the comprehensive guide above, you have learned how to perform the initial server setup on Ubuntu 20.04, 18.04, and 16.04. We covered various aspects, including user creation, SSH configuration, firewall setup, securing SSH access, configuring timezone and NTP, installing and configuring a web server (Apache), installing and configuring a database server (MySQL), and the importance of updating the system regularly. With this knowledge, you are now equipped to set