Self Host 101 - Set up and Secure Your Own Server

Syntax
21 Mar 202425:56

Summary

TLDRThis video tutorial offers a comprehensive guide to securing a newly created Virtual Private Server (VPS). It begins by highlighting the importance of locking down a VPS to protect against relentless hacking attempts. The host, CJ, demonstrates how to update and upgrade the VPS to its latest secure state, change default root passwords, and create a non-root user with sudo privileges. The tutorial also covers setting up SSH key-based authentication to replace passwords, disabling root login via SSH, and configuring firewall rules to restrict access to necessary ports only. Finally, it introduces the setup of unattended-upgrades to keep the system updated automatically, ensuring a secure and well-maintained server environment.

Takeaways

  • πŸ›‘οΈ The importance of securing a VPS from the constant threat of automated hacking attempts targeting default login credentials.
  • πŸ”‘ Demonstration of checking for SSH login attempts to identify any unauthorized access to a new VPS.
  • πŸ”’ Basic steps for setting up and securing a VPS, including running updates and upgrades to protect against vulnerabilities.
  • πŸ‘₯ Introduction to the concept of least privilege by creating a secondary user with limited permissions to perform administrative tasks when necessary.
  • πŸ”„ The necessity of keeping the system updated with the latest security patches through regular package upgrades.
  • πŸ”’πŸ”‘ Transitioning from password-based SSH logins to key-based authentication for enhanced security.
  • 🚫 Disabling password authentication in SSH to prevent brute force attacks.
  • πŸ”’πŸš« Prohibiting SSH access for the root user to further secure the server against unauthorized access.
  • πŸ”„ Using application firewalls like UFW to control network traffic and close unnecessary ports.
  • πŸ”„πŸ”’ Configuring the firewall to allow traffic only from specific IP addresses to limit exposure.
  • πŸ€– Implementing unattended-upgrades to automate the process of keeping the system updated with the latest security patches.

Q & A

  • What is the main purpose of the video?

    -The main purpose of the video is to guide viewers on setting up and securing a new Virtual Private Server (VPS) to prevent unauthorized access and potential attacks.

  • Why is it important to check for SSH login attempts on a new VPS?

    -It is important to check for SSH login attempts because hackers are constantly running automated scripts to exploit vulnerable servers, and being aware of such attempts can help in taking preventive measures.

  • What does the video offer for different types of users interested in VPS?

    -The video offers a basic guide suitable for complete beginners, hobbyists, and developers, showing them how to create a secure and locked-down VPS ready for various services.

  • What are some examples of self-hosted applications one might run on a VPS?

    -Examples include personal media servers like Plex or Jellyfin, cloud services like Nextcloud, photo hosting with PhotoPrism, password managers like Bitwarden or Passbolt, custom Discord bots, web servers like Nginx or Apache, and databases like MySQL, PostgreSQL, MongoDB, or Redis.

  • What is the difference between a VPS and PaaS?

    -A VPS provides a virtual machine with full control over the operating system and software, while PaaS (Platform as a Service) offers a platform to deploy applications without managing the underlying infrastructure.

  • How does one connect to a VPS using SSH?

    -One connects to a VPS using SSH by opening a terminal, typing 'ssh' followed by the username and the IP address of the VPS, and then entering the password when prompted.

  • Why is it recommended to run updates and upgrades immediately after connecting to a new VPS?

    -Running updates and upgrades ensures that the VPS is running the latest versions of all packages, which often include security patches and bug fixes, thus making the system more secure.

  • What is the principle of least privilege and how does it relate to VPS security?

    -The principle of least privilege suggests giving a user only the permissions they need to perform their tasks. In the context of VPS security, it means not running all commands as the root user to minimize potential damage from any security breaches.

  • How can one create a secondary user on a VPS with limited permissions?

    -One can create a secondary user with the 'adduser' command and then add this user to the 'sudo' group to grant them the ability to perform superuser actions when necessary, without having root privileges all the time.

  • Why is it advised to change the default password for the root user on a new VPS?

    -Changing the default root password is advised to prevent unauthorized access, as the default password provided by the VPS provider could be known or easily guessed.

  • What is SSH key-based authentication and how does it enhance security?

    -SSH key-based authentication uses a pair of cryptographic keys, a public key and a private key, to authenticate the user to the server without the need for a password. This method enhances security by making it harder for unauthorized users to gain access, even if they know the username.

  • How can one disable password authentication for SSH on a VPS?

    -One can disable password authentication for SSH by editing the 'sshd_config' file, setting 'PasswordAuthentication' to 'no', and then restarting the SSH service.

  • What is the purpose of changing the default SSH port on a VPS?

    -Changing the default SSH port can help deter automated attacks that target the standard port (22), as it forces potential attackers to discover the new port number before they can attempt to exploit it.

  • How can one restrict SSH access to specific IP addresses?

    -One can restrict SSH access by setting up firewall rules that only allow connections from specified IP addresses, using either the VPS provider's dashboard or command-line tools like 'ufw'.

  • What is 'unattended-upgrades' and why is it useful for a VPS?

    -'unattended-upgrades' is a program that automatically installs security and other updates on a system without user intervention. It is useful for a VPS as it helps keep the system up-to-date with the latest security patches and software updates, reducing the risk of vulnerabilities.

  • How can one ensure that automatic updates are enabled on a VPS?

    -One can ensure automatic updates are enabled by installing the 'unattended-upgrades' package and then running 'dpkg-reconfigure' to enable automatic upgrades through a configuration wizard.

