Remove A Swap File

In today’s article, we’re going to learn how to remove a swap file. This isn’t something I recommend, for reasons I’ll explain, but I have no ethical qualms about telling you how to do so. After all, it’s your computer. You get to decide what it does!

There are some folks who will tell you that if you have enough RAM you don’t need swap. Well, I am not one of those people. I tend to have swap enabled regardless of how much RAM I have. I have a good reason and I’ll explain it to you as best as I can. I mean, the actual inner workings of the kernel are above my head, but I’ll explain it – or let other people explain it.

See, contrary to popular opinion, swap isn’t just a place the kernel sticks stuff when you’re running low on RAM. For example, check out the picture below:

swap in use with plenty of free RAM
As you can see, swap is used even with plenty of free RAM.

Here’s more information about the free command.

Now, do me a favor and go Google Linux and swap. You’ll find plenty of pages that briefly have this to say (and barely this to say):

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full …

                                                                                                        That’s from MIT!

That’s only about half the story… There’s more to it, and that is kinda important.

Some More About Swap:

So, is it really that simple? Of course it isn’t! If it was really that simple, the intro to this would have been so much easier to write. No, it’s slightly more, a bit more, complicated than that. See, let’s quote a more thorough (and accurate) Linux.com:

Swapping is necessary for two important reasons. First, when the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately. Second, a significant number of the pages used by an application during its startup phase may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other applications or even for the disk cache.

                                                                                                     –Emphasis Mine.

What can we take from that? Well, it’s not just a place where the kernel stuffs things when there’s no RAM left – it’s also a place the kernel sticks things to avoid wasting RAM. This is very different than swap is just a place where the kernel stuffs things when RAM is low. It avoids using some RAM that’s best used elsewhere. That seems kinda valuable and important to me. Again, the inner workings of the kernel are pretty much witchcraft to me, but swap seems like a good thing to me.

The kernel is pretty smart about this, from what I’ve observed. It’s good at its job. It’s better at its job than you are. It is better at its job than I am. I highly encourage you to use swap, saving your RAM for more useful things. With disk space as large as it is, you’re not going to miss a couple of gigabytes. Let the kernel do its job, managing RAM as best as it can. They’ve put thousands of hours into making the kernel smartly deal with RAM, there’s no reason to hobble it.

Of course, ain’t nobody gonna listen to me… So, with that said, I might just as well go ahead and tell you how to remove a swap file… You’re gonna do what you want anyhow!

Remove A Swap File:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

The first thing we’re going to do is verify that you actually have a swap file…

If that mentions a swap file, only calling it a ‘swapfile’ (a single word), then your next step is to turn your swap off.

Now, for your next step you will want to remove the swap information from fstab, so let’s go like this to do that:

Find the line that starts with /swapfile and delete the entire line. All of it. Leave nothing behind. If you screw this up, your next boot may be an interesting exercise. The line is probably longer than it looks unless you’re using a terminal in full screen! Be sure to remove all of it. It should end with a 0, actually a 0 and then a tab and another 0.

Be doubly sure to remove the full line and then save your work. Then save it with nano. To do that, just press CTRL + X, then Y, and then ENTER. That’ll save it.

Finally, remove the swap file itself. That one is nice and easy, you just:

That should do it! If you’ve realized you’ve made a mistake and want to have a swap file, you can always add a swap file. (I’m pretty sure it should be ‘swapfile’, but the rest of the ‘net calls it ‘swap file’, so I’m going with the majority – even though the majority don’t even know what the damned thing does!)

Closure:

There you have it. You now know how to remove a swap file. If that’s what you want to do, you go right ahead and do it. Truth be told, assuming you’ve got enough RAM, it won’t necessarily break anything. You might not even notice it is gone. Still, I figure it’s better safe than sorry – and who am I to judge what the kernel wants?

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Shred A Partition

In today’s article, we’re going to shred a partition. What does that mean? It means we’re going to erase the data in a manner that makes it difficult for that data to be recovered. Notice that I didn’t say ‘impossible’? That’s because they’ve got some pretty sweet forensic tools out there and who knows what they can recover.

This will keep your deleted data safe from a common thief or dumpster diver. It’s also not a bad idea to just destroy the drive, perhaps after shredding it, just to be sure. If you’re the enemy of a powerful government, you probably shouldn’t keep data on hard drives unless you’ve secured them very well! This may not help those sorts of people.

The tool we’ll be using is actually called ‘shred’. It’s possibly installed by default, but it’ll be in your default repositories. The command defines itself as:

