Running Rsync and Sudo over SSH

April 7th, 2011 Category: Linux Server

I often use rsync for doing backups. But if you want to do a remote backup and want to backup files and directories which are owned by different users or the root user running using rsync is difficult if you have disabled root login in your sshd_config using the statement PermitRootLogin no. For sure this is the recommended setting if your server to backup has a public IP address.

A nice and easy solution which works best for me is to use sudo. This will allow to run rsync via ssh and sudo. To get it running you have to add an entry into your sudoers config file:

/etc/sudoers

backup_user ALL=(root)NOPASSWD:/usr/bin/rsync

This will allow the user backup_user to run the command rsync as root.

Next step is to add an rsync-path parameter to the remote rsync call:

rsync --rsync-path="sudo rsync" backup_user@myhosttobackup.net:/root /backup_directory

Finally rsync will automatically do an sudo to root on the remote server and all to backup all files owned by root as well.

Links:

socket: Address family not supported by protocol

January 30th, 2011 Category: Linux, Linux Server

I’m using rsync via ssh on my Linux NAS, it works nice. But I was wondering what could cause the message:

socket: Address family not supported by protocol

The message appears every time rsync or just ssh is called. In my case the reason is that ssh tries to connect via IPv6 first and afterwards with IPv4. Since I’ve not setup IPv6 on my NAS the connect fails and the message above appears. It’s not a big harm but it’s nicer to remove this message.

Removing the message is esay, just add -4 as command line parameter to ssh:

-4 Forces ssh to use IPv4 addresses only.

For ssh or rsync the usage looks like:


ssh -4
rsync -e "ssh -4"

And the message disappears.

Quick Rootkit Check

December 2nd, 2010 Category: Linux Server

If you are running your own Linux server connected to the internet it’s a good idea to check if your server is infected from a rootkit. This can be achieved using a few command lines:

cd /root
wget --passive-ftp ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz
tar xvzf chkrootkit.tar.gz
cd chkrootkit-0.49/
make sense
./chkrootkit

Check the output, if you get output indicating “INFECTED” then you should re-install your server and do everything to avoid further infects.

How do you protect your Blog from Hackers?

August 8th, 2010 Category: Latest Featured, Linux, Linux Server, Perl, WordPress

Using WordPress is nice and many websites use it. The advantage of course that there are many tips, additional plugins etc. are available. But on the other hand there is a big disadvantage. Hackers attack WordPress sitse due to many many sites use it. I found an article and video from Matt Cutts an employee from Google.

He recommends to protect your wp-admin area using the Apache .htaccess file. Here in detail access is only allowed from IP addresses from where you are logging in. It’s definitely a good way to protect you blog, but often people don’t have a fixed IP address. The DSL provider assigns a different IP address on every dial in. Unfortunately there is no easy way to obtain the network addresses of your DSL provider which can be entered in your .htaccess file.

Another important recommendation: Always keep your server software and WordPress software up to date!

Also remove the following line from your header.php which will show hackers your WordPress version:

<meta name=”generator” content=”WordPress <?php bloginfo(’version’); ?>” /> <!-– leave this for stats please -->

So let’s concentrate on the restricted access in the wp-admin area. It’s a little bit tricky to get the address list of your DSL provider. I’ll explain and show an example script below. First let’s have a look at the video from Matt Cutts.

As step one you have to find out your own IP address. Let’s assume your IP address assigned from your provider is 213.168.95.20 (this is just an example!). Next do a whois lookup:

server:~# whois 213.168.95.20
...
inetnum:        213.168.95.0 - 213.168.98.255
netname:        NC-DIAL-IN-POOL

Have a look at the netname NC-DIAL-IN-POOL. Usually provider assign  a common name for all DSL dynamic address. But we want to add all IP addresses or better all network ranges into our .htaccess file. Ok now we need to get all address ranges with this netname. This can be achieved using the Linux whois tool:

server:~# whois -h whois.ripe.net NC-DIAL-IN-POOL
...
inetnum:        194.8.209.0 - 194.8.209.255
...
inetnum:        194.8.205.0 - 194.8.205.255
...

Fine we get a list with all network ranges from our provider now, but still not the correct netmask format and a lot of other output which we don’t need. Basically it’s possible to manually generate the .htaccess file now. Not big fun… So I’ve created a small perl script which does the job. As small bonus it creates the needed format for .htaccess file which means you are able to directly add the output to your .htaccess file in the wp-admin folder.

Important: Before usage you have to add the netname of the dial-in pool of your provider. How to find it out is described above. Below is only an example. The output will look like:

server:~# ./gethtaccessranges.sh
allow from 194.8.209.0/24
allow from 194.8.205.0/24
allow from 195.14.226.0/24

Finally generate a .htaccess file in your wp-admin/ folder with the content:

order deny,allow
deny from all

and add the output of the script gethtaccessranges.sh below the two lines. That’s it! Now access to wp-admin is only allowed from dynamic IP address from your provider. Sure it’s not the perfect solution but I’ll prevent you from many other hack attempts from all over the world.

At the end of the script there is also an example in case providers use number in their netnames.

#!/usr/bin/perl -w
# gethtaccessranges.sh
# Harald Kraemer 08.08.2010
# www.technnitip.net
#
# NET::CIDR and NET::Whois is need to run this script.
# On debian you can install using apt-get:
# apt-get install libnet-cidr-perl libnet-whois-perl
#
# Important: Replace the netname "NC-DIAL-IN-POOL" at the end of the
# script with the dial-in netname of your provider! Use the command
# whois <my-ip-address> to find out the netname.

use Net::CIDR;
use Net::CIDR ':all';
use Net::Whois::Raw;
use strict;

sub getRanges
{
  my($netname) = @_;
  my $whois;
  my @whois_array;
  my @ranges;

  $whois = whois( $netname, 'whois.ripe.net' );

  @whois_array   = split( "\n", $whois );
  @ranges        = grep( /inetnum:/, @whois_array );

  foreach ( @ranges )
  {
    s/inetnum://g;
    s/ //g;
    s/ - /-/g;

    foreach my $item ( Net::CIDR::range2cidr( $_ ) )
    {
      print "allow from " . $item . "\n";
    }
  }
}

getRanges('NC-DIAL-IN-POOL');

#for( my $i=1; $i <= 30; $i++)
#{
#  getRanges('DTAG-DIAL' . $i);
#}

Checking DNS Configuration Online

April 2nd, 2010 Category: General, Linux Server

intodns Sometimes it’s need to check your DNS configuration for correct mail-server (MX), name-server (NS) and further settings.

Recently I found a nice online tool which checks the health and configuration of your domain. The online check is available on intoDNS.com. To use it just browse to this site and enter your domain name and you will get many useful checks and information about your server settings.

Links