Outlines

00:00

πŸ›‘οΈ VPS Security Basics and Setup

This paragraph introduces the importance of securing a Virtual Private Server (VPS) against constant hacking attempts. The narrator, CJ, explains the need to monitor login attempts and the benefits of running a command to reveal such activities. The video aims to provide a series of tutorials on self-hosting with a VPS, starting with the essentials of setting up and securing a server. The target audience ranges from beginners to developers interested in managing their own servers. The video promises to cover the creation of a secure, service-ready VPS, with potential future topics including turning the VPS into a web or database server.

05:00

πŸ”„ Updating and Upgrading VPS for Security

The second paragraph focuses on the initial steps to secure a VPS, starting with updating and upgrading the system to the latest versions. The narrator demonstrates how to use the 'apt' command to update package lists and upgrade installed packages on an Ubuntu-based system. The importance of keeping the system up-to-date to protect against vulnerabilities exploited by hackers is emphasized. The paragraph also covers handling kernel upgrades, which require a system reboot, and checking for the need to reboot after updates are installed.

10:02

πŸ”‘ Changing Root Password and Introducing Principle of Least Privilege

This section discusses the importance of changing the default root password upon the first connection to a VPS. It introduces the principle of least privilege, advocating for the creation of a secondary user with limited permissions to perform superuser tasks only when necessary. The process of adding a new user, setting a unique password, and adding the user to the 'sudo' group to allow for elevated permissions when required is detailed. The narrator also explains how to verify group membership and test the new user's ability to perform superuser actions using the 'sudo' command.

15:03

πŸ”’ Transitioning to SSH Key Authentication

The fourth paragraph outlines the process of transitioning from password-based SSH authentication to key-based authentication to enhance security. It explains the necessity of having an SSH key pair generated on the local machine and the steps to add the public key to the VPS's 'authorized_keys' file. The narrator demonstrates how to edit the file using 'nano' and the importance of using the SSH key for authentication instead of a password, thereby preventing brute-force attacks.

20:04

🚫 Disabling Password Login and Root SSH Access

This section describes the steps to disable password-based login entirely and prevent root user SSH access to further secure the VPS. The narrator guides through editing the 'sshd_config' file to set 'PasswordAuthentication' to 'no' and 'PermitRootLogin' also to 'no'. The importance of restarting the SSH service to apply changes is highlighted. The paragraph concludes with a test to ensure the new settings prevent password logins and root SSH access, reinforcing the VPS's security posture.

25:05

πŸ”„ Network Firewall Management and SSH Port Customization

The paragraph discusses network security by managing firewall policies and customizing the SSH port to deter automated attacks. The narrator explains how to close unnecessary ports and change the default SSH port to a different number to avoid common automated scripts. The use of 'ufw' for firewall management through the command line is introduced, as well as the option to restrict access to specific IP addresses. The paragraph also touches on the convenience of leaving SSH on the default port and the importance of regularly updating the system for security.

🌐 Final Security Measures and Future VPS Plans

The final paragraph wraps up the security measures by installing 'unattended-upgrades' to ensure the system stays updated automatically. The narrator explains how to enable and configure the service to apply updates automatically, including security and other package updates. The paragraph also hints at future videos in the series that will cover setting up web servers, databases, and SSL certificates on the VPS. The narrator invites viewers to share feedback and corrections to improve the tutorial's accuracy and usefulness.

Mindmap

Keywords

πŸ’‘VPS

VPS stands for Virtual Private Server, which is a virtual machine sold as a service by an internet hosting service. It is a software emulation of a computer that runs its own operating system and applications, allowing users to have full control over the server environment. In the video, the main theme revolves around setting up and securing a VPS to prevent unauthorized access and to serve various hosting needs such as web servers, databases, and personal media servers.

πŸ’‘SSH

SSH, or Secure Shell, is a cryptographic network protocol for operating network services securely over an unsecured network. In the script, SSH is used for securely connecting to the VPS, allowing the user to execute commands and manage the server remotely. The video emphasizes the importance of securing SSH access by using SSH keys and disabling password-based logins to enhance server security.

πŸ’‘Automated Scripts

Automated scripts refer to pre-programmed sequences of instructions that hackers use to attempt unauthorized access to servers. In the context of the video, these scripts are mentioned as a constant threat that runs 24/7 trying to exploit vulnerable servers, highlighting the necessity of taking security measures to protect the VPS.

πŸ’‘Security

Security in the video pertains to the measures taken to protect the VPS from unauthorized access and potential attacks. This includes updating and upgrading the server, changing default passwords, using SSH keys for authentication, and configuring firewalls to restrict access to certain ports and services.

πŸ’‘Principle of Least Privilege

The principle of least privilege is a security concept where a user is given the minimum levels of access necessary to perform their job functions. In the video, this principle is applied by creating a secondary user with limited permissions, avoiding the use of the root user for routine tasks to minimize potential damage from security breaches.

πŸ’‘SSH Key

An SSH key is a pair of cryptographic keys used for authenticating to an SSH server. The video script describes the process of setting up an SSH key for secure login to the VPS without the need for a password, which is a critical step in enhancing the security of the server against brute force attacks.

πŸ’‘Firewall

A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. The script discusses using a firewall to control which ports are open and accessible, and to restrict access to specific IP addresses, as part of the broader security strategy for the VPS.

πŸ’‘Unattended-Upgrades

