Category Archives: Linux

Disable IPv6 on CentOS 6

Just add or edit the lines below to the relevant files.

/etc/sysctl.conf  :  net.ipv6.conf.all.disable_ipv6 = 1
/etc/sysconfig/network  : NETWORKING_IPV6=no
/etc/sysconfig/network-scripts/ifcfg-eth0 : IPV6INIT=”no”

If you have a different interface than eth0, just replace the last line above with it.

Now you don’t need to reboot your server (as everybody else says on the internet), simply restart the network service and you’re good to go.

service network restart

Don’t forget that, since you’re not using ipv6 anymore, you can disable the ip6tables if active, to avoid extra resource usage.

service ip6tables stopchkconfig –level 345 ip6tables off

Mounting NTFS USB Disk on CentOS

You might get a warning unknown file system type ‘ntfs’ when trying to mount an NTFS disk on a CentOS server.

EPEL Repositories

To overcome this issue, you need the epel repositories enabled. If you haven’t done this before, just get the epel rpm file from the websites below and import them using rpm.
Note that the rpm files are different for CentOS 5 and CentOS 6.

#CentOS 5
wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
#CentOS 6
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

Get them via wget. If you get an error saying there is no such file, just browse the directory listing to get the exact version.

After downloading them, import them like this:

rpm -Uvh epel-release-*.rpm

Now we have EPEL enabled.

Install ntfs-3g and fuse

Now we should install the ntfs-3g and fuse packages using yum.

yum install ntfs-3g ntfs-3g-devel fuse fuse-devel fuse-libs

Mounting and Unmounting

Now you can simply mount and unmount the ntfs file system.

/sbin/mount.ntfs-3g /dev/sdb1 /media/usb
umount /dev/sdb1

Of course the lines above assume you have the device /dev/sdb1 as the usb, and you have created a folder /media/usb.
You can see that unmounting is also quite simply, just like an ordinary disk.

Removing the EPEL Repo

If you don’t want to keep EPEL in your repo for some reason, you can simply remove.

Find the packages name using grep, assuming it is epel-release-6-8 you can use the commands below:

rpm -qa | grep epel
# output is epel-release-6-8
yum remove epel-release-6-8
yum clean all

Cisco Router Backup Script With Python and Telnet

We needed a simple way to backup our network settings on a Cisco device at Veriteknik, so I decided to write a script.

You can simple connect to the device via telnet, declaring your username and password. For security reasons, we use IP address restricting as well.

So, it is quite easy to send and recieve telnet commands in Python, simply use the telnetlib library.

Getting the settings of a Cisco device is quite easy, simply enter the “sh run” command and the output is your settings. But normally the device will output the settings in bits and pieces, using a “more” like function. This is set on the terminal length parameter. To check it, simple use the following command on your device,

asr.vp.net.tr#show terminal | in Length
Length: 62 lines, Width: 195 columns

To set this value to 0, which means you’ll get the full output instantly,

terminal length 0

This command will change the option, but that is only for the current session. When you relog to the device, the value will be set to default, which is a good thing cause we want only our Python-Telnet session to get a non-more-like terminal mode. You can read about this on Cisco’s documentation here.

Now using the script below, we can simple get our backups. This script is for Python 2.x, it won’t be that different if you want to use it with 3.x either.

#!/usr/bin/python

import telnetlib
import datetime

now = datetime.datetime.now()

host = "192.168.1.2" # your router ip
username = "administrator" # the username
password = "SuperSecretPassword"
filename_prefix = "cisco-backup"

tn = telnetlib.Telnet(host)
tn.read_until("Username:")
tn.write(username+"\n")
tn.read_until("Password:")
tn.write(password+"\n")
tn.write("terminal length 0"+"\n")
tn.write("sh run"+"\n")
tn.write("exit"+"\n")
output=tn.read_all()

filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)

fp=open(filename,"w")
fp.write(output)
fp.close()

This script will output a file with a timestamp. This file will contain all the settings (actually the “shell run” output) of your device. Now why not give it a try with a cronjob?

A Backup Script For WordPress

I’ve written a backup script in order to get my WordPress blog backup automatically. The script is only usable on a Linux/Unix box, since it uses default GNU tools.

The script connects to the server via ssh, copies a folder to a location, dumps a database to the same place with the copied folder, creates a tar.gz out of it, then gets the new file via ftp to a prefered location.

The important thing here is that, you should add your ssh public key to the server so that ssh will connect automatically. I also use .my.cnf files to login mysql without specifying password, so you’d better do that. I’ve talked about it in an earlier post here.

Keep in mind that you need an ftp client to connect. If you don’t have it, install it using yum, apt or whatever.

#!/bin/bash

###  START OF EDIT THESE ###
############################
HOST='192.168.1.1' # ip address of your server
SSHUser='root' # user to connect as ssh
FTPUser='myfunkyftpusername' # user to connect as ftp
FTPPass='mysupersecretFTPpassword!' # ftp connection password
MYSQLUser='root' # # user to connect as MySQL

SSHPort=22 # change if different
FTPPort=21 # change if different
DB=wordpress # which database to backup?
DIRECTORY='/home/eaydin/public_html/wp-content' # directory to back up - server side
DIRWRITE='/home/eaydin/' # move the backup here on the server.
DROPBOX='/home/eaydin/Dropbox' # local file path to backup - host side. use your Dropbox folder?
FILENAME='wp-backup' # Filename to use for backups

