Syntax Video
Self Host 101 - Office Hours - Losing SSH Access, VPNs, Load Testing, Reverse Proxies and more!
- 0:00 Intro
- 0:26 Places to get a cheap VPS
- 0:52 How do we find the public IP of a VPS?
- 2:24 Add more public keys to a server
- 3:41 What happens if we lose our private key?
- 5:03 Why use a .pem file vs ssh key?
- 7:52 Can we use a VPN to connect to a VPS?
- 10:01 How much load can a small VPS handle?
- 11:24 How to run a node app in production?
- 13:18 Use scp to copy files to / from a server
- 14:25 Add 2-factor auth to SSH login
- 16:05 Automatically block requests with fail2ban
- 17:45 Use ssh config to set port / user
- 19:36 Setting the mode of the authorized_keys file
- 22:12 HTTP Servers like Caddy
- 23:12 Reverse Proxies Explained
- 26:31 Protecting Sites with Cloudflare
- 27:38 Please Comment / Question
- 28:02 Self-host Sentry
- 28:40 Thanks!
Intro
what's up CJ here in this video I'm going to be holding office hours so last week I posted a video called self-host 101 that showed you how to set up and secure a VPS or virtual private server and in this video I'm going to be talking about some of the comments questions suggestions Corrections uh that appeared on that video and all of these comments questions suggestions Corrections are coming from uh These Fine people um so let's get into it before that uh be sure to check out the post that I made over on X if you're looking for a VPS so uh there are a few different places I listed where you can get a really cheap virtual private server if you want to start learning about this kind of thing but definitely check the comments as well because there's lots of good suggestions of other places to find cheap VPS or for instance even places where you might be able to get one for free uh but definitely check out that post if you're looking to get your own VPS now our first question comes from bad info 259 and their question is how to get public
Places to get a cheap VPS
How do we find the public IP of a VPS?
IP so when you're connecting to your VPS to configure it you're going to need to connect over the public IP address and this is going to vary depending on who your provider is I have a few examples here like ionos has a dashboard and it shows your public IP there under like network if you're using AWS lightell you're going to see the IP address on the instance and also you'll see a static IP if you've assigned it uh if you if you're using hetner there is a public IP section if you're using Oracle there's a section called instant access so it's going to vary depending on provider but one thing you want to look out for is make sure that it is a public IP address that you're trying to connect to so if you're not familiar with private public versus private networks I did a video over on coding Garden where I kind of break down the difference between them um but you can make a quick note of this if you see an address that starts with 10 that's a private IP address if you see an address that starts with 17216 all the way up to 17231 those are also private and if you see an IP address that starts with 192.168 that is also a private IP
address so if you're trying to connect to these the only way you're going to get connected to them is if you're also on the private Network that those those uh these the IP address that you're trying to connect to is on um so make sure it's a public IP and then for any given provider it's going to vary so I I would suggest just searching the web if you can't figure it out connect to VPS and then whoever you got your VPS from and they should have some documentation on how to find that public IP and how to connect great question though thank you for that uh next we have two questions so the first one is from optimizer pro and it says if I don't have my machine where I log in with my secondary user how can I log into the server if the route is disabled as well and then we have a question from Scott Walter who says what happens if you disable password login and lose your public key so both of these have to do with potentially losing access to the server because in the setup video I talked about how to disable password off over SSH and only allow public private key authentication but what if you lose that public or sorry what if you lose that private key how do you get in and first
Add more public keys to a server
we'll address uh connecting from a different machine because you can actually set up multiple uh public keys on the server so let's connect to my server and I will show you in the SSH folder so if you CD into. SSH you should have that authorized Keys file where you put your public key and then if you edit it you can actually add multiple public keys inside of here so each public key will just be on a new line and so you can actually go set up your SSH keys on some other machine and then add the public key here as well and and also if you want to share access to the server with someone else you can put their public key into this file um so that's one way to connect from multiple multiple machines just make sure you have all of the public Keys listed in this authorized Keys file now um what happens if you completely lose access to the server we'll talk about that because that's what Scott Scott is saying it's like what if I don't have my my my private key anymore um and we can't log in as root user in this case you're going to need to connect to the server
What happens if we lose our private key?
in what is known as like console mode and so again this is going to vary depending on who your VPS provider is but I have some examples so if you're using ionos they have this drop down here that says open remote console and this is not actually an SSH connection this is uh using the hypervisor or the thing that's actually hosting the VPS to be able to connect directly to the machine and you will be able to log in with the root user if you still remember the root password using that console mode um and so this again varies based on provider but like on hetner it's called rescue mode so if you select rescue in the menu that's going to put you into a console and you can use your your root password to log in um but basically that's what you're going to need and if your provider doesn't have a console mode then you're kind of out of luck in that case you would potentially just have to reset the VPS and uh set a new uh root password and and be able to reog in uh but eventually in this series I'm also going to talk about the idea of backups so that way if something catastrophic like this happens and you can't get in anymore if you do have to
reload the VPS you at least have a backup that you can work from so we'll talk about and that in a future video but thank you to both optimizer pro and Scott for these questions all right your next question comes from Phil Sola and it is this is a great video for walking through VPS basic setup and security measures really enjoyed it looking forward to the next one thank you Phil uh one question for my own understanding we do something similar already with SSH keys at my day job but we use Pim keys to log in is there any difference or benefits to doing that in the way you're doing it without uh there isn't and uh a Pim file is actually a privacy enhanced mail file um that has a specific format so it has begin private key you'll actually have your private key inside of there and then end private key and you may not know this but the SSH key that you generate to connect to a remote instance actually is a pin file it just doesn't have the extension so if I look here on my local machine you can see that I have this uh private key right here but inside of that private key it actually is just a pin it just doesn't have the
Why use a .pem file vs ssh key?
pin extension and uh if we want to see an example of that I going to show the contents but I'm not going to show my private key I'm just going to grip for uh the string that should be in there which is all caps private key so if we say search that for private key you'll see that inside of that it actually is just a pen file and it doesn't have that pen extension but what's happening uh on your machine when you attempt to connect to a remote server um it's going to attempt to use your private key to to log in so that's why we don't have to specify it here we have it there in the SSH folder and it's already been added to the s H agent um and so it just happens by default but when you're using a pin file you're just specifying the private key to use so for example we can actually connect by manually specifying that that file there so we can do sshi and then specify our pin file and then the server that we want to connect to so and now we are explicitly saying hey use this this this key file to connect to the server and so
that's that's basically what's happening when you specify that that pin file and this is actually a really common way to like share access to a server might have a way at your company of like checking out these pem files and then using them to connect um but one cool thing you can do is you can actually add that that pin file to the SSH agent so you don't have to specify it manually every time so you can actually do SSH agent uh add and then specify the pem file that you want to add sorry I think it's SSH sorry it's just SSH dasad and then the pin file that adds it to the SSH agent and now you should be able to connect to that server without doing- I because the private key has actually been registered so now that you've registered that private key you should be able to connect and you don't have to specify it manually so it's a great question and it is interesting to to see like the different ways of connecting to the server but ultimately you are just using a private key but you're using it manually and the only difference is that key hasn't been added to the SSH agent so thank you Phil for your question uh
Can we use a VPN to connect to a VPS?
next question comes from Aztec and their question is how can you use something like zero tier or tail scale to allow SSH from those VPN Services first let's talk about what those are because they're actually new to me so uh zero tier is a a way to create manage uh create and manage virtual softwar defined networks uh and then tail scale is uh a meshed virtual private network but what I am familiar with are vpns or virtual private networks so these are just basically like it seems like protocols like on top of a vpns um but what I am familiar with is like openvpn so openvpn is a VN server that you can set up and um this is another way to potentially lock down your server you could use something like openvpn or even wire guard this is another very common one and I'm I'm sure that both tail scale and and zero T would work in a similar way where if you have your VPS connected to some VPN server now your VPS will have a private
IP within that VPN and so we talked about private Network addresses earlier and again check out that video over on coding Garden if you want to kind of see me draw a diagram talking about the difference is but once your VPS is connected to some VPN regardless of where it is or what kind it is your VPS will then have a private IP address so if you two connect to that VPN now you have a private IP address as well and then you'll be able to uh SSH into the machine and and also you would you would essentially set up some security rules for your your firewall as well and you can basically say only allow SSH attempts if they are coming from a private IP address and so that way you don't have to open port 22 to the entire world or whatever Port you're running SSH on you can only open it up to a specific subnet range within a private IP block and that way you're locking it down so that SSH can only happen from a private IP address and essentially you would need to be connected to some VPN that your VPS is on to even get access to it so so this is one way to further
lock down the machine and it is um it'll work regardless of what kind of VPN technology use you use as long as your VPS is on a VPN and you're on a VPN then you'd be able to connect after you set that security rule so thank you Aztec for that question next up we have a question from uh ra laser da 12 who says nice video uh this video will be a great checklist for when I'm spinning up a new VPS my question is how much can you pack inside a 1 CPU 1 gigabyte RAM VPS will it be able to run a simple sqlite database with n uh and any tips on how to make the most of it now first thank you for your question but this is going to vary widely depending on what you're actually trying to do with your VPS and one of the things things you can do is actually known as load testing so I I'll link this week AIA article here that talks about software load testing specifically you want to do Network load testing and there's a couple of tools that are pretty popular like I've heard of J meter I've heard of Gatling I've heard of locust uh these are all tools where you can simulate Network traffic
How much load can a small VPS handle?
and see how your server responds so if like because this is going to depend on your type of app and how fast it responds and everything else this is something that you might want to do like put your app on the server start running it and then create some sort of load test to see how your server responds to it because the all of these tools will show you like how long it takes to respond how many requests it can handle that kind of thing and so that's really the only way you're going to know if the server can actually handle it now on the surface I think it can handle it sqlite database fine uh maybe a few hundred users accessing your app totally fine but again it's going to depend on how much load your server is actually getting and how many users you have um but specifically for tips on how to make the most of it I will say that if you are running some node type app on a server you're always going to want to make sure you're running in production mode so n specifically if you just search their their site for production they have a section on making sure that n is production ready and you're going to want to make sure that you follow that and specifically you're going to want to make sure that you build the app
How to run a node app in production?
you would be surprised at the number of uh people I have found who are just doing like npm run Dev on a production server instead of building the app and then running the built files and so that's definitely one thing you want to do uh because when you're running in Dev the code isn't as optimized uh but if you uh build it you're going to get code that can just be run directly with node it won't need need like TS node or or anything like that so definitely build your code and then the other thing is you're going to want some kind of process manager because if you just run this directly if your server ever crashes and you don't have something watching that then your server just crashes for everyone but if you're running your app behind some sort of process manager then it can automatically restart the process if it exits so uh the Nu dos actually how show you how to do this with pm2 and pm2 I am I'm familiar with um and you can actually create this ecosystem file commit it to your source code and then when you're running the app on the server after you've built it uh you can just do a pm2 start and it'll look at
this file and determine how to start it but it keeps track of all of the logs you can set a log location um it automatically will restart if it crashes so you definitely want to be running something like this there there are other tools for this as well you can just use like systemd which is built into Linux to create your own service that automatically restarts on Crash but these are some of the things that you would want to do definitely in a future episode I'm going to talk about more stuff you can do to uh make sure your your app is running well in production but that's at least a start so thank you for your question uh next up let's get into comments and Corrections so uh this first comment comes from uh Chris Clen who says can't you use SCP to copy your public key to your VPS let's touch on that really quick because uh yes you can so when you're you're working with a remote server typically you're going to want to figure out some way to get files onto that remote server and SCP or secure copy protocol is one way of doing that so this is actually a command you can run that transfers files either from your local machine to some remote server
Use scp to copy files to / from a server
over SSH or from some remote server to your local machine so you can transfer files in both directions and it works over SSH so you don't have to open up any other ports you can just use the same SSH Port that you're using to connect to the server to actually transfer files and so SC is one way to do it you can check out this Wikipedia page where it talks about it I'll also link to the tldr page that shows you some some common ways of running the command but yes if you want to get files from your machine to a remote machine or from the remote machine to your machine SCP is one way to do it there are other ways so things like SFTP or even like putting something in a g git repo and then cloning it to the server uh there's other ways which we'll touch on in F future videos but sdp is definitely one um all right so the next thing Chris says is there's also a way to add two Factor Authentication using top codes as a second form of authentication to the terminal uh in gdar also mentions at work we use SSH keys on a Ubbi key and do not store them on our computer so uh this is a further way to lock down
Add 2-factor auth to SSH login
logging into a VPS over SSH uh and I've never done this before but it seems pretty easy to set up so specifically on Ubuntu there is this package called lib Pam Google Authenticator and you can set this up so that after connecting with your private key over SSH
onetime passcode as well so those are really great suggestions and and one additional form of security that you can set up if you want to lock down the server further now gdar also mentions uh a thing called fail to ban and so I didn't talk about that in the previous video but we are definitely going to talk about it in future videos and this is a really nice tool for automatically blocking repeated attempts to either like login or request files from your server so this is an open source package you can set it up on a server and one thing you can do is you can set up a rule that says if you see multi failed login attempts from a specific IP address block that IP address uh at the firewall level um and one thing that we'll do in a future video is we'll set up fail to ban to block uh repeated failed requests so sometimes attackers try to find files on your server by just brute forcing and requesting a bunch of different files file names like trying to find like slash admin or something like that and you can use fail to ban to look at your HTTP request logs and if it sees a bunch of 404s or it sees specific
Automatically block requests with fail2ban
kinds of request you can actually set up fail to ban to ban that IP address um from from making requests uh to the server now um what's cool about fail to ban is you can also uh set it up in a way where it it doesn't ban the IP address forever because you could do something similar to how a website might lock you out from logging in for like 10 minutes so let's say on a website you type your password Wrong five times and then it's like hey you're locked out try again in an hour or whatever else fail to ban also supports that so instead of just completely blocking someone it you can set it up so that if there are a few failed attempts block it temporarily and then unblock it but then if you see more and more failed attempts you can block it permanently so fail to ban is is pretty versatile in what you can do with it and definitely you should check it out but we are going to talk about it in a future video so thank you to Chris clim and gdar both for your comments uh these next comments come from nutty geek and cod mister and they talk about configuring SSH to use a non- default port and so in my video last week I talked about how I like to just leave
Use ssh config to set port / user
SSH running on Port 22 because it's convenience to not have to specify the port but they mention you can actually use uh the config file to specify the non default port and so let me show you what that looks like here so on your local machine in your SSH folder there's that config file and if you take a look at it you can actually add a section for a server so you can see uh by default I have this this section in here for connecting to GitHub and that's how it uses my my private key but I could add another section with my IP address of the server that I'm connecting to and then there are several options you can specify here and one of them is port and so you can specify that some non-standard port and now whenever you're connecting to this specific IP address over SSH the SSH agent will look at this file and use the port that you specified here so you don't have to specify it manually so that's a really good tip and then the other thing that uh Cod Mister mentioned is that um you can specify a user there too so this is this actually we we'll we demonstrate
this really quick and so you can say user and then then specify the user you want to connect to so I I'm just going to do 22 because I'm not going to change the port that SSH is running on on my remote machine but let me grab this IP address and what we can do is after we've set this up now whenever I need to SSH I don't have to specify my username so if I save this config now I can just do an SSH specify the IP address it's going to look in that config use the port use the username anything else you configured inside of there and and automatically connect so yeah these are really good tips from from nutty geek and code mister and you can also check out the SSH config man page that shows you all of the other things that you can put in that config file as well cool uh this next comment also comes from code mister and uh they made a really good point so they said uh I'm surprised this work you must have a very restrictive U mask by default and so they're talking about whenever I created that authorized Keys file on the server and so they give a suggestion for the type of uh mode that the file should be in whenever you create it uh but let me show you what
Setting the mode of the authorized_keys file
they're talking about so I'm here on my remote VPS and if I CD into that SSH folder we can do an ls- l and take a look at that authorized Keys file now you may not have known this but this section that you see right here for any file when you're doing an LSL this is the mode of the file and there are a few different sections there's this section which says what are the access rights of the owner and in this case I can read and write the file and then you have the next three which are what are the access of the group in this case the files group is the same as the owner it's a group with my name in it and then uh the last three are uh what can everyone else on the machine do um and so you might have seen this chamod command and uh this changes the mode of uh any given file and so if I do 777 this is going to open it up to the world so let's just do this really quick and then take a look at the mode now if you look at it now everyone can read write and execute so this isn't good to have because if there are some other users on this machine
they could potentially get access to this file and see and and modify it and and like a malicious user could add their own public key in here and then log in as you remotely so you definitely want to make sure that this has the right permissions and the permissions the most restrictive permissions you can use are 600 so if you do six that means read and write for the owner and then everyone else uh can't read or write that file so if I do 600 now you can see it has only rewrite privileges so you want to make sure that that it has that permission and if you're getting some kind of error and you're not able to log in with your SSH key it's probably because this file doesn't have the right permission so thank you very much code Mister for for mentioning that and I will direct you to also this this chamod calculator you can find a lot of different these a lot of different versions of this online but essentially you can change uh read write and execute for uh owner group in public and it'll show you what the corresponding value would be so 777 is rewrite execute for all users uh but if
we go to the same permissions that I just set and it is uh read and write only then it's 600 so you can figure out what you what your mode wants to be and then specify that from the command line but thank you very much code Mister that's a it's a great tip all right uh next up are suggestions for next time um and there are quite a few and this first one comes from uh Wess Salam and they said excited for this series we'd be interested to see Server configuration with caddy that's coming up next uh and Alex D says uh this is my first time I like the YouTube suggestion very clear tutorial and I like your energy thanks CJ well thank you Alex thank you for commenting uh and they said can you please add to this tutorial how we can run Docker apps in a VPS and use a different domain name for each app running engine X as a reverse proxy I am going to show how to do this in the next video in this series um and this is related because you can actually set up reverse proxies uh with a thing called caddy so um caddy and engine X are both types of HTTP servers and there just like a lot of other things there's
HTTP Servers like Caddy
a lot of options so you could use something like caddy server you could use engine X I have the most experience with engine x u i used it for years but more recently I switched over to caddy because it's so easy to configure but you can use something like engine X there's also uh Apache HTTP server there's also a thing called traffi but all of these tools allow you to set up what is called a reverse proxy so I'm going to do my best to just kind of like diagram really quick what a what's happening with the with the reverse proxy and regardless of if you have apps running inside of Docker really if you have multiple apps running on the server um some of them might be running in Docker but those ports are exposed and you might want to to set up a reverse proxy so let me actually just show you a quick little drawing of what that means so let's say you have here on your VPS you have an app running and it is running on Port 3000 now let's say you have your firewall set up and you only open port 80 and you only open port
Reverse Proxies Explained
443 uh now when a user comes to your site let's say like over https and you let's say you want to serve up the app that's running on Port 3,000 this is what a reverse proxy can do and beyond that you can also set this up with DNS so let's say we set up a DNS record for uh app. example.com and then we have an a record set that points to the IP address the public IP I'm putting a private IP but let's say you have an a record that points that domain to the public IP address of your server now when a request comes into your to this specific domain it'll come to this server but then you need some HTTP server running that can redirect that request and this is what something like caddy or engine X or Apache HTTP will let you do is you can set up a config that says when a request comes into the server for this specific domain then route that request to the application running on Port 3000 and then send the
traffic in that way and what's nice is you can actually set up multiple of these so like if you had another app running let's call this app two and it's running on Port 5000 you could have another domain let's say like
file instead of needing an app running on a port so as a quick and dirty explanation um I think you can figure it out though you don't need to wait for my video um I think the only difference between me and you is that like I've failed a hundred times at trying to set this kind of stuff up um but I believe in you you can fail too so try try your best look through the docs I I have confidence that you can set this up but essentially uh if you like if you look in any of the documentation for any of the htb servers that I mentioned they're going to have something on setting up a reverse proxy and they're going to talk about specific examples on on how to do that so thanks for your suggestions this will be in the next video um so so stay tuned for that um okay and then lastly uh Bel Chic is asking are you going to Clover Cloud flare I am so uh I actually have a domain pointed at my server and I'm using cloudflare to do that now cloudflare is is a a DNS provider they have a lot of other services too but regardless of where you buy your domain you can actually change the name servers to cloudflare and then do all of your DNS settings inside of cloud flare and so that's what I'm actually doing for this
Protecting Sites with Cloudflare
example server that I'm setting up in the series and uh they have a really generous free tier and one of the cool things that you you can get for free as well is the distributed denial of service protection so in the dashboard after after You' set up these domains they actually have a checkbox you can check and let's say your your server is getting a bunch of requests all of a sudden you can check that box and now cloudflare will will put a capture for people that are trying to get to your website and you've probably experienced this before like you're trying to get to some website and they're like prove that you're human before I'll let you in and that's the the kind of service that cloud flare can uh provide as well they they they do other things but that's just one thing that that they do which is pretty nice because you literally check a box and then then it adds that capture all right uh that is all the the the questions and comments that I'm covering in this video thank you for watching and if you have more questions let us know in the comments um part of making this series is just trying to help people right and so I I really do enjoy any any good comments bad comments Corrections like we want all of this stuff to be technically correct so so
Please Comment / Question
yeah anything you have to say we'd love to hear it just let us know down in the comments and especially if you have more questions about the stuff that I talked about here today I will also mention this this video is brought to you by centry so centry is an app and error monitoring platform they will let you know about errors that are happening in production for your app and you can see a nice dashboard of of the errors that have occurred and when they've occurred and how often they've occurred and stuff like that it's pretty slick uh if you want to get two months free use Co code tasty treats you can also go to sentry.io syntax or relevant to this series you can also host it yourself so if you didn't know centy is actually open source and uh you can run it on your own server if you want to so I I'll link to this in the description as well so if you're interested in uh trying to run Sentry on your own server you can and you don't even have to pay for it which is which is pretty slick but all right that's all I have in this video thank you for watching and I will see you next week when we continue the series so see you the next [Music]
Self-host Sentry
Thanks!
Quote it with credit and a link. Quoting Syntax