Ubuntu Server Initial Setup Guide
-
Initial Server Setup on Ubuntu
When you first spin up a new Ubuntu server, getting right to work is tempting. However, before you jump into deploying web applications, hosting a NodeBB forum, or installing Nginx, it is crucial to establish a secure foundation.
Relying on the default
rootaccount for day-to-day operations is a security risk. This guide will walk you through the essential first steps for any newly provisioned Ubuntu server: creating a dedicated user, granting the right privileges, and configuring a basic firewall.Step 1: Log In as Root
To begin, you will need your server's public IP address and the password (or SSH private key if you installed an SSH key for authentication) for the
rootaccount. Open your terminal and connect to your server as therootuser using the following command, replacingyour_server_ipwith your actual IP address:ssh root@your_server_ipIf a warning about host authenticity appears, accept it. If your server uses password authentication, provide your
rootpassword to log in. If your server uses an SSH key for authentication, you may need to enter SSH key passphrase the first time you use the key each session.Note: The
rootuser has absolute power over the system. Because of the heightened privileges of therootaccount, you are discouraged from using it regularly. Therootaccount can make very destructive changes, even by accident.Step 2: Create a New User
Now that you are logged in as
root, let's create a standard user account. We will use this account moving forward. You can name this user whatever you like; for this example, we'll useadminuser.adduser adminuserThe system will prompt you to create and confirm a strong password. You will also see prompts for additional information (like Full Name or Room Number). These are entirely optional—feel free to just press ENTER to skip them.
Step 3: Grant Administrative Privileges
Our new user needs the ability to perform administrative tasks when necessary, like updating packages or managing server blocks later on. Instead of logging back in as
root, we can grant this user superuser capabilities.On Ubuntu, users in the
sudogroup are automatically allowed to run administrative commands. Add your new user to this group:usermod -aG sudo adminuserFrom now on, whenever you need to execute a command with elevated privileges, you can simply type
sudobefore it.Step 4: Set Up a Basic Firewall
Ubuntu ships with a firewall configuration tool called UFW (Uncomplicated Firewall). Setting it up ensures that only authorized traffic can reach your server.
When software is installed, it often registers its profile with UFW. Because we need to maintain our connection to the server, we must ensure SSH traffic is allowed before turning the firewall on.
First, check the available application profiles:
ufw app listYou should see OpenSSH in the output, it will probably look like this:
Available applications: OpenSSHAllow connections for OpenSSH by running:
ufw allow OpenSSHNow, it is safe to enable the firewall:
ufw enableType y and press ENTER to confirm. You can verify that your firewall is active and that SSH connections are allowed by checking its status:
ufw statusAnd in the output, it will probably look like this:
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6)The firewall is currently blocking all connections except for SSH. As you continue building your server environment—such as opening ports 80 and 443 for web traffic later—you will adjust these UFW rules to allow the new services:
ufw allow 80/tcp ufw allow 443/tcpStep 5: Verify External Access for Your Regular User
Before closing your active root session, it is critical to verify that you can successfully log in and use
sudowith your new user. If you have problems connecting, you can troubleshoot and make any necessary changes asroot.Configuring SSH access for your new user depends on whether your server’s
rootaccount uses a password or SSH keys for authentication.If the root account uses password authentication
Open a new terminal window on your local machine and try connecting as your new user:
ssh adminuser@your_server_ipWhen the terminal prompts for password of your new user, enter it then you will be logged in.
Remember, if you need to run a command with administrative privileges, type
sudobefore it like this:sudo command_to_runYou will receive a prompt for your user's password when using
sudofor the first time each session (and periodically afterward).If the root account uses SSH key authentication
If you logged in to your
rootaccount using SSH keys, then password authentication is disabled for SSH. To log in as your regular user with an SSH key, you must add a copy of your local public key to your new user’s~/.ssh/authorized_keysfile.Since your public key is already in the
rootaccount’s~/.ssh/authorized_keysfile on the server, you can copy that file and directory structure to your new user account using your current session.The simplest way to copy the files with the correct ownership and permissions is with the
rsynccommand. This command will copy therootuser’s.sshdirectory, preserve the permissions, and modify the file owners, all in a single command. Make sure to changeadminuserof the command below to match your regular user’s name:rsync --archive --chown=adminuser:adminuser ~/.ssh /home/adminuserNow, open up a new terminal session on your local machine, and use SSH with your new username:
ssh adminuser@your_server_ipYou should be connected to your server with the new user account without using a password. Remember, if you need to run a command with administrative privileges, type
sudobefore the command like this:sudo command_to_runYou will be prompted for your regular user’s password when using
sudofor the first time each session (and periodically afterward).
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login