How to mount dir via NFS

Hi, for mount dir from another server you have to use NFS service
NFS - Network File System
First we need to install nfs service on both servers.
logon to your server with root user, and run:

# yum install nfs-utils nfs-utils-lib nfs-utils-lib-devel

After installation finished we need to tell the client machine (the one with the existing folder) that we want to share a folder.
edit /etc/exports like this:

# vi /etc/exports
and add the next line:
/the/folder/you/want *(rw,no_root_squash,async)

Now you need to be shore the NFS ports are open on your server: 2049/tcp 2049/udp and 111/tcp 111/udp

After everything done you can start the NFS service on both machines by:

#/etc/init.d/nfs start

Now you can mount your dir via NFS but first you need to open a folder to the mount one, by:

# mkdir /mnt/DIR
# mount servername:/the/folder/you/want /nmt/DIR

that's it, you done.

If you want the mount to be permanently (mount automatic when restart), you can do it by edit fstab file
and add it the next line:

# vi /etc/fstab
/servername:/the/folder/you/want /mnt/DIR nfs defaults 0 0
# mount -a


ENJOY.
Read more >>

Peace, Love, Linux

A new nice wallpaper I found:




Read more >>

How to add a Swap file

Sometimes it is necessary to add more swap space after installation. For example, you may upgrade the amount of RAM in your system. It might be advantageous to increase the amount of swap space if you perform memory-intense operations or run applications that require a large amount of memory.
You have two options: add a swap partition or add a swap file. It is recommended that you add a swap partition, but sometimes that is not easy if you do not have any free space available.

At a shell prompt as root, type the following command with count being equal to the desired block size:

# dd if=/dev/zero of=/swapfile bs=1024 count=1024000

in the count type the amount of space you wont for your swap file. For example, 1024000=1GB

Setup the swap file with the command:
# mkswap /swapfile

To enable the swap file immediately but not automatically at boot
# swapon /swapfile
Or use # swapoff /swapfile to disable the mount.

To enable it at boot time, edit /etc/fstab to include:
# vi /etc/fstab
/swapfile               swap                    swap    defaults        0 0

The next time the system boots, it will enable the new swap file.
After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command
# cat /proc/swaps
or
# free
Read more >>

How To Replicate MySQL Database - Step 2

Go Back Step 1

Getting the data to the Slave.


On the Master Server
I'm assuming you have a live Master server, and an as yet empty Slave server. This stage depends on whether data is constantly being added to the Master. If so, we will have to prevent all database access on the Master so nothing can be added. This means your server will hang during the next step. If no data is being added to the server, you can skip this step. On the Master server, log into MySQL and do the following:
# mysql -u root -p
   Enter password:
   FLUSH TABLES WITH READ LOCK;
   exit;

Now we will use mysqldump to get the data out. So, still on the Master server:


# mysqldump my_database -u root -p > /tmp/database.sql;
# gzip /tmp/database.sql;

Make sure you change my_database to your database name. You will now have a file called database.sql.gz in your temp directory. This is a gziped copy of your database.

On the Slave Server
Now we need to copy over the gzipped file. On the Slave run the following:
# scp root@192.168.1.100:/tmp/database.sql.gz /tmp/



Make sure 192.168.1.100 is the IP of the Master. This will copy the file from the Master and put it in your temp directory on the Slave. Now we just need to import into MySQL:
# mysql -u root -p
   Enter password:
   CREATE DATABASE `my_database`;
   exit;
# gunzip /tmp/database.sql.gz
# mysql -u root -p
my_database  


Finishing

On the Master Server
 Now we need to find the position the Master is at in the logs. So, log into MySQL and run the following:
# mysql -u root -p
   Enter password:
   SHOW MASTER STATUS;

This should give you an output along these lines:


+--------------------------+-------------+---------------------------+------------------+
| File                     | Position    | Binlog_Do_DB              | Binlog_Ignore_DB |
+--------------------------+-------------+---------------------------+------------------+
| mysql-bin.000001         | 21197930    | my_database,my_database   |                  |
+--------------------------+-------------+---------------------------+------------------+
Keep that on-screen.

On the Slave Server
Log into MySQL and do the following:
# mysql -u root -p

   Enter password:
   slave stop;
   CHANGE MASTER TO MASTER_HOST='
