Situatie
Solutie
1. Don’t Forget the -a
You might need to add someone to a group to allow them, say, to use a particular piece of software. For example, VirtualBox requires users to be part of the “vboxusers” group. We can do that with usermod
.
The groups
command lists a user’s groups.
groups
We’ll add user dave
to a new group. The -a
(append) adds the new group to the list of existing groups the user is in. The -G
(groups) option identifies the group.
sudo usermod -a -G vboxusers dave
The new group is seen after the user logs in and out.
groups
He’s now in “vboxusers” group. However, if you forget to use the -a
(append) option, all of the user’s existing groups are removed. The only group they’ll be in, is the new group.
This is the incorrect command:
sudo usermod -G vboxusers dave
When they log in next, they’ll find they are only in one group.
groups
If you have a single configured user, and you do this to them, you’ll have serious problems. For one thing, the user is no longer a member of the “sudo” group, so you can’t use sudo
to start correcting things.
2. Using the Wrong Drive Identifier With dd
The dd
command writes blocks of data to file systems. It is often used to write ISO images to USB memory sticks.
The Linux naming scheme for storage devices uses a single letter for identification. The first hard drive is named “/dev/sda”, the second is “/dev/sdb”, the third is “/dev/sdc”, and so on. Partitions are identified by number. The first partition on the first hard drive is “/dev/sda1”, the second is “/dev/sda2”, and so on.
If you burn an image to a USB memory stick, you need to know the drive identifier of the USB memory stick. We’ll find that by piping lsblk
through grep
, looking for entries with “sd” in them.
lsblk | grep sd
We can see that hard drive “/dev/sda” is a 32GB drive with three partitions. One of the partitions is the “/boot” partition, and partition “/dev/sda3” is mounted at “/”, which is the root of the file system.
Hard drive “/dev/sdb” is reported as a 7.5GB drive. It is mounted at “/media/dave/Pink.” Plainly, drive “/dev/sda” is the main hard drive on this computer, and “/dev/sdb” is the USB memory stick.
The command to write an ISO file that’s in the “~/Downloads” directory to our USB memory stick is:
sudo dd bs=4M if=Downloads/distro-image.iso of=/dev/sdb conv=fdatasync status=progress
We’re prompted for our password, then dd
dives into action. There are no “Are you sure?” warnings or chances to back out. The writing starts immediately.
However, if you type the wrong letter for the drive identifier and it matches an existing hard drive, you’ll overwrite that drive instead of the memory stick.
This is the incorrect command:
sudo dd bs=4M if=Downloads/distro-image.iso of=/dev/sda conv=fdatasync status=progress
We told dd
to use “/dev/sda“, so it did. The write action is much faster, but ends with a warning. You’ve just trashed your Linux installation.
Check and double-check drive identifiers before hitting “Enter.”
3. Using the Wrong Drive Identifier With mkfs
There are other commands that take drive identifiers as part of their command line, such as the mkfs
tools. These format drives by creating file systems on partitions.
On this computer we have a 25GB drive and a 10GB drive.
If we want to create an Ext4 file system on the first partition of the 10GB drive, we’d use these commands.
sudo umount /dev/sdb1
sudo mkfs.ext4 /dev/sdb1
But if we make the mistake of using an “a” instead of a “b” in the drive identifier, we’ll wipe out one of the partitions on the 25GB drive and render our computer unbootable.
This is the incorrect command:
sudo umount /dev/sda1
sudo mkfs.ext4 /dev/sda1
That one little letter packs a punch, so make sure you’re hitting the right drive.
4. Don’t Delete Your crontab File
The cron
daemon runs tasks at predetermined times for you. It takes its configuration from a crontab
file. Each user—including root—can have a crontab
file. To edit your crontab
, use this command:
crontab -e
The crontab
file is opened in an editor. You can make changes and add new commands.
But if you mistype the command and hit “r” instead of “e”, you’ll remove—as in delete—your crontab
file.
This is the incorrect command:
crontab -r
The next time you use the crontab -e
command, you’ll see a default, empty file.
This is an easy mistake to make, because “e” and “r” are next door to each other on most keyboards. Rebuilding a complicated crontab
file is no fun.
5. Repeating History
Using the history
command is great when you’re trying to reduce keystrokes and save time. If you can pull a long-winded command out of history, you gain speed and accuracy. As long as you select the right command from your history.
The history
command lists your previous commands in the terminal window. They’re numbered. To re-use a command, precede its number with an exclamation mark “!
“, and hit the “Enter” key.
history
Suppose we’ve cloned a Git repository, got into a mess with it, and deleted it. We need to clone it once more. By scrolling in the terminal window, we can soon spot the git clone
command. We can re-run it by typing:
!60
But if we’ve only glanced at the screen and misread the number, we might pick the next number in error:
!61
That runs the next command in the list, rm *
. That deletes all files in your current directory.
You can also use the “!
” exclamation mark with a string of text. The first matching command is executed for you. It isn’t displayed so that you can check it’s the one you were thinking of, it is executed immediately.
Imagine the scenario where you have a script called “restart.sh”. This script defaults a set of config files for some software you’re writing. Periodically, as you develop and test,you need to wipe the slate clean, so you call your script.
This command should be enough to find and match the command in your history, and to execute it.
!re
But if you’ve used the reboot
command since you last used your script, it’s the reboot
command that is found and immediately executed.
On your single-user home computer that is probably just an annoyance. On a shared server it’s an annoyance for many other people, too.
6. The Calamity of Spaces
Spaces in filenames and directory paths can cause havoc. That’s why they should always be escaped or quoted.
Issues with spaces can be avoided by using tab completion. Press the “Tab” key when you’re typing a filename or directory path and the shell will auto-complete as much of the path or filename that it can. You might need to type a letter to differentiate between the file you want and any other files that share part of the same name, but another press of the “Tab” key will complete the remainder of the filename for you.
This saves on keystrokes, prevents spaces from creeping in because of typos, and correctly escapes any legitimate spaces so that they don’t cause issues.
Let’s say we have a “Development” directory that contains two other directories, “geocoder” and “bin.” There’s also a “bin” directory inside the “geocoder” directory.
To delete the files in the “geocoder/bin” directory and remove the directory, you’d use this command.
rm -r geocoder/bin
Now imagine you inadvertently added a space after “geocoder/”, like this.
This is the incorrect command:
rm -r geocoder/ bin
Boom. The “Development” directory is now empty. The “Development/geocoder”, “Development/geocoder/bin”, and “Development/bin” directories have all been completely wiped out.
Remember, tab completion is your friend.
7. Using > Instead of >>
Redirection sends the output of a process to a file. We use the greater-than sign “>
” to capture output from a process. If the file exists, it is emptied first.
Let’s say we’re investigating a memory leak. We’ve got a script called “memlog.sh.” It displays memory statistics once per second. We’re going to redirect that into a file called “memory.txt”, for later analysis.
memlog.sh > memory.txt
head memory.txt
Next day, we want to continue with our investigation, and we restart the script. This time we need to use two greater-than signs “>>
” so that the new data is appended to the file.
memlog.sh >> memory.txt
If we use a single greater-than sign “>”, we’ll lose yesterday’s data because the file is emptied first.
8. Redirection in the Wrong Direction
Redirection can use the contents of a file as input to a program.
We have a file called “placenames.sql” that we want to import into sqlite3
. The schema file describes how to recreate the database tables. It also holds the data that we want stored in the database. At 1.3GB and over 11 million lines, it’s a big file.
ls -hl placenames.sql
wc placenames.sql
We can create a new database called “places.sqlite3” with this command.
sqlite3 places.sqlite3 < placenames.sql
More often than not, when we’re redirecting we use the “>” character. You must concentrate to avoid typing “>” out of habit. If you do, whatever output sqlite3
generates is written to your schema file, obliterating it.
This is the incorrect command:
sqlite3 places.sqlite3 > placenames.sql
Our schema file has been destroyed, overwritten by the welcome message from the sqlite3
shell.
cat placenames.sql
Bye bye, 1.3GB of data.
Leave A Comment?