Unattended-Upgrades is a feature in some Linux distributions, including Ubuntu, that allows the system to automatically install security updates. In the video, the presenter explains how to enable this feature to ensure the VPS stays up-to-date with the latest security patches without manual intervention.

πŸ’‘Port

In the context of networking, a port is an endpoint of communication in the Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). The script mentions ports in relation to the SSH service, which by default listens on port 22, and the practice of changing the default SSH port to avoid automated attacks, as well as closing unnecessary ports to tighten security.

πŸ’‘Root User

The root user, often referred to as the 'superuser,' has the highest level of access to a system, allowing the execution of any command without restriction. The video emphasizes the security risk of using the root user for daily tasks and recommends disabling SSH access for the root user to further secure the VPS.

Highlights

Introduction to securing a new VPS against automated hacking attempts.

Demonstration of checking SSH login attempts to identify vulnerabilities.

Series overview on self-hosting with VPS, including turning it into a web or database server.

Explanation of VPS uses, such as hosting personal media servers or cloud services.

Discussion on the benefits and considerations of self-hosting applications and databases.

Comparison of VPS with other hosting options like PaaS in episode 730 of Syntax.

Instructions on setting up a VPS with Ubuntu, including system requirements and initial setup.

SSH connection tutorial for accessing the VPS using a terminal.

First steps after logging into a VPS, such as updating and upgrading the system.

Importance of keeping the VPS updated to protect against exploits.

How to change the root user's password for enhanced security.

Principle of least privilege and creating a secondary user with limited permissions.

Enabling a new user to perform superuser actions when necessary.

Transitioning from root to a new user for daily tasks to improve security.

Setting up SSH key authentication to replace password-based logins.

Disabling password login to prevent brute force attacks.

Preventing root user login via SSH to further secure the server.

Managing network and firewall policies to control inbound connections.

Using ufw or provider dashboard to manage open ports and close unnecessary ones.

Changing the default SSH port to deter automated attacks.

Restricting specific ports to certain IP addresses for enhanced security.

Installing and configuring unattended-upgrades for automatic system updates.

Customizing update settings and ensuring the service is active for ongoing security.

Conclusion and invitation for feedback on securing a VPS for practical applications.

Transcripts

play00:00

you are looking at SSH login attempts to

play00:02

a brand new virtual private server that

play00:04

I just created and if you have your own

play00:06

VPS and you haven't taken the steps to

play00:08

lock it down try running this command to

play00:10

see if there are login attempts

play00:12

happening and you'll be surprised to

play00:13

what you find because there are hackers

play00:15

running automated scripts 24/7 trying to

play00:17

find and exploit vulnerable servers and

play00:19

you don't want to be a victim of this

play00:21

now in this video I'm going to show you

play00:23

the basics of setting up and locking

play00:24

down a VPS to prevent these login

play00:26

attempts and automatically block other

play00:27

types of attacks as well this will be

play00:30

the first video in a series on

play00:32

self-hosting with the VPS so if this

play00:34

kind of thing interests you let us know

play00:35

in the comments and also stick around

play00:37

for future videos in the series where we

play00:38

take this VPS and turn it into a web

play00:40

server or a database server now whether

play00:43

you're a complete beginner a hobbyist or

play00:44

a developer that wants to start managing

play00:46

their own servers this video will show

play00:48

you everything you need to know to

play00:49

create a secure locked down VPS that's

play00:51

ready to run any service you throw at it

play00:53

so let's jump in I'm CJ welcome to

play00:58

sentense

play01:04

now if you're new to the world of vpss

play01:06

welcome there are a lot of things you

play01:07

can do with them and a lot of reasons

play01:09

why you would want to manage one

play01:10

yourself now a VPS is just one way to

play01:12

host things on the web if you're

play01:14

interested in learning about more check

play01:15

out episode 615 of syntax where Wes and

play01:18

Scott compare the different kinds of

play01:19

hosting and the various providers now

play01:21

like I mentioned you can do a lot of

play01:22

things with a VPS and one of the reasons

play01:24

people typically start learning about

play01:26

and looking into VPS is to self-host uh

play01:29

things like a personal Media Server Like

play01:31

Plex jellyfin or MB or host their own

play01:34

cloud with something like nexcloud or

play01:35

host photos with something like Photo

play01:37

prism or host your own password manager

play01:39

with like bit Warden or passbolt uh but

play01:42

you can also self-host your own

play01:43

applications and web services things

play01:45

like a custom Discord bot or some long

play01:47

running process or like a websocket

play01:49

server or self-host your own instance of

play01:51

Sentry you could also run web servers

play01:53

like enginex Apache or caddy you can

play01:55

host databases like MySQL postgress

play01:57

mongodb or reddis and others and

play01:59

ultimately reduce your dependency on

play02:01

managed services like versell netlify

play02:02

Heroku render Railway fly.io am I

play02:05

missing any and also manage database

play02:07

services like AWS RDS Planet scale neon

play02:09

and others now in episode 730 of syntax

play02:12

Wes and Scott talk about some of these

play02:14

paas or platform as a Service

play02:16

Alternatives they talk about things like

play02:18

uh piku doku Kubo cap Rover kifi

play02:22

probably a couple others and these all

play02:23

allow you to get the same functionality

play02:25

as verell or netlify but running on your

play02:27

own server now managing a VPS is not not

play02:30

for the faint of heart it requires

play02:31

constant attention and maintenance which

play02:33

is why managed Services exist and why

play02:35