192.168.1.100', MASTER_USER='slave_user',  
   MASTER_PASSWORD='your_password', MASTER_LOG_FILE='mysql-bin.000001',   
   MASTER_LOG_POS=21197930;
   slave start;
   exit;

The Slave will now be waiting. So all that's left is to...

Back to the Master Server
To release the tables from lock, Note you only have to do this if you previously run

   FLUSH TABLES WITH READ LOCK;

We shoud already be logged into MySQL, so all you have to do is:
   unlock tables;
   exit;


Read more >>

How To Replicate MySQL Database - Step 1

Configure the Master Serve

First we have to edit /etc/my.cnf, comment out these lines:
#skip-networking
#bind-address            = 127.0.0.1

Now we need to tell MySql to write a bin-log (these logs are used by the slave to see what has changed on the master)
add these lines to /etc/my.cnf in [mysqld] section:
log-bin = /var/log/mysql/mysql-bin.log
server-id=1

If you want to replicate just one database you may add this line also:
binlog-do-db=my_database

Then restart MySql
/etc/init.d/mysqld restart

Then we log into the MySQL database as root and create a user with replication privileges
# mysql -u root -p
Enter password:
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
exit;

Configure the Slave Server

Again, we have to edit /etc/my.cnf file for the salve server,
add these lines to /etc/my.cnf in [mysqld] section:
server-id=2
master-host=192.168.1.100
master-connect-retry=60
master-user=slave_user
master-password=your_password
#replicate-do-db= my_database
relay-log = /var/lib/mysql/slave-relay.log
relay-log-index = /var/lib/mysql/slave-relay-log.index

Master-host – can be IP or host name of the Master Server
Replicate-do-db – add this just if you want replicate one database.
You should also make sure skip-networking has not been enabled.

Then restart MySql:
# /etc/init.d/mysqld restart 


Read more >>

How to block Root access and use sudo permissions

In my servers Policy I usually block the Root user access via ssh,
and I create an admin user how I give a sudo permissions to manage the server.
for disable Root login edit sshd_config file:
# vi /etc/ssh/sshd_config

search the line #PermitRootLogin yes , remove the # from it and change it to 'no'.
do the same to this line: #StrictModes yes
the section in the sshd_config file should look like this:
#LoginGraceTime 2m
PermitRootLogin no
StrictModes no
#MaxAuthTries 6

now restart the ssh service:
# /etc/init.d/sshd restart

OK, now you block the root access, the next step is to create admin user and give him sudo permissions to the commends you like.
How it work?
#useradd admin
#passwd admin
(Enter any password you want to admin user)


#/usr/sbin/visudo
now you need to edit this file to your needs
first create User alias specification
User_Alias ADMIN = admin
then create Command alias specification
Cmnd_Alias CADMIN = /bin/rm, /sbin/service, /bin/chown, /bin/tar, /bin/cp
you can add here any command you want the user admin will have.
and at last you need to create User privilege specification
ADMIN   ALL=NOPASSWD: CADMIN
in the end the file should look something like this:

# sudoers file.
# This file MUST be edited with the 'visudo' command as root.
# See the sudoers man page for the details on how to write a sudoers file.

# User alias specification
User_Alias ADMIN = admin

# Cmnd alias specification
Cmnd_Alias CADMIN = /bin/rm, /sbin/service, /bin/chown, /bin/tar, /bin/cp

# User privilege specification
root    ALL=(ALL) ALL
ADMIN   ALL=NOPASSWD: CADMIN

That's it.

Read more >>

How to bond Ethernet interfaces

Bonding eth-interfaces

If you need to bonding your Ethernet interfaces, Do the following:
add following lines to the /etc/modprobe.conf file
# vi /etc/ modprobe.conf
alias bond0 bonding
options bonding mode=1 arp_interval=100 arp_ip_target=192.168.1.1
create the file /etc/sysconfig/network-scripts/ifcfg-bond0  with the normal IP setting:
# vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
IPADDR=192.168.1.40
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
Add eth0 and eth1 to the bonding pair by editing the files:
/etc/sysconfig/network-scripts/ifcfg-eth0 and
/etc/sysconfig/network-scripts/ifcfg-eth1 to look something like this:
DEVICE=eth0
BOOTPROTO=none
HWADDR=00:17:A4:10:D7:32
ONBOOT=yes
TYPE=Ethernet
MASTER=bond0
SLAVE=yes
Restart the network service:
# /etc/init.d/network restart



