Sunday, July 17, 2016

Taming the Linux Beast: Adding My Two Cents

This is not a "Linux 101 Guide"; rather, it is a collection of my ideas and experiences on how I (slightly) improved my working efficiency under Linux (particularly Ubuntu) during my short life as a Linux enthusiast.

  • Save frequently used commands.

Say you found a nice (not nice) command which, unfortunately, has a truckload of parameters; rather than looking up the documentation (man, --usage or --help) every time you need to figure out the right combination, write a small script to encapsulate the whole command (e.g. for wget --method=HEAD -S -O - url, which attempts to retrieve the size of a download without downloading it in the first place). Better still, define an alias (alias dlsize='wget --method=HEAD -S -O -') so you can simply say dlsize url in place of the lengthy expansion.

  • man is your best friend.

Many users have (almost) forgotten that a whole pile of tool and program documentation comes bundled with any decent Linux distro, available at the expense of just one command--man. In addition to the advantage of not having to take your hands off the keyboard, man also allows you to search the manuals on-the-fly for any specific parameters or features you require, and often provides examples for common command use cases.

  • Avoid the urge to sudo.

Whenever their command fails, many users involuntarily retry it with sudo prepended. While this works most of the time, it can easily turn out to be disastrous from safety (you'll get no Permission denied warnings if you accidentally sudo rm -r /bin), security (running a malicious app or script--or even a legit one with an unfortunate corner-case bug--with sudo would allow it to wreak havoc on your system) standpoints.

A good example is docker. In its default installation, Docker not add the current user to the docker group (membership in the group is a requirement for connecting to the Docker daemon). When many users don't know this and run into the traditional error Cannot connect to the Docker daemon, they try the sudo approach and, when they find it working, stick to it for life. The proper approach, however, is to add yourself to the docker group (a simple sudo usermod--yes, here we need a (one-time) sudo) and perform a new login cycle for loading the new user info, after which you can easily use docker without the sudo hack.

  • Mind your file permissions and ownerships.

Whenever you find some file inaccessible due to lack of permissions, don't just blindly (sudo) chmod 777. Some files are not meant to be readable, modifiable or executable by some parties, which is why Linux has taken the trouble of defining three types of permissions (rwx) under three levels of isolation (ugo). Same goes for ownership (chown). In fact, proper maintenance of both these aspects is a key trick in minimizing application and system errors, not to mention unpleasant mistakes of deleting or modifying files.

  • Ease your life with symlinks.

Symlinks can save lots of time if you have to make some 'dynamic' file available at a different location on the filesystem. A good example is a development build process, where you want the build artifact (e.g. WAR file) to be available at a different location for testing. A useful property of symlinks in this case is that, although symlinks are invalidated when the corresponding files are deleted, they automatically become valid again when the files get regenerated, so you don't have to worry about them once they are in place.

  • Read the messages. And the errors. And the logs.

More than half the problems encountered in Linux can be solved simply by looking at the messages printed out upon the fault. Almost all of the rest, you just have to check the respective error logs. It is surprising to see how many people are accustomed to just run a command and google "$command not working" when it fails. A little bit of attention can sometimes save you hours of frustration.

  • Continue to nurture your list of Linux shortcuts and tricks.

With the plethora of resources--inline help (-h, --help, --usage), manpages, logs, online articles, forums and tutorials, it is almost impossible to find an 'unresolvable' problem in Linux. While it is impossible to memorize all this stuff, often it is a good idea to persist the sources in some way (e.g. custom scripts, aliases, browser bookmarks, sticky notes). Meanwhile it is good (and fun) to try and find ways to fix whatever issue you come across during your day-to-day Linux encounters; it would make your life easy and help you admire the simplicity and elegance of Linux.