this type of thing is typically a

play02:36

full-time job but if this kind of thing

play02:38

still interests you let's get into it

play02:41

now the next part of this video will

play02:42

assume that you want to set up a VPS and

play02:44

you have acquired one so like I

play02:45

mentioned check out episode 615 of

play02:47

syntax if you'd like to know where to

play02:49

get one and more about these various

play02:51

hosting providers now I'm going to be

play02:53

setting up a tiny VPS it's running

play02:55

Ubuntu

play02:56

22.041 gig of RAM 1 CPU and 10 GB of dis

play03:00

space now this video is not sponsored by

play03:02

any VPS hosting or domain provider so

play03:04

I'm going to keep things fairly generic

play03:06

so you can use whatever you can afford

play03:07

and whatever you prefer now depending on

play03:09

the type of workload you're going to be

play03:11

running on this VPS you might need

play03:12

better specs but for me I'm going to

play03:14

have a few static sites a few node.js

play03:16

applications and a couple databases so

play03:18

the minimal specs that I mentioned will

play03:20

work for what I need to

play03:26

do now let's connect to rvps for this

play03:29

we're going to use a thing called SSH

play03:32

and you're going to need a terminal to

play03:33

do this now on a Mac I am using an app

play03:36

called iterm you can also use the

play03:37

buil-in terminal on a Mac if you're on

play03:39

Windows there's a thing called Windows

play03:40

terminal if you're on Linux just search

play03:42

for the app called terminal and open it

play03:45

up and we're going to want to run the

play03:46

command SSH so once it's open we're

play03:48

going to type SSH and then we'll need to

play03:50

type the username that has been set up

play03:52

for us on the VPS now this is going to

play03:54

vary by where you got your VPS for me

play03:57

the VPS username is root and we're going

play04:00

to log in with that you yours might be

play04:02

admin or Ubuntu or Debian uh it might

play04:05

vary but for me it's root and then after

play04:07

the at sign we're going to put in the IP

play04:09

address of our server now this IP

play04:11

address will be listed in whatever

play04:12

dashboard of wherever you got your VPS

play04:14

but this is a public IP address and that

play04:16

means anyone in the world that's on the

play04:18

internet will be able to access uh the

play04:20

server at this IP address just like

play04:21

we're about to access this server now

play04:23

you should have a command that's SSH

play04:24

username at IP address hit enter and now

play04:27

it's going to attempt to connect now the

play04:28

first time you're ever connected ing to

play04:30

a server you're going to see this

play04:31

warning and here you can just type yes

play04:33

and what this is doing is it's storing

play04:35

the fingerprint of this server on your

play04:37

computer so the next time you connect

play04:39

it's going to validate that

play04:40

fingerprint now uh the next time you

play04:43

connect you shouldn't see this warning

play04:44

and if you ever see this warning again

play04:46

and you haven't changed anything on the

play04:47

server then that means something has

play04:50

gone wrong something is potentially

play04:51

compromised like the the SSH key on the

play04:53

server has actually changed and

play04:55

sometimes there's a legitimate reason

play04:57

for that uh but just so you know you

play05:00

should only see that warning the first

play05:01

time you connected the server and you

play05:02

shouldn't see it anytime after that now

play05:04

we'll need to type in our password now

play05:06

as you type you're not going to see

play05:07

anything on the screen here in the

play05:08

terminal but as long as you type

play05:11

everything correctly you should get

play05:12

connected to your

play05:18

server now one of the first things you

play05:20

should do when you connect to your VPS

play05:22

for the first time is run updates and

play05:24

upgrades so like I mentioned earlier I'm

play05:27

running Ubuntu

play05:29

will work if you're on an Ubuntu or dbn

play05:31

based system now depending on what your

play05:33

VPS has set up for you if you are not

play05:36

connecting as the root or super user

play05:38

then all of these commands you're going

play05:40

to need to type pseudo in front of so

play05:42

like pseudo apt upgrade or pseudo apt

play05:44

install for me because I'm running as

play05:46

the root user I will not need to put

play05:48

pseudo in front of those commands but if

play05:49

you try running those commands and it

play05:50

says you don't have permission put

play05:52

pseudo in front of them so the first

play05:54

thing we're going to do is just say apt

play05:55

update now this command isn't going to

play05:57

actually upgrade anything it's just

play05:59

going to to update the package lists

play06:01

locally on your machine so when you do

play06:02

run an upgrade or you do run an install

play06:04

it's always grabbing the latest versions

play06:06

from those specific repositories so now

play06:09

that we have the latest package list you

play06:11

can see that it says 76 packages can be

play06:13

upgraded so we're going to run the

play06:15

upgrade command so I'll just do apt

play06:18

upgrade and that's going to grab and

play06:20

install all of the latest versions of

play06:21

all the packages that I have installed

play06:24

and we can confirm this with Y and it'll

play06:26

start downloading and and upgrading

play06:28

everything now you might be wondering

play06:30

why are we upgrading one of the ways to

play06:32

keep your VPS secure is to always be

play06:35

running the latest versions of all

play06:37

packages because when hackers attempt to

play06:40

gain access to a machine or exploit a

play06:42

machine typically they're taking

play06:43

advantage of old broken versions and so

play06:47

anytime a a package has an upgrade a lot

play06:50

of times it's potentially a bug fix or a

play06:51

vulnerability fix and so we always want

play06:53

to make sure we're always running the

play06:55

latest versions of all packages just to

play06:57

make sure that we always have the latest