Read more >>

How to mount your system with live CD

more then once I was needed to reconfigure my ubuntu grub or change the root password of a certain machine.
If you can't login to the machine or you don't have your root password,
the easy way to do so is to mount the system from a live CD.
I'm using Ubuntu 9.04 live CD:

insert the live CD to your cdrom and restart the computer.
chose the first option of the main menu -
"Try ubuntu whitout any change to your computer"


when it finish loading, open the terminal console and run the next commands.
first we need to find your linux partition
# sudo fdisk -l

than we need to mount into it
# sudo mkdir /mnt/root
# sudo mount -t ext3 /dev/sda1 /mnt/root
# sudo mount -t proc none /mnt/root/proc
# sudo mount -o bind /dev /mnt/root/dev
# sudo chroot /mnt/root /bin/bash

That's it, Now you login the machine as root user, and you can do what ever you want.

for reinstall grub you can use grub-install:
# grub-install /dev/sda1
or
# grub
grub> find /boot/grub/stage1
grub> root (hd?,?)
grub> setup (hd?)
grub> quit

or change the root password with passwd:
# passwd root


Read more >>

Ubuntu 9.04 - Jaunty Jackalope



Every time a new version of my favorite operating system - Ubuntu, comes out,
I invite the original disk for free.
Although I download it straight and updating my ubuntu at home,
I love that I have the original CD - who does not ??

Recently a new version of ubuntu 9.04 - Jaunty Jackalope, comes out.
I ordered the original CD and it just arrived in the mail.

Read more >>

How to add FTP user - script

- After installing FTP server -
check my HowTo post - How to install FTP server.
you want to get the next script for add your FTP users easily.

Create a new script:
# vi /usr/bin/add_ftp_user
and copy the next lines to it
#!/bin/bash
# Script to add a user to Linux system

if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

usermod -g proftpd $username
save and run the command:
# chmod +x /usr/bin/add_ftp_user

For add a new user just run the script
# add_ftp_user
enter a user name and password
and that's it.

Enjoy


Read more >>

How to install FTP server

In every company there is a need to send big files that it's impossible to send them by mail.
The most popular way to do it is to install a FTP server.
My favorite FTP package is ProFTPD .


Installation

add user proftpd
# useradd proftpd

download proftpd-1.3.2 from here:


After download the tar file, run:
# tar -zxvf proftpd-1.3.2.tar.gz
# cd proftod-1.3.2
# ./configure --prefix=/usr --sysconfdir=/etc \ --localstatedir=/var/run &&
# make
# make install

Configuration

edit the configuration file
# vi /etc/proftpd.conf

copy/paste the next configuration to your proftpd.conf
ServerName                      "ProFTPD Default Installation"
ServerType                      standalone
DefaultServer                   on
RequireValidShell               off
Port                            21
PassivePorts                    60150 60200
UseReverseDNS                   off
IdentLookups                    off
ServerIdent                     on "Welcome to FTP Server"

AuthPAM                         on

Umask           022

SystemLog       /var/log/proftpd.log

MaxInstances    30

# Set the user and group under which the server will run.
User            proftpd
Group           proftpd

# Added this line to chroot users in their home dirs
#DefaultRoot     /var/www/html
DefaultRoot     ~

# Normally, we want files to be overwriteable.

AllowOverwrite          on


# A basic anonymous configuration, with no upload directories.
#
#User                    ftp
#Group                   ftp

# We want clients to be able to login with "anonymous" as well as "ftp".
#UserAlias               anonymous ftp

# Limit the maximum number of anonymous logins.
#MaxClients              10

# We want 'welcome.msg' displayed at login, and '.message' displayed
# in each newly chdired directory.
#DisplayLogin            welcome.msg
#DisplayChdir            .message

# Limit WRITE everywhere in the anonymous chroot.
#
#DenyAll
#
#
Create a file /etc/pam.d/ftp with the following content
(otherwise you will not be able to log in with system users using FTP):
# vi /etc/pam.d/ftp
#%PAM-1.0
auth    required        pam_unix.so     nullok
account required        pam_unix.so
session required        pam_unix.so

