How to Create a Network of Machines in VirtualBox with SSH Access

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody so there are a lot of system administration videos I'd like to do in the near future on topics like ansible and setting up web servers and running a Jenkins server and things like that and in order to do that I'd like to have some virtual machines set up that we can have SSH access to so in a previous video we already saw how to create snapshots of certain machine States and in this video we'll be learning how to clone virtual machines add these machines to a network so that they can communicate with each other and make sure that they have internet access and also set up SSH so that we're able to SSH into these machines from our host machine so this will allow us to pretty much build an entire virtual lab that we can use to test all kinds of different stuff so after we're done this will give us the ability to quickly spin up a new virtual machine that behaves just like a real machine on our network so if you do want to follow along with those future videos and don't currently have a machine that you can run experiments on then you should probably follow along with this video so that you have a virtual machine that you can SSH into and we'll be using VirtualBox for this video and in a few previous videos we looked at how to install a couple of different operating systems in VirtualBox so we learned how to install Ubuntu desktop and Kali Linux so if you'd like to see how I got these machines installed then you can go back and watch either of those videos and I'll put a link to that in the description section below so in this video we're going to go ahead and create two clones of this Boonton machine and get them set up to where we can SSH into them from our host machine so let's go ahead and get started so first of all I'd like to point out that the virtual machines that we've created so far are pretty fresh installations so the only thing that we've done is installed the operating system and some guest additions now I also went in and added some code to the bash RC files and made the text a little larger so that you could see but other than that it should be just about the same so those changes aren't going to affect if you're following along but if you notice that my prompt looks different than yours when you log into these machines then that's the reason and if you do get those machines set up how you like I usually like to create a snapshot of that machine so that you can go back to the fresh install or anyplace else if anything gets changed by mistake and like I said if you would like to learn more about how to do snapshots then I do have a video on specifically just that and I'll put a link to that video in the description section also personally one thing that I also like to do is after I get all my operating systems set up then I like to rename these to base at the beginning here so that I know that these are machines that I want to clone from so now that I have these set up to where I want to clone from these machines I'm not going to change anything on these machines anymore and then I'll just work with the clones so let's go ahead and do that now and get started on creating our machines that we're going to be able to SSH into so to start let's check out our VirtualBox preferences so we want to go up here to VirtualBox and hit preferences and from here let's go to Network and now let's choose host only network and we want to be sure that we have a host only network in here so you can see that I have one you might have to but you want to be sure that you at least have one and if you need to create one then you can click this Add button over here so now I'm going to double click on this VBox net zero and look at its settings so you can see that we have an ipv4 address of 192 168 56.1 and we're going to need to remember that IP for later and our subnet mask is 255 255 255 0 and if we click on the DHCP server settings here then I'm just going to leave this unchecked because we're going to be assigning static IPS to our machines and once we're sure that we have that host only network and we need to change the network settings for our base machine so let's go ahead and do that now so I'll close out of this and then for our base machine I'm going to open up select the base machine and then open up its settings now let's go over here to network and within Network we're going to go to adapter 1 which should be the default and we're going to have this set to NAT n 80 and this will allow our guest to connect to the internet through our host connection and now let's go over here to adapter 2 and let's enable this network adapter also and for this adapter we're going to choose this drop down here and let's choose host-only adapter now just a quick side note if you want to view these different network options on the VirtualBox page they have a good rundown of what each of these options mean and I'll leave a link to that in the description section below I have it pulled up right here it's just this website here and I'll leave a link to this but they have what all of these different Network possibilities are so I'm going to go ahead and minimize this again be sure that we have host-only adapter selected there and once we select host only you also want to make sure that it's set to the specific network that we just made sure existed okay so now that we have our different based Linux system set up how we'd like let's look at how fast we can get up and running with creating multiple clones that we can SSH into so now that clone that machine I'm just going to right click on our machine and hit clone you can see we could all you also use command o and within here I'm going to rename this to this first clone here I'm just going to name a boon 2-1 and we do want to reinitialize the MAC address of the network cards so let's go ahead and continue and we'll do a full clone for this one and we will do the current machine State and once that clone is finished I'm going to go ahead and create a second clone here and I'm going to call this one a boon - - and we'll reinitialize the MAC address and continue and full clone and current machine State is fine okay so once both of those are created let's go ahead and start up both of those machines and login to each one so I'll just grab a boon - - and put it on the right side of the screen and boom - one I will put on the left side of my screen now since we clone these from a machine that already had guest additions installed we already have destined ditions installed for these clones as well so that's the benefit of cloning machine that's already been at setup so now I'll log in to both of these Shane machines and then as soon as they log in here I'm going to open up terminal so open up terminal on boom - one here and also on a boon - - so first let's make sure that we can access the Internet on both of our machines and to do that I'm just going to go ahead and ping google.com and I'll do that on the second machine here as well so you can see that we are pinging google.com so it does have network access and the reason that has that access is because we're using that n80 as our first adapter and that gives us internet access through our host machine so now let's set up the host names of both of our machines so I have my prompt set up to display the host name here in the prompt and you can see that currently these are both the same because it has the same host name as our base machines since that's what we clone from so to do this to change these host names we're going to have to edit two different files and you can use them to edit these files but if you're unfamiliar with them then you may want to use an editor like Nano so let's edit this file so we're going to do sudo nano and the first file that we want to edit is under e.t.c slash etc' slash host name and we'll open that up and put in our credentials here and for the first machine we will just call this a boon 2-1 and we'll also do this over here on the second machine so we'll do sudo nano slash ET c / hosting and put in our credentials and we will call this one a boon - - - and in nano to save this you hit ctrl X and then Y to save and over here we'll hit control X and then Y to save and now another file to change that host name we need to open up our host file so /et see slash hosts and right here on the first line where we had the old host name I'm just going to erase that and this one was called a boon 2-1 so we will save that and then over here on this machine will do the same thing sudo nano that's - ET c / host and on the second line here we're going to erase the old host name and the new one for this machine was a boon - - so ctrl XY to save okay so now that we have that done let's run a command I F config and dash a and we'll do that on this machine as well ifconfig - a it looks like I missed a space there so I have config space - eh okay so now we should be able to see our network interfaces now this first one here with the eye net address is bound to our first adapter and we're going to bind this second one here to an IP address on our host only network that we specified before so keep the name of this second interface in mind here because it may be different than what is on my machine so for you this may be something like eth1 or something like that so be sure that you keep in mind the second adapter name here so mine is enp 0 s 8 so I'm going to remember that and while remembering that now I'm going to change one more file here to change our network interfaces so this file is going to be sudo nano and the file is slash etc' slash network slash interfaces and we'll open this up in our second machine here as well so that's /et c slash network slash interfaces and within this file I'm going to come down here to the bottom I'll do it on my first machine first and this is going to be a kind of along here but just bear with me so we're going to say Auto and then the name of that interface and mine was EMP 0 s 8 then on the next line we're going to do I face and then the name of that interface again now we're going to tell this that we want it to be a static IP address so it's AI net static and now we want to specify what this address is so we'll say address and I'm going to set the address for this machine to 192.168 got five six dot 100 and now this 192.168 six remember that was the first part of our host-only adapter address okay and what that said let's also do our subnet mask so we'll do net mask and we're going to set this this to 255 255 dot 255 dot 0 okay and now we're going to have to do the same thing on our second machine here so I'll go ahead and speed this up so that you don't have to watch me type all this again okay so once we get to this point on our second machine now we want to be sure that we choose a different static IP address for each of these machines because we have to know which machine that we're going to be SS aging into so for this one instead of 192.168.1 hundred let's do one hundred and one and now the subnet mask is going to be the same so we'll do a net mask of 255.255.255.0 and you'll want to check your typing at this point because it's easy to make typos whenever you're typing all this stuff out so let's go ahead and save both of these so ctrl x and y ctrl X and yes to save okay so now once we've made it to that point I'm going to go ahead and reboot my machine so I'm going to do a sudo reboot and we'll do this on the second machine as well sudo reboot now I think we could have activated those changes without a reboot but since it doesn't take too long I just like to go ahead and reboot at that point just in case okay so once our reboot is complete let's go ahead and log back in to each of our machines and once we're logged into each of our machines let's pull back up the terminal and we'll run a couple of checks here okay so first of all we can see that it has the new hostname for each machine so this is now a boon to one and this is now a boon to two so now if we rerun the ifconfig command ifconfig - a and over here we'll do is config - a now if we look at our second adapter here we can see that now it has an IP address that we set in our network interfaces file and over here let's do the same check so we have our second adapter here and we have a static IP address that's set to the IP address that we specified in that file so now that we have these machines set up on the same network they should be able to communicate with each other and we can test this by pinging their IP addresses so from the first machine we can say I want to ping 192.168.0.101 because that's our second machine is 101 so if I run that you can see that we're getting some ping responses back and from the second machine we can try to ping the first machine so we can say ping 192.168 6.1 hundred and that's our first machine so if we run that we can see that we're getting a ping response back from that as well so now I'll go ahead and kill those and clear our screens okay so that's nice that those machines can talk to each other but also the good thing about this is that if we open up a terminal on our host machine then we should also be able to hit both of those guest machines from our host machine as well so to try this we can do a ping 192.168.1 hundred for our first machine and we can see that we're getting a response back from that and if I do a ping on the second machine then we can see that we're getting a response back from that as well so I'll go ahead and clear that out okay so we can ping those machines but now let's try to SSH into those machines so instead of a ping command here I'm going to do an SSH and then the user that we want to SSH as so core EMS at the IP address so we're going to try to log in to and I'll try to log in to the first machine here so I'll make that a 100 so we're trying to SSH as core EMS on this machine so if I run this now at this point that may have worked for you and it may not have so it didn't work for me so if it doesn't work for you then you might not have OpenSSH installed so if it doesn't work for you then we need to go to our virtual machine and install your SSH server so to do this let's go to each of our machines here and we're just going to do a sudo apt-get update and type in our credentials here and we'll do that on this machine as well and type in our credentials and that just updates our packages list so now I'll clear those out and now we want to install ssh server so to do this we will do sudo apt-get install and this is open ssh all one word with a dash and then server and then it will ask you to continue again you can just hit yes there and we'll do the same thing on our second machine so sudo apt-get install open and then - server and then it'll ask us to continue and we'll hit yes now I should also point out that those commands may be different depending on which version of Linux that you're using so now I'm going to clear our screens here now if you already had OpenSSH server installed and it still wasn't working then one thing that you can try is just restarting that service and to do that you can restart the service by saying sudo service SSH restart and we'll do that over here as well so sudo service SSH restart so now if those changes work correctly then we should be able to SSH into our machine now from our host machine so let's go ahead and try that so that same command that failed a little bit ago if we try this again so now we've got a little further it's asking us if we want to continue connecting and we'll type yes and then for the passphrase I'm just going to leave that blank and hit enter and now it's asking us for our password so we'll put in our password and now we can see here that from our host machine we are on the boon to one machine so the SSH was successful so if I do an LS then this is the home directory of my virtual machine if I do au name - a then we can see that it says that it's Linux Ubuntu so we actually our SSH into that machine so let's try to do the same thing with our second machine so instead of 100 there as the one that we're SSA Qing in - let's do 101 if I hit enter okay so at this point that may have worked for you but also wanted to show you what this looked like as well so if you have been messing around with a few different machines and trying this with a few different machines then it may get to the point where you need to remove your keys belonging to a hostname from a known host file because that's what this error is he so basically with that first machine when we typed in yes and added information to that known host file and if you do this with multiple machines then that can get confusing so to do this and to remove that key from the known host file all we have to do is do ssh-keygen and then a - capital R option and then the IP address and the IP address for this is 192.168.10.10 and I'll clear my screen here now so now let's try to SSH into that second machine again so now it says that it didn't find it and if we want to continue connecting and we'll type yes and then again for the passphrase we'll just leave that blank and now it's asking for our password okay so now we can see that we're SSH into that boom to machine that second aboon to machine ok so once I get the machines to this point where I can login via SSH I usually like to create a snapshot of that virtual machine at this point so I can revert back to that state if I ever mess anything up by experimenting because that's what we want to do within these machines is experiment with different software's so I will go ahead and create two snapshots so on a boon to one here I'll go in and create a snapshot and for the snapshot description I'm just going to say SSH setup and in the snapshot description I usually like to add the static IP address just in case I forget in the future I can look in the description so I'm just going to say 192.168.1 hundred for that one and I will copy that for the next one and for a bun to two machine I'll take a snapshot and again I will just afford the name I'll do SSH set up and down here I will paste in the IP address and that was 1 0 1 and hit OK there oh and actually I just realized that I made a mistake when creating these snapshots if you have watched my snapshot video then you'll remember that you don't want to take snapshots of running machines unless you need to because it'll save the exact state of that machine and takes up way more space so I'm going to shut down these machines first and then create a snapshot and I'll go ahead and skip forward here so that you don't have to see this entire process okay with those snapshots created now I'm going to power these machines back up so that we can SSH back into them one last time okay and lastly let's set this up so that we can SSH into these machines by their hostname so currently if I was to say SSH as core EMS at a boon to one for our first machine and hit enter then it says that it doesn't know that hostname so even though we can SSH into that machine using the IP address it would be nice if we could SSH into it using the hostname as well so to get this working we're going to have to update our hosts file on our host machine so that it knows that that hostname is associated with that IP address and I'll just use Nano again so I'll do sudo nano and this host file is in slash etc' slash hosts and we'll need to put in our password okay and at the bottom of this file I'm going to go down to the very bottom here and first we're going to put in the IP address so we'll put in 192.168.1 hundred and then we'll have a space actually instead I will do a tab and now we want the host name associated with that IP address so I'll do a boom 2-1 and now let's do that same thing for the second one here so 192.168.1 o1 tab and this one is a boom - - and we'll hit control x + y to save that okay so now let's clear our screen and try that again so I'll try to SSH in as a boon 2-1 and we will add that to our hosts file no passphrase now it's asking for our password okay we can see that we're in there and just to double-check let's exit out of that machine and let's try to ssh into a boom - - so we'll type in yes there no passphrase type in our password and we can see that we can SSH into both of those using the hostname okay so I think that is going to do it for this video now I know that that seems like a lot to setup these machines that can communicate with each other and that we can SSH into but really the process isn't that long from start to finish if you do it a few times and like I was saying before this is going to allow us to test a lot of different things in a sandbox environment that we can just scrap and recreate if we mess anything up now one thing that I wanted to mention is that there are a lot of different ways that I've seen other people set this up and this is just my preferred method for several reasons that I'm not going to go into here now there are ways to do this similar thing using port forwarding or bridged network connections or using docker but those are all slightly different behaviors and from my testing needs this method has worked well okay so now that we have some fresh machines set up that we can SSH into maybe I'll try to put together some tutorials on tools like ansible where we can automate the provisioning of multiple machines or we can do something like Jenkins where we can walk through how to get started with continuous integration or anything like that but if you have any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways are simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching Oh you
Info
Channel: Corey Schafer
Views: 99,662
Rating: undefined out of 5
Keywords: virtualbox, virtual box, virtualbox ssh, ssh, virtual lab, network, virtualbox (software), virtual machine, vm, programming tutorial, LAN, software engineering, tutorial, virtualbox network
Id: S7jD6nnYJy0
Channel Id: undefined
Length: 24min 13sec (1453 seconds)
Published: Thu Jul 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.