play06:58

fixes and patches for for anything that

play07:00

we're using on our system now when you

play07:01

run this upgrade you may or may not see

play07:03

this notice that there is a pending

play07:05

kernel upgrade now the kernel is like

play07:07

the root of the operating system and

play07:09

this is another thing that could

play07:10

potentially be be vulnerable so we

play07:11

always want to be running the latest

play07:13

version of the kernel now if you upgrade

play07:14

the kernel you are going to have to

play07:16

restart the machine which is why we

play07:18

typically want to run these commands

play07:19

before we do anything else on the VPS

play07:21

because we're going to need to reboot it

play07:23

a few times before we can start running

play07:24

our own applications so I am going to do

play07:27

the kernel upgrade here and then uh from

play07:29

there will'll likely restart our

play07:31

machine now this is asking me what

play07:33

services should be restarted uh like I

play07:35

said we are going to restart the entire

play07:37

machine so we actually don't really even

play07:39

have to worry about this I can just okay

play07:40

through it because once we reboot the

play07:42

whole machine then all of these services

play07:43

will be restarted anyways now that all

play07:46

of the upgrades have run and in my case

play07:47

I also upgraded the kernel you can do

play07:50

this to determine if you need to reboot

play07:52

so if you do an LS of /var run/ reboot

play07:55

required if that exists that means your

play07:58

machine needs to be rebooted and uh

play08:00

there are different ways to do this you

play08:01

can actually do this from the command

play08:03

line if you just type reboot but you can

play08:05

also do it from your VPS dashboard and

play08:06

that's how I'm going to do it just

play08:07

because I want to make sure that this

play08:08

thing actually reboots because if you

play08:10

accidentally shut down the machine and

play08:12

there there's no way to restart it in

play08:14

your dashboard you're going to have to

play08:16

reach out to like to the support team of

play08:18

where wherever you're hosting your VPS

play08:19

so for my VPS there's a dashboard where

play08:22

I can literally say restart VM so I am

play08:25

going to exit from my SSH session here

play08:28

go to my dashboard and restart the VPS

play08:31

now that the machine has rebooted we're

play08:32

going to SSH back in and once you're in

play08:35

as long you shouldn't see system restart

play08:37

required that's a good sign and also if

play08:39

it says zero updates can be applied

play08:40

immediately you should be good to go but

play08:42

since I upgraded the kernel it's likely

play08:45

that some packages some more packages

play08:46

can be upgraded as well so we are

play08:48

actually going to do an apt upgrade

play08:50

again now in my case it says there are a

play08:53

couple of packages that have been kept

play08:54

back and it didn't autoinstall them so

play08:56

I'm actually just going to try to

play08:57

install them directly so I'm going to

play08:58

copy their name and then just do an apt

play09:00

install with those names now you may not

play09:04

have gotten that error and that's

play09:05

completely fine if it says you have no

play09:07

upgradeable packages you're good to go

play09:08

but at this point because I did install

play09:10

those if I do an app upgrade now I

play09:12

should see uh there's nothing to be

play09:13

upgraded which means I'm on a fully

play09:15

upgraded

play09:20

system now the next thing I'll do is

play09:22

change the root users password now uh

play09:26

for me in my VPS dashboard they

play09:28

literally gave me a password that I

play09:29

could copy paste to log in as the root

play09:31

user and I don't want to be able to do

play09:33

that so I'm going to change it to

play09:34

something that only I know and something

play09:36

that can't be copy pasted from the

play09:38

dashboard so if you type

play09:40

PWD that will prompt you to change the

play09:43

password for the currently logged in

play09:44

user in this case I'm going to change

play09:45

the password of the root user now you

play09:47

won't see anything when you're typing

play09:48

your password but as long as you type it

play09:49

correct both times it'll change the

play09:51

password now to check it I'll exit the

play09:52

SSH session and relog in with my new

play09:58

password

play10:02

so I've confirmed my updated password

play10:04

works now we can start locking down the

play10:07

server and one of the first Concepts

play10:09

you'll learn in security is the

play10:10

principle of least privilege and if

play10:13

we're running all of our commands as

play10:14

root we always have super user

play10:16

privileges if you actually if you type

play10:18

ID in the terminal and your user ID is

play10:20

zero that means you are the super user

play10:21

you can do anything on this machine and

play10:24

there are some things we need super user

play10:26

privileges for but we don't need it for

play10:28

everything so we're going to create a

play10:30

secondary user and they will not have

play10:32

super user permissions by default but

play10:35

will be able to run things with super

play10:37

user permissions if they require it now

play10:39

to add a new user we can use the add

play10:40

user command so I'm going to type add

play10:42

user and then the name of the user in

play10:44

this case I'm going to call it CJ

play10:45

because that's

play10:46

me so this will create the user and it's

play10:48

going to ask for a password now give it

play10:50

a password but give it a different

play10:51

password than the root user you don't

play10:53

want those passwords to be exactly the

play10:54

same for security purposes so I'm going

play10:56

to give this a password now after you

play10:57

set up the password It'll ask you a few

play10:59

more questions just fill those out and

play11:00

for some of these you can just leave

play11:01

them blank if you don't have any info

play11:03

for them now that we've created the user

play11:04

they're a regular plain old user they

play11:06

can't do anything as the super user yet

play11:09

but what we'll do is We'll add them to a

play11:10

group called pseudo and that will then

play11:13

allow them to perform Super User actions

play11:15

if they need to so to do this we're

