Hey guys and welcome back.
So what I want to talk to you about in this nugget right here is a little bit of a change
of pace.
We're going to talk about the swap on command.
Now when we're talking about swap space, what we mean is that when our RAM is being
taxed a little bit too hard, i.e. we run out of RAM as opposed to just crashing the system,
what the system is going to seek to do is going to seek to use this swap space.
This is going to act as virtual memory, so to speak.
And the way this is going to happen is it's actually going to use the hard disk.
So really, this is not something we want to happen.
This is more of a safety net.
And because we are using the hard disk as opposed to the actual RAM, this is going to
be very slow in comparison.
So the performance will take a big hit, but it is a cheap and very useful solution.
So before we get going, I shall just issue the free command.
We can actually see here that we have our actual memory, which is our RAM.
And below we actually have our swap space.
This is how much of the disk we have allocated that can be used for this purpose of swapping.
Now one thing to note here is with this concept of swapping, we can have a swap partition,
which is a dedicated portion on the disk, which is used for this swapping action.
But we can actually just create a swap file, so both solutions do work.
So like I say, if I issue the swap on command, we can actually see we actually do have a
swap file.
It is a file.
It's not a partition.
We can see the size of the swap and we can see none of it happens to be used.
This is a good sign for us because we don't want our swap file to be used because what
it means, well, it means that our memory is running out and it's not a good thing.
Now with the swap on command, we can actually create or remove a particular swap partition.
Now as it transpires right now, I do not have a swap partition.
Now if I actually wanted to create a swap partition, I could use a swap on command by saying swap
on and then providing the actual device I want to make a swap partition, say for example,
SDC2 or whatever it may be.
Or if I wanted to remove a partition, I could say swap off and then specify the partition
that could remove it.
So I can do the same thing with respect to the swap file.
I can say swap off and then give the name of the swap file.
Oh, and of course, need that super user privilege.
I can now say swap on and I no longer have that swap file.
You see that now if I want to return that swap file, I just say swap on and again, just
say swap file.
And I don't know why I keep forgetting the super user.
So there we go.
Try again, swap on and it is back once again.
Now what I could do is if I wanted to create my own swap file, so what I'll do is I'll
just say swap off swap file and I'll just remove this swap file.
So it's fully gone.
If I wanted to create my own, I could use the command DD.
So I'll say pseudo DD and I'll make the input file equal to something called dev zero.
This is just going to create a file full of zeros and I'll make the output file equal
to swap file.
I'll say the bite size is equal to one meg and I'll make the count 128.
So we'll have 128 units of one meg.
That's going to be the size of the swap file.
If I hit enter, we have now generated this file.
So if I do an LS dash L or rather LS dash L on swap file, we can actually see the size
of the file we have created.
Now, if we want to make this a swap file, what we should do is we should change the permissions
or make this 600 so that only the root user can actually access and modify this file.
The reason being is that this file is going to contain RAM information.
So that could be potentially sensitive.
You don't want regular users to be able to access this file.
So I'll change the permissions.
If I add a whip, notice that the permissions have changed from here.
We only have read writes, nothing for the other group.
And what I now want to do is I want to actually denote that this file is going to be a swap
file.
Now, the way I can do this is I can say pseudo MK swap and then specify the swap file I'm
targeting.
I'm setting up some swap space.
And now what I want to do is I actually want to enable it as a swap on because right now,
if I just say swap on, we don't have anything.
I will say pseudo swap on and then give the name of the swap file.
There we go.
If I now say swap on, we have now generated this swap file of 128 meg.
If our system happens to be taxed, we can now use this space as a little backup.
So really, as we can see here, the swap on command and the swap off command.
In fact, let me just briefly show you.
I can just turn this off once again, swap off for the swap file and swap on.
It's no longer there and put it back.
These commands are very, very useful.
They allow you to control either swap files or if we happen to have a swap partition,
we can do the very same thing.
So really, I just wanted to highlight this at the end of this skill for the purposes
of the examination.
Really do focus on these commands.
I recommend you read the man pages and lab this up.
Okay, doc.
So I hope this has been informative for you and I'd like to thank you for viewing.