### END OF EDIT THESE ###
#########################
DIRWRITE=${DIRWRITE%/} # remove trailing / from dir name.
FILENAME=${FILENAME%/} # remove trailing / from filename in case the user types it.
DATE=`eval date +%d%m%Y"-"%H%M` # create date format. (created on the host side, not server. depends on the host time setings.)
FILETAR=$FILENAME-$DATE.tar.gz # name of the tar.gz file (not path!)

ssh -t $SSHUser@$HOST -p $SSHPort "\
cp -R $DIRECTORY $DIRWRITE/$FILENAME-$DATE ;\
mysqldump --add-drop-table -u $MYSQLUser $DB > $DIRWRITE/$FILENAME-$DATE/wordpress.sql ;\
tar -cvzf $DIRWRITE/$FILETAR $DIRWRITE/$FILENAME-$DATE ;\
chown $FTPUser:$FTPser $DIRWRITE/$FILETAR ;\
rm -rf $DIRWRITE/$FILENAME-$DATE
"
ftp -n $HOST $FTPPort <<END_SCRIPT
quote USER $FTPUser
quote PASS $FTPPass
lcd $DROPBOX
cd $DIRWRITE
binary
get $FILETAR
quit
END_SCRIPT
exit 0

The lines between 5 and 17 are the ones you should edit, they’re all self explained in the comments.

It’s a good idea to add the script to your crontab.
In order to do it, especially on Ubuntu systems, just add your current PATH value right below the /bin/sh line. Like this,

eaydin@eaVT:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

So you should add this line at the top of the script,

#!/bin/env bash
PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Or, just run the script whenever you want. I usually set the download path (defined as the $DROPBOX variable on line 16) to my Dropbox folder, this way my backups get automatically synced on the Dropbox server.

smartd Settings on a CentOS Server

smartd is a great tool to keep track of the health status of your server disks. It tracks the S.M.A.R.T records on specified periods and warns you in case anything goes wrong. Even though it is quiet simple, people can get lost while setting up their configuration. Here I’ll explain how my generic settings go. Keep in mind that this is for CentOS servers.

To install the service, simply get the smartmontools package via yum. This will also install mailx if isn’t already installed.

yum install smartmontools -y

Now a file named /etc/smartd.conf will be created. This is where we tell smartd what to do. First, learn the names of your devices using fdisk.

root@eaVT:~# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006f1aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   943237119   471617536   83  Linux
/dev/sda2       943239166   976771071    16765953    5  Extended
/dev/sda5       943239168   976771071    16765952   82  Linux swap / Solaris

This output tells that I have one physical disk (/dev/sda) with three partitions (/dev/sda1, /dev/sda2, /dev/sda3). But we are only interested in the physical devices, which means smartd will only deal with /dev/sda.

Open /etc/smartd.conf using your favourite (vi?) text editor. Find the line that says
DEVICESCAN -H -m root
and comment it out. Then add this line
DEVICESCAN -S on -o on -a -m youremail@yourdomain.com -s (S/../.././02|L/../../0603) -M test
The result should look like this:

# The word DEVICESCAN will cause any remaining lines in this
# configuration file to be ignored: it tells smartd to scan for all
# ATA and SCSI devices.  DEVICESCAN may be followed by any of the
# Directives listed below, which will be applied to all devices that
# are found.  Most users should comment out DEVICESCAN and explicitly
# list the devices that they wish to monitor.
#DEVICESCAN -H -m root
DEVICESCAN -S on -o on -a -m youremail@yourdomain.com -s (S/../.././02|L/../../0603) -M test

Of course, don’t forget to replace it with your own email address. After this simply restart smartd service.

service smartd restart

Now wait for a while and check your email. According to my personal experience, it takes around 5-10 minutes to receive it. You will get a TEST email that says your disks have error. Now that we’ve established you can get the email when an error occurs, lets set it up to a real case.

Go back to /etc/smartd.conf and uncomment the line starting with DEVICESCAN. Don’t forget that there shouldn’t be any line starting with DEVICESCAN on this file, otherwise smartd will halt reading the conf file after it.

Now add the following lines to the /etc/smartd.conf

/dev/sda -H -C 0 -U 0 -m youremail@yourdomain.com
/dev/sda -d scsi -s L/../../1/01 -m youremail@yourdomain.com

Of course, replace the /dev/sda and email address according to yours.

The first line tells smartd to run a silence check on the /dev/sda disk and email us on any error.
The second line indicates that a long check will be made every Monday and 1 a.m. and on any error it will be mailed to us. If we wanted to make the test every Sunday at 6 p.m. the setting would have been L/../../7/18 -m youremail@yourdomain.com

If you’d like to add a new disk, (for example /dev/sdb) simply add it as a new line.

/dev/sda -H -C 0 -U 0 -m youremail@yourdomain.com
/dev/sda -d scsi -s L/../../1/01 -m youremail@yourdomain.com
/dev/sdb -H -C 0 -U 0 -m youremail@yourdomain.com
/dev/sdb -d scsi -s L/../../1/01 -m youremail@yourdomain.com

Now save the file and restart the service again.

service smartd restart

Normally, it is possible that the service won’t get started on reboot. You must add it with chkconfig in order to run it automatically in a CentOS box. To check it:

[root@emre ~]# chkconfig --list |grep smartd
smartd         	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@emre ~]# chkconfig smartd on
[root@emre ~]# chkconfig --list |grep smartd
smartd             0:off    1:off    2:on    3:on    4:on    5:on    6:off

This means that it will run on user levels 2, 3, 4 and 5. What this means is a different story.

So that’s it for now.