play11:16

going to do a user mod we'll do-

play11:19

lowercase A- capital G and then we will

play11:22

specify pseudo and then the username in

play11:24

this case my username is CJ now from

play11:27

here if we want to make sure it worked

play11:28

we can type groups and then the name of

play11:30

the user and we'll see the groups that

play11:32

they're in so by default a user is

play11:33

always in a group with their own name so

play11:34

I'm in the CJ group but you can see that

play11:36

we've added that pseudo group as well so

play11:38

now to try this out let's actually exit

play11:40

our SSH session as root and try logging

play11:43

in as this new user that we created so

play11:45

I'm going to exit we're going to do an

play11:47

SSH but now instead of logging in as

play11:49

root we're going to type the username

play11:51

that we just created we'll type in the

play11:52

password and we're good to go so now we

play11:55

are able to log into the machine but not

play11:57

as the root user and like I said this is

play11:59

is one of the first steps of locking

play12:00

down the machine is making sure that

play12:02

you're not always doing things as the

play12:03

root user now you can see that Ubuntu is

play12:05

actually being very helpful right now

play12:07

and it's saying uh if you want to run

play12:08

something as the administrator or as the

play12:10

super user you can use the pseudo

play12:12

command so for example if I tried

play12:13

running an app update like we did when

play12:15

we were under the root account you'll

play12:17

see this error and basically that means

play12:20

we don't have permissions to do it but

play12:22

if I'd like to perform this action as

play12:23

the super user I can do pseudo apt

play12:26

update now it's going to ask for your

play12:28

password the first time and this is your

play12:30

user password so just typee that in and

play12:32

from there it will run the command with

play12:33

super user privileges now you can use

play12:35

this pseudo command for any command that

play12:37

you want to run as the super user now if

play12:39

you're new to Linux and the the pseudo

play12:42

command um you you are basically

play12:45

unlocking the ability to understand this

play12:47

classic XKCD joke of um if you put

play12:50

pseudo in front of any command you now

play12:51

are executing it as the super user and

play12:53

pseudo used to stand for super user do

play12:56

uh but now the command can also be used

play12:58

to run commands on the machine as other

play12:59

users as well so it actually stands for

play13:01

substitute user do and it allows you to

play13:03

perform commands as other users and by

play13:05

default it'll perform the commands as

play13:07

the root

play13:13

user now the next step in locking down

play13:15

the machine is making it so that we

play13:17

connect with an SSH key instead of a

play13:19

password now to do this you will need an

play13:21

SSH key I'm going to link this article

play13:23

by GitHub in the description it shows

play13:25

you how to generate an SSH key if you

play13:27

don't have one uh but the next steps

play13:29

will require you to have an SSH key now

play13:31

it's important to note that SSH key that

play13:33

you generate should be generated on your

play13:35

local machine the machine that you're

play13:36

going to be connecting to the VPS from

play13:38

not on the VPS itself so make sure when

play13:41

you follow the directions to generate

play13:42

the SSH key it's happening on your local

play13:44

machine now that SSH key locally on your

play13:46

machine lives in your home folder in a

play13:48

folder called SSH and for me this is the

play13:51

public key file and if we run the cat

play13:53

command on it we can see the contents of

play13:55

it now this is completely okay to share

play13:57

because this is my public key

play13:59

I would not want to share the private

play14:01

key which is in the file without the pub

play14:02

on it but the public key completely okay

play14:05

to share and this is what we need to add

play14:07

to the server to be able to log in using

play14:10

this SSH key now let's add this public

play14:12

key to the VPS so I'm going to copy the

play14:15

my public key and then over on the VPS I

play14:18

need to create a folder in my home

play14:20

folder called SSH so right now I'm in my

play14:22

home folder I'm going to make a

play14:24

directory called SSH and then I need to

play14:26

create a file called authorized unor

play14:28

keys now to do this I'm going to use a

play14:30

command line tool called Nano it's very

play14:32

easy to use basically it's a command

play14:33

line text editor and then I'm going to

play14:35

specify the file name so I want to edit

play14:36

the file called SSH SL authorized

play14:41

Keys now once I'm in here this is just a

play14:44

text editor I can I can type whatever I

play14:46

want but I need to put my public key

play14:48

into this file so I'm going to paste it

play14:50

in and then I'm going to exit and save

play14:52

the file so you you can see in Nano it

play14:54

literally tells us to exit we can press

play14:56

crl X and then to save it we'll press

play14:59

press Y and then hit enter so now my

play15:01

public key is in that authorized key

play15:03

file and that's actually all we need if

play15:05

I exit my session here and then try

play15:08

sshing back in you'll notice it doesn't

play15:10

ask for my password and just instantly

play15:12

logs me in and this is because behind

play15:14

the scenes it was doing a public private

play15:16

key negotiation and allowed me to log in

play15:18

with my SSH

play15:24

key now that we've set up our SSH key we

play15:26

are going to disable password login in

play15:29

entirely and this is how we're going to

play15:30

stop attackers from trying a bunch of

play15:31

different passwords is no one will be

play15:33

able to log into our server with a

play15:34

password you have to use an SSH key once

play15:36

we enable this setting now it's

play15:38

important to note if you're connecting

play15:39

to this VPS from other computers you're

play15:41

going to need to make sure that you set

play15:42

up an SSH key on those machines as well

play15:45

and you add the public key to that

play15:47

authorized Keys file that we worked on

play15:49

earlier but at this point let's disable

