Linux Tips, Part 2

In my last blog post, I shared some of my commonly used Linux commands. Today I’m going to share a few more. As before, all of these are things done in a terminal window, using the Linux command line. And most will need to be done as the ‘root’ user, or using ‘sudo’. Enjoy.

Last time I talked some about using package management tools (in case I didn’t mention it before, packages are the main way in which software is installed on Linux machines). I left out a good command last time, which is:

dpkg -l

This will list all installed packages on the computer. So for example the output looks like this:

ii apache2 2.2.16-6+squeeze7 Apache HTTP Server metapackage
ii apache2-mpm-itk 2.2.16-6+squeeze7 multiuser MPM for Apache 2.2
ii apache2-utils 2.2.16-6+squeeze7 utility programs for webservers
ii apache2.2-bin 2.2.16-6+squeeze7 Apache HTTP Server common binary files
ii apache2.2-common 2.2.16-6+squeeze7 Apache HTTP Server common files
rc keyboard-configuration 1.68+squeeze2 system-wide keyboard preferences
ii koha-common 3.8.3-1 integrated (physical) library management system
ii mysql-client-5.1 5.1.61-0+squeeze1 MySQL database client binaries
ii mysql-common 5.1.61-0+squeeze1 MySQL database common files, e.g. /etc/mysql/my.cnf
ii mysql-server 5.1.61-0+squeeze1 MySQL database server (metapackage depending on the latest version)
ii mysql-server-5.1 5.1.61-0+squeeze1 MySQL database server binaries and system database setup
ii mysql-server-core-5.1 5.1.61-0+squeeze1 MySQL database server binaries
ii mysqltuner 1.1.0 high-performance MySQL tuning script

The state of the package is in the first three characters. The first character is the desired state, that is, what you (or someone else) has indicated you want for that package. An ‘i’ in the first column means the package has been marked for install. A ‘r’ in the first column means it’s been marked for removal.

The second character gives the current state, so an ‘i’ in the second place means the package has been installed. A ‘c’, esp. in combination with ‘r’ (e.g. ‘rc’), means the package has been removed but some configuration files were left behind.

(The third character is almost always blank, so we don’t worry about that.)

The rest of the output is pretty self-explanatory. The next column is the package name, then the version installed, and finally a brief description of the package.

Anyone who’s used Linux very much should be familiar with the ‘ls‘ command, which lists the directory contents, and it’s expanded format, ‘ls -l‘. In fact I use ‘ls -l‘ so much I’ve enabled an alias for it, ‘ll‘. Aliases are simple things where you set a particular set of characters to be equivalent to some longer command, so ‘ll‘ is actually an alias for ‘ls -l‘. On Ubuntu, the ‘ll‘ alias is usually enabled by default, but on Debian installs it is not. You can enable it for a particular user by editing the ‘.bashrc’ file, located in the user’s home directory. The default .bashrc file that is used for most users on Debian has the alias already in it, but it’s commented out. Just find the line that reads #alias ll='ls -l' and remove the hash mark (#) at the beginning of the line. Type ‘source .bashrc‘ and you now have a handy little aliased commmand. That alias will also be automatically active the next time you login. (Note – This assumes you’re using the bash shell.)

You can add other aliases you might find useful to .bashrc as well. For example, on a site where Koha is installed as a package, you might put the following in your .bashrc:
alias kohaver='cat /usr/share/koha/intranet/cgi-bin/'

So now every time you type ‘kohaver‘ you’ll ouput the script, which gives you the current version of Koha installed.

Command aliases are very handy things to use. You can see all the aliases for a given account by entering ‘alias‘, without any parameters.

Ever need a basic stopwatch while working? Want to time how long a command takes to run? Want to find out how much time you spent doing a particular task? Open up another terminal window to your Linux system and type:
time cat

Press CTRL-C to stop it. It will give you the time passed since you entered the time cat command.

It has happened to all of us. We’re running MySQL on one of our servers, but we have lost the MySQL root user’s password. Here’s a simple way to reset the root user’s password in MySQL.

First stop MySQL:
sudo service mysql stop
sudo /etc/init.d/mysql stop

Restart mysql with:
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &

(Note – You may have to hit ‘Enter’ again to get back to a command prompt.)

The ‘–skip-grant-tables’ option starts mysql without using the privileges system at all, so anyone with access to the server will have total unrestricted access to all databases. Since this is a pretty dangerous state to be in, we also use ‘–skip-networking’ option, which turns off external access to mysql, thus disabling the ability for someone outside of your server to connect during this vulnerable time.

Now go into the mysql command line program with:

You won’t need to enter a username or password, you will automatically have full privileges on all databases. You can now reset the mysql root user’s password with:
mysql> UPDATE mysql.user SET password=PASSWORD('newpass') WHERE user='root';

Then enter:
mysql> flush privileges;

Quit your mysql session. You will need to stop the running MySQL server again, then restart as normal:
sudo service mysql stop
sudo service mysql start

That’s it. MySQL should now start up with a new password for your root user.

Obviously, since this temporarily makes MySQL unavailable, if you do this on a production system, you’ll want to do it during a period of little or no activity.

And that’s all for this time. I hope you found at least some of this useful.


Leave a comment

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.