Introduction
When you first create a new Debian 8 server, there are a few configuration steps that you should take early on as part of the basic setup. This will increase the security and usability of your server and will give you a solid foundation for subsequent actions.
Step One — Root Login
If you are not already connected to your server, go ahead and log in as the root
user using the following command (substitute the highlighted word with your server's public IP address):
ssh root@SERVER_IP_ADDRESS
Complete the login process by accepting the warning about host authenticity, if it appears, then providing your root authentication (password or private key). If it is your first time logging into the server, with a password, you will also be prompted to change the root password.
Step Two — Create a New User
Once you are logged in as root
, we're prepared to add the new user account that we will use to log in from now on.
This example creates a new user called youruser
but you should replace it with a user name that you like:
adduser youruser
You will be asked a few questions, starting with the account password.
Enter a strong password and, optionally, fill in any of the additional information if you would like. This is not required and you can just hit "ENTER" in any field you wish to skip.
Step Three — Root Privileges
Now, we have a new user account with regular account privileges. However, we may sometimes need to do administrative tasks.
To avoid having to log out of our normal user and log back in as the root account, we can set up what is known as "super user" or root privileges for our normal account. This will allow our normal user to run commands with administrative privileges by putting the word sudo
before each command.
Install Sudo
Debian 8 doesn't come with sudo
installed, so let's install it with apt-get.
First, update the apt package index:
apt-get update
Then use this command to install sudo:
apt-get install sudo
Now you are able to use the sudo
and visudo
commands.
Grant Sudo Privileges
To add these privileges to our new user, we need to add the new user to the "sudo" group. By default, on Debian 8, users who belong to the "sudo" group are allowed to use the sudo
command.
As root
, run this command to add your new user to the sudo group (substitute the highlighted word with your new user):
adduser youruser sudo
Now your user can run commands with super user privileges!
Step Four — Add Public Key Authentication (Recommended)
The next step in securing your server is to set up public key authentication for your new user. Setting this up will increase the security of your server by requiring a private SSH key to log in.
Generate a Key Pair
If you do not already have an SSH key pair, which consists of a public and private key, you need to generate one. If you already have a key that you want to use, skip to the Copy the Public Key step.
To generate a new key pair, enter the following command at the terminal of your local machine (ie. your computer):
ssh-keygen
Assuming your local user is called "youruser", you will see output that looks like the following:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/youruser/.ssh/id_rsa):
Hit return to accept this file name and path (or enter a new name).
Next, you will be prompted for a passphrase to secure the key with. You may either enter a passphrase or leave the passphrase blank.
Note: If you leave the passphrase blank, you will be able to use the private key for authentication without entering a passphrase. If you enter a passphrase, you will need both the private key and the passphrase to log in. Securing your keys with passphrases is more secure, but both methods have their uses and are more secure than basic password authentication.
This generates a private key, id_rsa
, and a public key, id_rsa.pub
, in the .ssh
directory of the localuser's home directory. Remember that the private key should not be shared with anyone who should not have access to your servers!
Copy the Public Key
If your local machine has the ssh-copy-id
script installed, you can use it to install your public key to any user that you have login credentials for.
Run the ssh-copy-id
script by specifying the user and IP address of the server that you want to install the key on, like this:
ssh-copy-id youruser@SERVER_IP_ADDRESS
After providing your password at the prompt, your public key will be added to the remote user's .ssh/authorized_keys
file. The corresponding private key can now be used to log into the server.
Now you may SSH login as your new user, using the private key as authentication.
To read more about how key authentication works, read this tutorial: How To Configure SSH Key-Based Authentication on a Linux Server.
Step Five — Configure SSH
Now that we have our new account, we can secure our server a little bit by modifying its SSH daemon configuration (the program that allows us to log in remotely) to disallow remote SSH access to the root account.
Begin by opening the configuration file with your text editor as root:
nano /etc/ssh/sshd_config
Here, we have the option to disable root login through SSH. This is generally a more secure setting since we can now access our server through our normal user account and escalate privileges when necessary.
To disable remote root logins, we need to find the line that looks like this:
#PermitRootLogin yes
You can modify this line to "no" like this if you want to disable root login:
PermitRootLogin no
Disabling remote root login is highly recommended on every server!
When you are finished making your changes, save and close the file using the method we went over earlier (CTRL-X
, then Y
, then ENTER
).
Reload SSH
Now that we have made our changes, we need to restart the SSH service so that it will use our new configuration.
Type this to restart SSH:
systemctl restart ssh
Now, before we log out of the server, we should test our new configuration. We do not want to disconnect until we can confirm that new connections can be established successfully.
Open a new terminal window. In the new window, we need to begin a new connection to our server. This time, instead of using the root account, we want to use the new account that we created.
ssh youruser@SERVER_IP_ADDRESS
You will be prompted for the new user's password that you configured. After that, you will be logged in as your new user.
Remember, if you need to run a command with root privileges, type "sudo" before it like this:
sudo command_to_run
If all is well, you can exit your sessions by typing:
exit