Saturday, June 30, 2012

Compilation of Practical Linux Systems Administration Tips and HowTos

 Turn shell functions as callable shell commands

. /path/to/script/containing/function

Using 2>&1 in logging shell script output

> (greater than symbol) means to redirect output somewhere
1 is  fd1 (file descriptor 1) which denotes STDOUT (standard output)
2 is fd2 (file descriptor 2) which denotes STDERR (standard error)
On a side note, 0 means STDINPUT (standard input)
& is AND operator

2>&1 then means, redirect STDERR to STDOUT. This is useful for redirecting all program errors and output to a log file.

run_something > 2>&1 > /path/to/log/file or
run_something > 2>&1 | tee /path/to/log/file

If you wish to append to the file instead of replacing its contents all the time, you can use >> instead of >

run_something > 2>&1 >> /path/to/log/file

How to write email via CLI using "mail" for automated scripts

mail -s "My Subject" < message.txt

You can also customize header variables like 'from' address using the -S

mail -S -s "My Subject" < message.txt

In a shell script, check if an error was thrown and error level is not zero after running a command, script or program using dollar-question mark ($?).

Error level in a shell is denoted by dollar - question mark ($?). If something goes wrong from a previous execution, it will then have a value other than zero. To use in shell script,

if [ $? -eq 0]; then
    echo "success";
    echo "fail";

To do arithmetic expansion and evaluation with a shell script, use "let" or double parenthesis construct.

let a=1+2;
echo $a;


echo $a;

Password-less Remote Authentication Using RSA/DSA Public Keys on Servers.

First attach generate your client RSA public/private key pair. In Linux systems it is usually generated by,

ssh-keygen -t rsa

The above the generates (public key) and id_rsa (private key)

Then, copy the contents and add it to the specific user of your choice in the target server's authorized_keys.

For root user append the contents of below,


For normal users append the contents of to the home directory of the target user.


Note that on the remote server, logged in as the user  and running the command `ssh-keygen` will do the same thing as when you are in your client where it generates a private and public key file. In the process, it also creates the correct SSH directory where the system would look for authorized_keys file, in this case $HOMEDIR/.ssh. It is recommended that you use this method in creating your .ssh directory of the logged-in user then create the authorized_keys file.

ssh-keygen #this creates the .ssh directory, public key and private key
cd $HOMEDIR/.ssh
touch authorized_keys

For a user called test with a home directory in /home/test, the sequence of commands will be as follows.

ssh-keygen #this creates the .ssh directory, public key and private key
cd /home/test/.ssh
touch authorized_keys

Once that is completed, append the public key to the authorized_keys file

/path/to/user/test/ >> authorized_keys

You can now log-in using the user added to the authorized_keys using the normal ssh connection but no need to type in password.

ssh test@server

Change date and timezone

Backup and link the zoneinfo so it reflects to your current date settings.

mv /etc/localtime /etc/localtime-default; ln -sf /usr/share/zoneinfo/Asia/Manila /etc/localtime;

Create htpasswd file for basic authentication

htpasswd -b[cmdpsD] password.file user password

Notice that -b option is required which indicates to use the password provided in the command line.

  • Related Links Widget for Blogspot

No comments: