Category Archives: Linux

Linux: How to clone and split screens using Xrandr

XRandr is a very handsome tool to deal with multiple screens in Linux. It allows to clone split, clone and re-arrange the output. It has been developed as official user-interface for the RandR protocol which serves to configure the X11 server. It is a bit difficult to use. However, if you know some magic commands and create aliases the tool may still do some magic as will shown below. For the too trick we assume that you are using a laptop with a VGA port where an external screen is plugged in.

In the first exercise we just want to clone this external screen:

xrandr --output LVDS-0 --auto --output VGA-0 --auto --same-as LVDS-0

After executing that line you should just have a exact copy of your screen on the external one connected by VGA.

I the second exercise we want to split the screen between the Laptop screen and the external one.

xrandr --output LVDS-0 --primary
xrandr --auto --output VGA-0 --mode 1920x1080 --right-of LVDS-0

This example adds the external screen right of your laptop screen. With the –left-of option you can add it at the opposite site.Make sure that you have to choose a resolution which is supported by both screens.

How can I backup a MySql Database or export it to a file?

Imagine you have a large MySql database and want to backup it or backup your wordpress blog which also uses a MySql database. There is nothing easier than just using the handsome mysqldump command. It writes all the information to a sql script which you then can use to restore the databases at any machine you want. It is only two steps to create the script and restore the database.

To export the database just use the following command:

 mysqldump -uroot -pmypassword  --all-databases > backup.sql

In that case just all databases will be backed up. Of course the command also offers options to backup single databases.

If you want to restore the database you have backed up later you can doing by the following even shorter command:

mysql -u root -p < alldb.sql

One common error mysql dump might throw is the following one:

mysqldump: Couldn’t execute ‘show fields from `general_log`': Can’t create/write to file ‘/tmp/#sql_63f9_0.MYI’ (Errcode: 2) (1)

If you get it do not panic! It is usually simply a permissions issue or a problem with discspace. You must make sure that the “mysqld” user has the permissions to write its tmp directory (in this example it is /tmp). If not your will get this error.

If you verified that you really have enought disc space and futhermore the permissions are correctly set but exporting your sql database still does not work there might be another cause for the problem:

You might get this error if you are using an older version of mysql in combination with a newer version of a Read Head based linux system (e. g. Fedora). There have been bug reports that the OS does not allow mysql to the directory /tmp. This is really odd but in my case really was the cause of the problem! The solution here is also an easy one, if you find it:

vi /etc/my.cnf 

Then add the tmpdir variable to the [mysqld] section:

[mysqld]

tmpdir=/var/tmp

In a last step just restart the mysql deamon:

/sbin/service mysqld restart

You will not believe how smooth the export feature is now working. Enjoy!

Find and Replace all occurrences of a word with VIM

This post will be short one. If you have to work at remote servers with really slow internet connections and also have to edit files there you might get stuck with VIM. Yes sure, this program does not have a graphical user interface but it is extremely powerful! Do not underestimate it. One of the most common tasks for a text editor is finding and replacing a word or a entire pattern of course. Since VIM commands are a bit cryptic sometimes they are easy to forget if you do not use them on a daily base. The following one does replace one pattern by another one in the entire file. If your file is large this saves you a lot of typing!

%s/orignial pattern/new pattern/g

Thats it. If you want to use special characters you have to put a “\” in front of them.

A space in vim can be replaced using “\s” or if it is more than one space “\s\s\s”

For tabulators you can use the pattern “\t”.

Howto extract the text between two key words in Bash

Are you having a text file with a repeating pattern of key words and want to extract the content which is written between these keywords. Your file could look like the follows

this is not interesting
hello
this is the text I Want to extract
world
also this line should be skipped
hello
I want also this text
And this one
world
ignore this text

and so on. As you can imagine Bash offers all the tools you need to do the trick. There are plenty of ways to achieve the goal of extracting the text between the keywords from your file. My favorite bash command to do so is the following one.

awk '/^hello$/,/^world$/ { print }' input.txt > output.txt

In case input.txt contains the example line above output.txt will only contain the lines between the key words. The keyword embracing the interesting text will remain in in the output file. You easily can remove them e.g. by using the find and replace feature of vim or any other text editor.

Life can be that easy!

Execute Script on Connection established using Network Manager Dispatcher

