Linux Tips Aggregated – 2
It took some time, but the next batch of Linux Tips is ready. If you are following me on Twitter then you have already received all the tips. Now they are here again with some explanation just like previous time. All the feedback is appreciated and corrections are gladly accepted. ;) Obligatory ‘your computer might blow up’ disclaimer follows: All these commands were tested on my computer with my K/Ubuntu Linux installation1. Before you actually use any of the commands that are published here, make sure you test them on your computer with your test files, not your actual data. I don’t want to be responsible for any tears and hair being pulled out.
#11 – Permanently erasing your hard drive
Sometime you might actually want to sell your computer2 or perhaps you’re just giving old things away. At this point you want to make sure that nobody can recover your data from your hard drive, right? Deleting your files and formatting the disk would seem like a sensible thing to do. Yes, if you don’t care about your data and you don’t mind your nudie picz appearing somewhere on the Internet . ;)
What happens when you delete your files? This depends on the file system but in general there is this special place on the hard drive that stores the location of all files on the disk. When you delete the file an entry in this table is deleted. The actual data is still kept on disk until somebody overwrites it. When you format the hard drive the process is a bit different but the end result is similar. Hard drive appears empty but all the data is still mostly there. Oh, noes! Can somebody recover this data? Of course! That’s why we need to wipe the hard drive completely.
This is easier than eating pancakes if you’re using Linux!
sudo dd if=/dev/zero of=/dev/sda |
We will use the almighty dd(1) program. Two parameters are required, first is a special device /dev/zero which contains only zeros and second parameter is your hard disk device. Run it and have a lunch, usually it takes a long time to complete depending on the size of your disk. I didn’t test this, but logic dictates that specifying a larger block size will speed up the process. Adding bs=4M to the command might speed things up because dd(1) will write four megabyte blocks at once instead of writing byte per byte.
WARNING! Again, WARNING! With the above command you will delete ALL your data on your sda device3. Really. No, I mean it! Your data will be gone and nobody will be able to help you, not even Three Wise Men. ;)
Special note for spies, secret agents and other international men of mystery. Data on the device will be deleted and gone. However! Some hard core hacker with a multi million dollar equipment could retrieve your data in a lengthy process that requires dedication and costs a lot. So this technique is not good enough for keeping secrets from NSA or CIA and such. Throw the disk in a smelter if you really want to be sure. ;)
#12 – KDE plasma desktop troubles
KDE 4.3 is, after a long time, decently stable desktop environment. However, decently stable doesn’t mean rock-solid stable and it might start acting up on you someday. So if your plasmoids stop responding to clicks and panel seems to be frozen you’ll need to kill the plasma desktop.
If you have an open konsole or other terminal emulator such as xterm then select it. If not, press ALT-F2 to open KRunner and type in konsole to start one. Now all you need to do is to kill existing plasma-desktop process4 and run a new one.
killall -9 plasma-desktop ; plasma-desktop |
This command will effectively kill all the plasma-desktop processes and start a new one which will then (hopefully) behave like it should.
#13 – Mounting FAT partition with read/write permissions
If you are like me you still have some old FAT or VFAT partition laying around somewhere. If nothing else then you surely have a flash drive or your digital camera memory card or a cell phone that connects to your computer. Anyway, those old relics from the beginning of 80′s5 are still all around us. Sometimes mounting them permanently with correct permissions might be a little difficult.
mount -oumask=000 -t vfat /dev/sdb1 /mnt |
Above command will mount /dev/sdb1 partition to the directory /mnt and because of the parameter umask=000 all the files and directories will have their permission set to 777. If you look puzzled, then I assure you, I was too. The umask parameter actually specifies which permission bits should be turned off.
Confusing? Oh, double negation was never confusing. No bits should not be turned on. That’s what umask=000 means. ;)
#14: Mounting a truecrypt disk from a command line
sudo truecrypt -t -k "" --protect-hidden=no /dev/sdh /secret |
Setec Astronomy, right? You have a top secret truecrypted disk connected to the computer and you need to access it to get some secret files from it. However you can’t run TrueCrypt GUI. TrueCrypt is an open source software that can be used to encrypt your hard disk or other devices.
Above command will mount device /dev/sdh to the directory /secret using truecrypt with no key files and it will prompt for a password.
#15 – Kill PC speaker in Ubuntu
Ubuntu as it is great it still has its flaws. One of them is PC speaker. Back in the old days PCs were able to play simple beeps, squeaks and high pitched tones. That was about it and all of them were annoying. 8bit chip music was like an orchestra compared to PC speaker. Today PC speaker is practically never used and whoever in Ubuntu camp decided that it should be used as default system beep must have a weird sense of humor. That is why we will kill the the damned thing permanently.
sudo echo "blacklist pcspkr" >> /etc/modprobe.d/blacklist.conf ; sudo modprobe -r pcspkr |
Two simple commands split by a semicolon in one single line. First we use echo(1) and a little output redirection to append one string to the and of the blacklist.conf file. We just told Linux kernel6 that module pcspkr should never be loaded. This module is essentially a driver for PC speaker. Upon next reboot driver won’t get loaded and your computer speaker will stay silent. Second part of the command removes the driver from currently running system and silences the speaker.
You didn’t really think that you will have to reboot to make this work? ;)
#16 – Remove zero sized files in your current directory and subdirectories
For some reason your home directory is full of files that have size zero bytes. Want to get rid of them?
find ./ -size 0 -print -exec rm {} \; |
First the ./ parameter which tells find(1) to look in current directory then -size 0 which will select all files that are empty. We don’t want to be kept in the dark so we add -print and all the files are also displayed on the screen. At last the -exec which calls rm(1) that takes care of removing files.
A word of caution, as always with Linux, this will delete all the files that have size 0 bytes, make sure that you really don’t need them. Wise thing to do is that every time before you run find(1) with -exec rm {} \; parameter you omit it and check the output if files that are listed are really the ones you want to get rid of.
#17 – Unable to unmount a drive because someone is using it?
Sometimes you leave an open terminal window or a file manager window that can successfully prevent you from unmounting a drive. Same thing happens if there is an open file on that drive. You need to find it before you can unmount.
sudo lsof -n | grep media |
By using lsof(8) and grep(1) we can easily check for these kind of mishaps. First with -n we tell lsof that it shouldn’t resolve IP addresses into hostnames7 because it could take too long. Lsof will list every file and every descriptor opened and the list can be quite long. So we pipe the output through grep and search for the directory of our mounted drive. Here’s an example of the result returned:
bigwhale@thefish:~$ lsof -n | grep /media/Big |
bash 4608 bigwhale cwd DIR 252,0 65536 1 /media/Big |
bigwhale@thefish:~$ |
In my case, I wanted to chech if I have any files opened in /media/Big directory. Lsof found one result. It appears that I have one shell opened and it is not doing anything just sitting in that directory. I could now search for terminal window or I could simply kill this particular shell.
When publishing this tip I was reminded about umount -l command which does the lazy unmount. Drive will be reported as unmounted and you won’t see it. However, it will stay mounted until all the files in use are closed, then the drive will unmount. The problem with this method is that sometimes the drive is never unmounted and in case of an external drive it could lead to data corruption.
#18 – Create a large file
There are times when you need a huge file for some test. For example, you want to see how fast is your uplink and you need a large file for upload. You could find a large file on your disk or you could create it by opening your favorite text editor and copy a bazillion Lorem Ipsum paragraphs in it. Either that or you could again use the almighty dd(1).
dd if=/dev/zero of=file.big bs=1000M count=1 |
Parameters explained: Input file is /dev/zero, output file is our test file. Then block size of thousand megabytes and count of one block. You will end up with a file full of zeros with the exact size of 1000*1024*1024 bytes. :) This command is very similar to the one we used in Tip #11. Here we specify filename as output file instead of the whole device and we limit the number of blocks being written with the count parameter. Otherwise dd will continue to write until it runs out of diskspace.
Be careful when you specify the block size. Unexpected results will probably happen if you specify block size of 10 gigabytes and you don’t have enough memory to store it. If you need larger file then increase the count number.
#19 – Continuous monitoring of a file
When programs write their log files they usually append entries at the end of the file. If you need to continuously monitor one file for new entries you can go insane if you have to reload the file each time you want to refresh your view. Imagine that your file is 10 GB in size. Of course there is an easier solution and tail(1) comes to the rescue. Tail will by default display last ten lines of the file and adding parameter -f will tell it to display additional output as the file grows larger and more lines are appended to the end.
tail -f logfile.log |
#20 – Lost computer, unknown hostname and IP address.
Did you ever forget what is the name of that server that has no keyboard and no screen attached to it and is buried under a pile of junk or perhaps the server is in a nuclear shelter three thousand miles away and you have absolutely no access to it. Unless there is a nuclear war on the horizon, of course. You can try looking for it with ping(8).
sudo ping -b 192.168.1.255 |
We told ping that it should ping the whole broadcast address of our subnet. Broadcast address is the last IP address in a subnet so in a subnet 192.168.1.0/24 the broadcast address would be 192.168.1.255. Try pinging it. You might get a response from the server. If not you’ll have to use some other way of locating it. We will cover this in the future.
All coments are, as always, more than welcome. Until the next time, more tips are coming soon, just follow @BigWhale on Twitter.
- I am using a mix of Ubuntu and Kubuntu. [↩]
- Never happened to me before, but anyway. [↩]
- If your using SATA or SCSI controller and/or device /dev/sda is present [↩]
- If it is still present if not, killing will fail and you can ignore that. [↩]
- Previous century of course [↩]
- More precisely to the mechanism for autoloading kernel modules [↩]
- If you didn’t hear yet, on unix everything is a file and even a network connection can appear as a file. [↩]

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=16eb4631-754f-471d-9065-0fa0a2880f18)