play15:51

password login now to disable password

play15:53

login we're going to need to edit the

play15:55

sshd config we'll use pseudo for this

play15:57

because we need super user to edit that

play15:59

file we'll use Nano because it's a text

play16:01

file and the file is ATC SSH

play16:05

sdore config now we'll edit this file

play16:08

and because we're using pseudo we'll

play16:10

have to type in our user password now

play16:11

once we're in the file we can hold the

play16:13

down arrow to get to the section on

play16:15

password

play16:16

authentication now I will note that in

play16:19

the previous section I said that login

play16:20

with SSH key would just work if it's not

play16:22

working for you it's possible that this

play16:24

setting was not set to yes so make sure

play16:25

that you set this setting to yes but

play16:27

we're looking for the password

play16:29

authentication setting so yeah right now

play16:31

password authentication is set to yes I

play16:33

want to set this to no and then save the

play16:36

file now on my machine there's actually

play16:39

another config that I need to update and

play16:42

that is in the D folder so if we take a

play16:46

look in that folder there's also sshd

play16:55

config.inc authentication set to yes so

play16:58

I want to make sure that I set this one

play17:00

to no as well so we'll save that update

play17:02

it and now we need to restart the SSH

play17:05

service to get these settings to kick in

play17:06

so I'm going to do pseudo service SSH

play17:09

restart now to test if this worked let's

play17:11

actually try logging in as the root user

play17:13

so I'm going to ssh in as root and if we

play17:17

get this error permission denied public

play17:19

key we know that it's working because we

play17:21

didn't set up a public key for the root

play17:22

user we only set it up for our other

play17:24

user and uh now we actually can't log in

play17:27

with the password uh as the user but if

play17:29

we try sshing in as the user we created

play17:32

the public private key exchange should

play17:34

happen and we should get logged into the

play17:41

machine now we've locked things down so

play17:43

we can only log in Via an SSH key but to

play17:45

lock things down even further we're

play17:47

going to prevent login via the root user

play17:49

over SSH entirely so to do this we'll

play17:51

jump into that same config file we're

play17:54

going to go down to the section that

play17:55

says permit rout login and we're going

play17:59

to remove the hash will actually which

play18:00

will actually enable this configuration

play18:03

by default without password just means

play18:04

that it would only allow the public

play18:06

private key authentication for the root

play18:08

user but we're actually just going to

play18:09

set this to no and that way the root

play18:11

user cannot log in bya SSH at all so

play18:14

we'll save this file and then we'll

play18:16

restart the SSH service so we'll do

play18:18

pseudo service SSH restart and from here

play18:22

root can no longer log in bya

play18:27

SSH

play18:30

now the next step in locking down your

play18:31

VPS is to control the network and

play18:33

firewall policy now for me my VPS

play18:36

provider has a dashboard where I can

play18:38

open and close various ports on the

play18:40

firewall you might have that as well

play18:42

you're going to want to look for a

play18:43

section called ports or firewall or

play18:45

network uh but if you don't have that

play18:47

section in your VPS provider dashboard

play18:49

you can actually use an application

play18:51

firewall so built into Ubuntu is an

play18:54

application called ufw or uncomplicated

play18:57

firewall and it allows you to control

play18:59

the firewall from the command line

play19:01

directly on the VPS itself but uh since

play19:03

my provider has a dashboard where I can

play19:05

control all of the ports and everything

play19:07

I'm going to be using that for all of

play19:09

these this network stuff that I talk

play19:15

about now the first thing we'll do is

play19:17

close all ports that don't need to be

play19:18

open now if you're new to all of this

play19:20

stuff ports are like little doorways on

play19:22

your computer that can receive and

play19:24

respond to network requests and so for

play19:26

example we've been connecting to our VPS

play19:28

via SSA and by default the SSH service

play19:30

is running on Port 22 to respond to

play19:33

those SSH requests now um if if you're

play19:37

not running anything else on your

play19:38

machine that needs to be exposed to the

play19:39

internet like you don't have a web

play19:41

server or you don't have a database or

play19:42

anything else you can just close down

play19:44

all other ports so either use ufw to do

play19:47

this from the command line or go into

play19:49

your VPS provider dashboard and any

play19:51

ports that it has open like a lot of

play19:53

times Port 80 and 443 will be open by

play19:55

default because the provider assumes

play19:57

that you're going to be running an H

play19:58

HTTP or https service and that's those

play20:01

Services respond on those specific ports

play20:03

but if you're not going to have a web

play20:05

server there you can close those ports

play20:07

so just remove those rules that that

play20:08

open up those ports or if you're using

play20:10

ufw uh use it to either open or close

play20:14

those

play20:19

ports now one of the common things

play20:21

people do to lock down a machine further

play20:23

is change the port that SSH is running

play20:25

on now we mentioned it runs on Port 22

play20:27

but you could run it on 2222 or 4242 you

play20:30

could pick a port number um and to do

play20:33

that you would need to go into your

play20:34

firewall whether it's in a dashboard or

play20:36

it's from the command line and open up

play20:38

the port that you run want to run SSH on

play20:40

and then you can go into that

play20:41

configuration file that we were in

play20:42

earlier and there's actually a section

play20:43

that will let you set the port that SSH

play20:46

runs on now if you do this this is one

play20:48

way of preventing automated attacks

play20:51

against your server because a lot of

play20:52

scripts that hackers are running will

play20:54

assume that SSH is running on Port 22 if

play20:57

Port 22 is open so if you close 22 and

play21:00

run SSH on a different port that's going

play21:02

to prevent those automated attacks now

play21:04

there are ways of poking and pting at a

play21:06

server to figure out what ports are open

play21:08

and if any ports are open attempting to

play21:10

figure out what services are running on

play21:11

those ports so a Savvy hacker will still

play21:14

be able to find your SSH Port even if

play21:16

you change it from the default 22 now

play21:19

personally I like the convenience of not

play21:21

having to type in the port number when I

play21:22

SSH so I just leave SSH running on Port

play21:25

22 but again that's this is one way of

play21:27

locking down the Sur if you want to

play21:29

prevent those automated login

play21:35

attempts now you can further lock down

play21:37

these ports by restricting access to

play21:39

specific IP addresses Now by default if

play21:41

you're setting up a rule and it has the

play21:43

ipv4 address

play21:44

0.0.0.0 that means allow and respond to

play21:48

requests from any IP address but let's

play21:51

say for instance you only want to allow

play21:53

access to Port 22 from your computer

play21:56

that you're running right now if you

play21:58

have a static IP address that means an

play22:00

IP address that doesn't change you can

play22:01

set up that firewall rule so that it

play22:03

only allows connections from your IP

play22:05

address now I don't have a static IP

play22:07

address I'm just running home internet

play22:09

um and so day-to-day my IP address might

play22:11

change but if you're at a business or

play22:13

you're paying your internet service

play22:14

provider for a static IP address then

play22:16

this is a kind of rule that you can set

play22:18

up where basically in the rule instead

play22:19

of 0.0.0.0 you put your IP address and

play22:23

now your server will only allow

play22:25

connections on that specific Port from

play22:27

the IP address that you sp

play22:33

specify now that things are locked down

play22:36

we're going to want to make sure that

play22:37

our system stays up to date and so for

play22:39

this we're going to install a program

play22:41

called

play22:42

unattended-upgrades so from the command

play22:44

line if you do pseudo app install

play22:47

unattended-upgrades this will install it

play22:50

now my system came preconfigured with it

play22:52

but if yours didn't you're going to need

play22:53

to install it from here we're going to

play22:55

enable automatic updates so you do

play22:57

pseudo D package - reconfigure

play23:00

unattended-upgrades this will run a

play23:03

little uh configuration wizard you just

play23:05

want to say yes on enabling automatic

play23:08

upgrades now from here you're good to go

play23:10

but you might want to customize it some

play23:12

more so I'll link to the documentation

play23:14

in the description but you'll notice

play23:17

that they talk about a specific config

play23:19

file that you can modify so if we take a

play23:21

look at this there are some settings

play23:23

inside of there like the kind of updates

play23:25

that should be applied and also further

play23:27

settings like should the machine be

play23:29

automatically rebooted and stuff like

play23:31

that so if you say pseudo Nano and then

play23:33

the location of that file you can edit

play23:36

it now by default only security updates

play23:39

are enabled so these two lines here and

play23:41

these two lines here are for security

play23:43

updates but if you want to automatically

play23:45

update other types of packages as well

play23:47

you can uncomment these lines so if you

play23:49

remove these two slashes that actually

play23:52

makes it so that this line will be

play23:54

matched whenever it's running the

play23:55

automatic upgrades as well so I'm going

play23:56

to do this for updates because this will

play23:57

just update regular packages as well now

play24:00

like I said there are other options in

play24:02

here I'll link the git repo that talks

play24:04

about the other kinds of options but

play24:06

there are options in here also for

play24:08

things like automatically rebooting uh

play24:11

picking what time the server should

play24:13

should reboot uh you can also set up

play24:15

mail so that it sends you an email if

play24:17

there are updates that need to happen

play24:19

these kinds of things now from here we

play24:21

want to make sure that the service is

play24:23

running so if I do a pseudo system CTL

play24:29

status for unattended upgrades as long

play24:32

as you see this output with a little

play24:33

Green Dot there then that means the

play24:34

service is running in the background and

play24:36

it will be automatically installing

play24:37

updates now at this point you can do

play24:39

whatever you want on your VPS you can

play24:41

open ports you can install Services it's

play24:44

yours to do what you would like uh

play24:46

personally I'm a web developer so in the

play24:48

next video in this series I'm going to

play24:50

set up this machine to be a web server

play24:53

I'm going to set up some static sites

play24:54

I'll have some nodejs apis running I'll

play24:57

have a couple of databases running

play24:58

inside of containers I'll have reverse

play25:01

proxies set up with subdomains and also

play25:03

set up some SSL certificates so if

play25:04

you're interested in that kind of thing

play25:06

let us know in the comments but also uh

play25:08

stick around for the next video in this

play25:12

series that's all I have for you for now

play25:14

if you enjoyed this let us know in the

play25:16

comments if there are some pieces that I

play25:18

missed let us know in the comments as

play25:20

well um I absolutely want this to be a

play25:22

valid and secure resource for people

play25:25

that are setting up a VPS so if I said

play25:27

anything wrong or you think things could

play25:29

be done better let me know in the

play25:30

comments I'll add them as corrections to

play25:32

this video so that's all I got for now

play25:34

I'll see you in the next

play25:44

[Music]

play25:54

one

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
VPS SecuritySSH KeysFirewall ConfigAutomated UpdatesLinux ServerSystem UpgradesSecurity Best PracticesRoot UserPseudo CommandUncomplicated FirewallSSH Port