If you want a script to be executed each time a Network connection is established there is a easy way to do so with Linux distributions which use the Network Manager (like Fedora, Suse, Ubuntu etc.). You simply have to copy the script to the directory /etc/NetworkManager/dispatcher.d and change the permissions to 755. (It is crucial to use 755, otherwise the script will be ignored.

chmod 755 /etc/NetworkManager/dispatcher.d/10-my-scirpt

The scipts in this directory are executed by the order of their name eg. 10-my-script will be executed before 20-my-script.

Setup Webserver Monitoring with Munin

The tool Munin offers an easy way to monitor a webserver. It comes along with a lot of features like monitoring disk and memory usage, the CPU, the fork rate of new processes and much more. The monitored data is accessible by the web browser. The setup of Munin takes only a few minutes.

First you have to install Munin. In Fedora Linux this can be done via YUM:

yum install munin munin-node

Once installed you have to edit the configuration file /etc/munin/munin.conf (The default file contains a lot of comments). For a basic setup you can add the following lines:


dbdir /var/lib/munin
htmldir /var/www/html/munin
logdir /var/log/munin
rundir  /var/run/munin


[www.example.com]
    address 127.0.0.1
    use_node_name yes

In this case “/var/www/html/” has to be the HTML directory of you webserver. What is missing now is to create the HTML directory for Munin and set the correct permissions:

mkdir -p /var/www/html/munin
chown munin:munin /var/www/html/munin

Restart Munin

/etc/init.d/munin-node restart

The monitoring is running now. The results can be accessed by the webbrowser:

http://www.djvu-pdf.com/munin/

By default there is a .htaccess password protection for this page activated. You can either remove the file /var/www/html/munin/.htaccess or create a username password combination like follows:

You can create the required password file (usually /etc/munin/munin-htpasswd, it is quoted in the .htaccess file) like follows

htpasswd -c /etc/munin/munin-htpasswd Admin

Where Admin is the username. You will be asked for the password in a prompt.

Munin produces intuitive graphs of the monitored resources like the following one:

Clone Hard Disk with Command Line Tools

If you have two hard disks in you machine and you want to be one and exact copy of the other one you can use the tool “dd” to do so.

Lets say you want /dev/sdb to be a clone of /dev/sda, then you can simply use the command:

dd if=/dev/sda of=/dev/sdb

Make sure, that /dev/sdb has at least the same size as /dev/sda!

How long does dd take?

Cloning a hard disk with dd can take several hours.

Assuming a average speed of 8 MB/s copying a disk with 120 MB would take about four hours.

Send Server Ip Address by Email

This post describes to configure a web server in a way that it sents its IP address by email in a defined interval of time. This can be useful for example if your server has a dynamic IP address that changes with the time. In this example the tool Postfix is used to sent the emails instead of sendmail since it works similar and is way easier to configure than sendmail.

Preparations:

1. Install Postfix

In a first step it is necessary to setup and install postfix. You can download the source code here but most recent linux distributions include the package already. In Fedora the setup can simply be done by:

yum install postfix

2. Configure Postfix

You need an SMTP server in order to sent the emails. If you have an email account with SMTP Server you can add the following lines to the postfix configuration file /etc/postfix/main.cf :

relayhost = smtp.provider.com:25
 
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes

Now the password file /etc/postfix/passwd has to be created. The password file has to contain the line

smtp.provider.com:25 username:password

To convert the password file in a so called postmap look-up table do the following:

postmap hash:/etc/postfix/passwd

If

postmap -q smtp.provider.com:25 /etc/postfix/passwd

gives you the correct reply everything is working fine.

3. Test Postfix
To test this configuration, postfix has to be started

postfix start

Postfix now provides a sendmail command you can use like follows to test your setup:

Create a simple text file test.mail:

From: you@provider.com
message
(compulsory blank line at the end)

and execute the command

sendmail -f you@provider.com -s recipient@provider.com &lt; test.mail

The mail should have been sent now. If something is not working errors can be found in the following file:
/var/log/maillog

If you find something like

warning: SASL authentication failure: No worthy mechs found

status=deferred (SASL authentication failed; cannot au
thenticate to server smtp.provider.com: no mechanism available)

you probably have to add the following line to your /etc/postfix/main.cf:

smtp_sasl_security_options = noanonymous

Howto sent the IP by email:

To sent the IP address of you server by emil you simply you have execute the following simple script send_ip.sh by a cron job.

#!/bin/bash
# send_ip.sh

wget http://checkip.dyndns.com/ -O ip.txt
sendmail -f you@provider.com -s recipient@provider.com &lt; ip.txt
rm -f ip.txt

http://checkip.dyndns.com/ can be replaced by any web page that is displaying your IP.

The only thing that is missing now is to setup a cron job that executes the script in a defined interval of time, lets they every six hours.

As root do:

crontab -e 

and add the following line:

0 */6 * * * source /home/user/send_ip.sh ;

Now start the crond and thats it!

/sbin/service crond start
chkconfig crond on

Attention!
You have to make sure, that the postfix mailing system is always running. In case to ensure that postfix sendmail command is not interferring with the “normal” sendmail command stop it.

service sendmail stop
postfix start

SSH Tunnel as Socks Proxy Server for the Web Browser

If you have SSH access to a remote host and you want to use it as proxy server for your web browser you can do so easily by opening up a SSH tunnel:

ssh -D 9999 username@host.com -N

If you now set your socks proxy in your web browser to localhost, port 9999 your traffice is redirect via host.com. In firefox you can do so in Preferences->Network->Settings

Burning MP3 using Fedora and K3b

If you want create Audio CDs from your mp3 files using Fedora K3b offers a easy way. To enable K3b to burn mp3 you have also to install the package k3b-extras-freeworld.

Like this it should work:

yum install k3b k3b-extras-freeworld