John's Linux page

From ProgClub
Revision as of 02:29, 26 July 2011 by John (talk | contribs) (MySQL)
Jump to: navigation, search

Hi there, I'm John. I just wanted a page where I could document various Linux things that I bump into. This is that page. Thank you ProgClub. :)


Configuring vim as your editor

Sometimes all you need is:

$ export EDITOR=/usr/bin/vim

Which works for svn, for example.

Other times you need to run

$ update-alternatives --config editor

And then select vim from the list. This is what you do to configure your visudo editor.

Configuring your locale

$ sudo /usr/sbin/locale-gen en_AU.UTF-8
$ sudo /usr/sbin/update-locale LANG=en_AU.UTF-8

User and group management

Adding a user

To add a new user on a linux system:

# useradd username
# passwd username

To have the home directory created from '/etc/skel' use the 'adduser' script instead:

# adduser username

Adding a user to a group

To add an existing user to an existing group:

# gpasswd -a username group

e.g. to add user 'jj5' to the 'sudo' group:

# gpasswd -a jj5 sudo

Which user am I?

To determine which user you are running as enter the command:

$ whoami

Which groups am I in?

To find which groups you are a member of:

$ groups


$ groups username

Where 'username' is the username of the user you are querying, e.g.:

$ groups jj5


Configuring SSH Key login

On the client machine generate a key-pair (if necessary, check for existing ~/.ssh/

$ ssh-keygen -t rsa

Copy the public key from the client to the server:

$ scp ~/.ssh/

Configure the authorized keys on the server:

$ ssh
$ mkdir ~/.ssh
$ chmod go-w .ssh
$ cat ~/ >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ rm ~/


Dumping a MySQL database

You can dump the database into a file using:

$ mysqldump -h hostname -u user --password=password databasename > filename

Loading a MySQL database from a dump file

You can create a database using:

$ echo create database databasename | mysql -h hostname -u user -p

You can restore a database using:

$ mysql -h hostname -u user --password=password databasename < filename

Creating a MySQL user

# mysql -h localhost -u root --password=<password>
mysql> create user 'username'@'localhost' identified by '<password>';

Granting all MySQL user permissions

# mysql -h localhost -u root --password=<password>
mysql> grant all privileges on dbname.* to user@host;


Applying firewall rules

For configuration info see this article.

$ sudo vim /etc/iptables.test.rules
$ sudo /sbin/iptables -F
$ sudo /sbin/iptables-restore < /etc/iptables.test.rules
$ sudo iptables -L
$ sudo -s
# iptables-save > /etc/iptables.up.rules
# exit


Maintaining .htaccess passwords

To add or modify the password for a user:

$ htpasswd /etc/apache2/passwd username

Configuring PHP session timeout in .htaccess

For a session timeout of 9 hours:

php_value session.cookie_lifetime 32400
php_value session.gc_maxlifetime 32400

Disabling PHP magic quotes in .htaccess

php_flag magic_quotes_gpc Off

Requiring HTTP Auth in .htaccess

AuthType Basic
AuthName "Speak Friend And Enter"
AuthUserFile /home/jj5/.htpasswd
Require valid-user

Restarting Apache

The hard way

$ sudo /etc/init.d/apache2 restart

The graceful way (avoids dropping active connections)

$ sudo apache2ctl graceful


Including a file relative to the including file

require_once( dirname( __FILE__ ) . '/relative/path/to.php' );

Enabling error reporting

 error_reporting( E_ALL | E_STRICT );
 ini_set( 'display_errors', 'On' );

Setting an error handler

set_error_handler( "error_handler", E_ALL | E_STRICT );
function error_handler( $error_code, $error_message, $error_file, $error_line, $error_context ) {
  // ...

BASH scripting

For a primer on bash scripting see TFM: Erotic Fantasy: /bin/sh Programming.

Telling a script to run in bash

The first line of the file should be:


Checking if a command-line argument was not passed in

if [ "$1" = "" ]; then
  echo "Missing parameter 1.";
  exit 1;

Checking command exit status

cd /my/path
if [ "$?" -ne "0" ]; then
  echo "Cannot change dir.";
  exit 1;

Checking if a directory doesn't exist

if [ ! -d "/my/dir" ]; then
  mkdir /my/dir

Deleting old backups

To keep only the latest five backups:

rm `ls -t | awk 'NR>5'` 2> /dev/null

To change into the script's directory

cd `dirname $0`

Disk management

Checking available disk space

$ df -h

File searching

Finding a file with a particular name

$ find -iname "*some-part-of-the-file-name*"

Will start searching from the current directory, so maybe

$ cd /

first. For a case-sensitive search:

$ find -name "*eXaCT CaSE*"

Job control

Stopping a running process

Press Ctrl+Z to stop a running process.

Resuming a stopped job in the backgroud

To resume a stopped process in the background

$ bg %1

where '1' is the job number reported by bash when you pressed Ctrl+Z.

Resuming a stopped job in the foreground

To resume a stopped process in the foreground

$ fg %1

where '1' is the job number reported by bash when you pressed Ctrl+Z.

List current jobs and their status

$ jobs

Debian/Ubuntu package management

Show list of installed packages

# dpkg --get-selections

Searching for installed package

# dpkg --get-selections | grep package-name


# aptitude search package-name

Showing which files are installed as part of a package

# dpkg -L package-name


See Write for information about the write command.