shred – overwrite a file to hide its contents, and optionally delete it

It further describes itself as:

Overwrite the specified FILE(s) repeatedly, in order to make it harder for even very expensive hardware probing to recover the data.

This is actually a pretty easy article and won’t take much time to go over. There’s just a couple of commands I wanted to cover, and we’ll  just showing you how to shred a partition. So, ‘snot all that complicated, nor all that involved. On with the show!

Shred A Partition:

Like many other articles on this site, we’re gonna need an open terminal. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

First, let’s make sure you have shred installed. Enter this command:

If you don’t have shred installed, you’ll need to install it using your package manager. If you are using apt, then it’d look like:

Once installed, you need to know which partition you want to shred. For that, you’ll possibly need the following:

Once you have identified the partition, such as ‘sdb2’, you can shred the partition. To do that, try this command:

We use the -v for verbose, so it shows us the progress. This command will do the default. It will overwrite the partition three times. You can change that value easily enough. To do so, use a command similar to:

The -n lets you pick the number of times it will shred the partition, that is overwrite it with data, and you can pick any number you want. So, change the ’10’ to whatever you want, but be aware that it can take quite a while.

That’s it, really. There’s more that you can do with shred, so check the man page. I’m only covering how to shred a partition in this article.

Closure:

There you have it, another article. This one tells you how to shred a partition. If that’s something you need to do, you now know how to do it. As I mentioned in the first section of this article, it’s not very complicated. It will take some time, so be sure to use the -v option and keep track of what’s going on.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Disable Bluetooth From Automatically Starting At Boot

In today’s article, we’re going to learn how to disable Bluetooth from automatically starting at boot. It’s a handy thing to know, if you’re like me and have no real use for the service.

I suppose it’d even speed your system’s boot time a trivial amount and reduce the number of running services. So, it’ll make your system a trivial amount more efficient. I just don’t care for Bluetooth so I have a way to prevent it from automatically starting in my notes. These articles are generally based on my notes, so now you’ll know.

NOTE: I should probably also mention that this is really only useful for those of you who use systemd. If you still haven’t moved to systemd, or refuse to move to systemd, then this article isn’t going to be of much use to you. You can still do this, but you’ll have to do so with your particular init system’s commands. These commands are not gonna work for you.

Even if you don’t want to disable Bluetooth from automatically starting at boot, you may want to learn how to disable other services. The process for other services is pretty much the same as it is for Bluetooth. So, you can learn something from this article if you’re new to this whole thing.

This is a nice and easy article, it shouldn’t take much time to read and understand. So then, let’s get on with it!

Disable Bluetooth From Automatically Starting At Boot:

Yeah, that’s longer than it should be. Oh well… Blame the need to optimize for keywords! I try to keep ’em shorter, but here we are…

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With your terminal open, let’s first check the Bluetooth service status:

It may or may not be running. If it’s not running, odds are that Bluetooth isn’t set to automatically start at boot. You can ensure that it’s not set to automatically start with the following command:

Now, that’s the command you probably want. It’ll disable Bluetooth from automatically starting at boot – and it will still let the Bluetooth service start if you want it to later or if something else in the system calls for it to start. You can reboot and make sure the setting has changed, if you’d like.

On the other hand, if you ‘mask’ the service, like we did in the How To: Disable Sleep And Hibernation on Ubuntu Server article, you won’t be able to start the service at all. That’s the biggest difference between ‘disable’ and ‘mask’, in case you’re curious. Both of these options are better than manually deleting the services, ’cause you can undo the setting fairly easily. In fact, to do so with the Bluetooth service, you just:

After which, you should check the status again:

And all should be well. You can now reverse it after you’ve chosen to disable Bluetooth from automatically starting at boot. See? Pretty easy and handy information to have for other services. By the way, if you chose ‘mask’ then you’d ‘unmask’ the service, which seems like an obvious way to do it.

Closure:

See? I told you that this one would be pretty quick and easy. I’m kinda amazed that I haven’t run out of ideas for articles yet! I’m still chugging along, well after the initial year-long scope for the site. If I can do it, anyone can! I’m still very much open to guest articles, within reason.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Let’s Have A Look At The free Command

If the title wasn’t enough, today’s article has us taking a look at the free command. Once again, this will be a nice and easy article. I wanted to write a witty title, but Google doesn’t like witty titles for stuff like this and Google has been mad at me for a couple of months! Either way, it’s a good time for an easy article.

If  you’ve somehow never used the free command, you’re in luck! That’s what this article is going to be about! It should be relatively short, as there’s really only a few ways you’ll want to use the command.