Extras

IF you useing IPTABLE add the lines to you iptable
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 60150:60200 -j ACCEPT
-A OUTPUT -p tcp --dport 22 -j REJECT
IF you want to Deny from FTP users access to the server via ssh run:
# vi /etc/ssh/sshd_config
and copy the next line to the end of the file
#FTP Group Block ssh Access
DenyGroups proftpd
Read more >>

How to tweet from command line

First, make sure you have "curl" install on your machine.
if not, install it with
"yum" or "apt-get".










Create a new script with your favorite editor, i like vi
# vi /usr/bin/tweet (as root user)
and copy the next lines to it.

#!/bin/sh

tweet="${@}"
user="USER"
pass="PASSWORD"

if [ $(echo "${tweet}" | wc -c) -gt 140 ]; then
echo "FATAL: The tweet is longer than 140 characters!"
exit 1
fi

curl -k -u ${user}:${pass} -d status="${tweet}" https://twitter.com/statuses/update.xml >/dev/null 2>&1

if [ $? == "0" ]; then
echo "Successful tweet!"
fi

Change the USER to your user name and the PASSWORD to your twitter password.
save and run the command:
# chmod +x /usr/bin/tweet

that's it.

run from your command line
# tweet "Testing tweet sctipt"

and you are twitting.

Read more >>

FileMonitor


Hi,
I was needed for notification when one of my logs is changing.
I didn't find any script or program that doing it, so i write my own program.


File Monitor is the small application which help you monitor after file or directory on you server
and send you notification by mail if there any change on them

Pre install

please verify you have "mutt" installed on your machine
mutt is the mail application that File Monitor use to send emails to you
if not, run "yum install mutt" for Red Hat distribute or "apt-get install mutt" for dubain distribute .

Install
after untar the file:
'tar -zxvf fileMonitor.tar.gz'
go to the untared folder: cd /fileMonitor
run "./install.sh" to complete the installation.

After installation
to start fileMonitor first you have to edit "vi /etc/fileMonitor/fileMonitor.conf" file
you have some parameters to change:
1. which file or directory you want to monitor
2. how many time you want to white between checking
3. what you want to do when your target been changed - check "ls /ect/fileMonitor/scripts"
4. what is your mail who receiver the notification

to start fileMonitor run "/etc/init.d/fileMonitor start"
to stop fileMonitor run "/etc/init.d/fileMonitor stop"
or if you want to restart the aplication run "/etc/init.d/fileMonitor restart"









Save File: filemonitor.tar.gz

Discover Simple, Private Sharing at Drop.io
Read more >>

How to delete files, except the recent one

Hi,
I am using this script to delete logs file from my server.
It search all the files in the certain folder and delete them all except the recent one.
this help me to save disk space on my servers.


#!/bin/sh

DIR=/any/dir/you/want

cd $DIR
lastfile=$(ls -rt | tail -1)
for afile in $(ls | grep -v $lastfile)
do
echo > $afile
rm -rf $afile
done

Enjoy.


Read more >>

Linux commands wallpaper

Discover Simple, Private Sharing at Drop.io
Read more >>

How to ssh without password

On client side, (the machine you want ssh to)
Run the next command.
Use the default settings and an empty passphrase:
# ssh-keygen -t rsa

On the remote machine, (the machine you want ssh from)
Run the next line:
# ssh user@remote test -d \~/.ssh \|\| mkdir \~/.ssh \; cat \>\> \~/.ssh/authorized_keys <~/.ssh/id_rsa.pub
don't forget to change the user@remote to your own one.

OR
you can copy the id_rsa.pub from the clinet machine with :
# ssh-copy-id -i ~/.ssh/id_dsa.pub username@remotebox

END.



Read more >>

How to use ftp in a shell script

If you want to send a file to FTP server for backup or for any other reason,
create new script file with your favorite editor
# vi /usr/bin/ftp_put.sh
and copy the next lines to it

#!/bin/sh
HOST='ftp server'
USER='user name'
PASSWD='password'
FILE='$1'

ftp -n $HOST
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

change the "ftp server", "user name" and "password" to your needs.
after saving the file, run the next command
# chmod +x /usr/bin/ftp_put.sh

That's it
run this command to send file to your ftp server
# ftp_put.sh /any/file/you/want


Read more >>