What is the free command? Well, it’s an application that you run in the terminal (like oh so many of my articles) and it gives you some needed information about memory usage. It’s not fine-grained information, it’s about total usage. 

You can get this information all sorts of ways. Both top and htop will happily spit this information out. There are any number of GUI ways to get this information, such as your task and process manager may have a tab to tell you about memory usage. 

Us? No, we’ll be using the free command. The man page happily describes the free command as:

free – Display amount of free and used memory in the system

See? It’s another one of those terminal applications that does exactly what it says it’s going to do! Imagine that!

The Free Command:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

You can just run the command with nothing else:

That’s the same as you’d get if you ran free -w, by the way.

You can pick the display units as flags. Just look into man free to learn how to do that, but the easiest output for most people to read will be to display the units in megabytes or in ‘human readable’ form. You do those with:

Or the human-readable format:

Or if you prefer base ten:

Next, as you can see there’s a swap and regular memory section. If you have swap enabled, you can get some small benefit from seeing the total memory used. To do that, try this:

Finally, you can run the command periodically and get a running output so that you can monitor memory usage over time. That one’s pretty simple. The -s means seconds, so the following will refresh every ten seconds:

There’s more that can be done with the free command, so be sure to run man free in the terminal. However, those are the most common ways I use the command, so it’s likely to be fairly similar usage needs for you.

Closure:

There you have it, we have an article about the free command. It’s a good way to monitor memory usage, unless you need something more fine grained. If you want, there are are tools for that – including top and htop. But, those are fine article ideas for another time.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

A Few Ways To Count CPU Processors Or Cores

So, in today’s article we’re going to learn a few ways to count CPU processors or cores. This is something you might already know about the system in front of you, but may want to verify with a remote system.

To the Linux system, there’s little difference between an individual processor or a core, or even a thread. Each core will appear as its own processor – and that’s exactly how it should be. Linux will treat each as its own processor and do its best to take advantage of multi-core/multi-CPU systems.

Now… Hmm… I shouldn’t need to explain this, but your computer has a processor called a CPU. It may have more than one, though that’s unlikely in a personal computer. Instead, your CPU may have multiple processors on it – each functioning more or less independently as ‘cores’. Additionally, your CPU cores may have multiple threads and each of those threads will appear as its own CPU.

That’s a very simplistic overview and I have to do things like limit how many characters are in the title of the article – and I don’t really want to try to explain everything. 

For example, you could have a 4 core CPU that’s dual threaded. 4 x 2 = 8, so your system will see 8 CPUs. There’s only one physical processor, but you’re effectively working with 8 of them. (By the way, you can *sometimes* compile software optimized by the number of cores available.)

Count CPU Processors Or Cores:

(or threads, I suppose)

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Now, there are so many ways that you can count the CPU processors or cores. We’re just going to cover a few of them. Feel free to leave other solutions as a comment. I’ll approve the comments as quickly as I can.

Solution #1

We just recently used the ‘cpuinfo‘ file to learn about your CPU. So, we’ll start with that one first. We’ll process it with ‘grep’ and then do some counting:

It might look something like this:

output showing there are 8 CPUs available
You’ll have to use your imagination for the rest. I’m only making one screenshot.

Now, those are individual threads that it’s counting – on the same physical CPU. But, to the system they appear to be individual CPUs. I can assure you, I do not have 8 physical CPUs in my laptop.

Solution #2:

Now, your computer does technically know if it’s a physical CPU, CPU core, or CPU core thread. It just normally doesn’t much care. If your CPU and OEM vendor did things properly, you can learn that information using ‘dmidecode‘.

They do not always implement this properly and you will sometimes find mistakes when using ‘dmidecode‘. Let’s hope for the best when we run this more convoluted command:

That one uses egrep and picks out multiple words to include. If you run that command, you should find out that your computer really can tell the difference between cores and threads – and physical CPU count (though not with this command).

Solution #3:

This final solution is nice and easy. If you’re gonna memorize one of these, this would be the one to memorize. In fact, if you’re going to need to count CPU processors or cores, you might just as well use this one. It’s easy and effective.

That one will output the total number of CPUs, cores, or threads. That’s all it will output, just a single number. That number is how many CPUs (effectively) your computer sees – and that’s it. It’s pretty much perfect.

Closure:

There you go. You have another article, this one explaining how to count CPU processors or cores (or threads, really). It’s handy information to know, though you probably already know it. Like I mentioned above, you can optimize C/C++ code to run on the specific number of cores – and there are other reasons you might want to know this. 

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.