Sunday, July 22, 2007

Adding a null route to a host on linux

Personally ive found this to be of great help while dealing with ddos attackers or just someone that i need to kick out completely from a server i manage.

As a webhosting company owner / administrator this is a saver and sometimes saves you time of going directly into the firewall and block someone.

This is done by adding the following line to the /etc/sysconfig/static-routes file:

any host 111.111.111.111 reject (being 111.111.111.111 the ip address to block)

When you perform a "route -n" you will see this IP with "-" as it's gateway. This will cause your server to drop all response traffic to this IP.

Destination Gateway Genmask Flags Metric Ref Use Iface
111.111.111.111 - 255.255.255.255 !H 0 - 0 -

If you wish to un-blacklist this IP in the future simply remove the aforementioned line from the static-routes file and restart the "network" service os the server.

Make a backup Using Tar in linux

This is just a small example of how you can use the TAR application with linux to generate a powerfull backup script triggered by a cron.

You will need to create an archive , either using vi or touch.

For example in this case lets say our script is called backup

vi backup

This will create the archive and open the vi editor, to add content to the archive press the I key to insert and then check out at my example here:

#--------------------------------------
#Beggining of script
#--------------------------------------

# Lets say you want to remove an old backup on autobackups folderrm /autobackups/mybackup*.*

# Now lets say you want to make the script sleep for 3 secondssleep 3

#Now you want to backup /home entirely into /autobackups using tar

tar -cvf /autobackups/home$(date +%y%m%d).tar /home


#---END---

Please notice that you are creating a tar archive with the whole content of /home and adding the date of the backup to the name of the archive. To execute this script, you need to save it , on VI press ESCAPE , then activate the CAPS LOCK and press the Z key 2 times. (zz)

You will need to give the archive execute permissions, use the following command: chmod a+x backup , basically to run the script just type ./backup considering you are on the same path of the archive. Easy does it :)

Tuesday, July 17, 2007

Changing the hostname to a Unix box

The following simple instructions can help you change your linux box / server hostname with ease , the first option is permanent , option number two will change the host name but changes wont be reflected after a reboot. Check it out
As described above, there are 2 ways to change the hostname of a linux box manually or by terminal ( real man do it by terminal )

This first option will save the changes even after a reboot:

Edit:

vi /etc/sysconfig/network

HOSTNAME=.

Second option:

vi /proc/sys/kernel/hostname

but that won't be saved when you reboot.

Using *nix bash to connect to an FTP

Have you been in the need of an automatic script that connects to a remote ftp server to copy some backups or something similar? There is no need to be a bash expert, the following simple example can give you the basics to have a fully functional ftp script on bash.
Yes, for any *nix user bash is definitely an usefull part of the operating system, the problem is not everybody has the proper programming basics to interact with bash and really exploit the great power you can find in unix based operating systems like linux or solaris.

The example below, is a simple script that will connect to server with ip address 192.168.2.191 and basically copy a dump file called myexample.dump into the remote location mentioned before as exampleremote.dump

Please notice you need to create a blank archive, lets call it ftpbash.ex

touch ftpbash.ex

Next use VI editor to edit the file:

vi ftpbash.ex

Now copy the following code:

#!/bin/sh
UPASSWD="myuser"
USER="mypass"
ftp -i -v -n 192.168.2.191 <
user $USER $UPASSWD
binary

send /myexample.dump /exampleremote.dump

bye

Next basically save the file ( CapsLock , then press Z twice)

Now to execute the script you need to give execution permissions to it, so type:

chmod a+x ftpbash.ex

To execute it just type ./ftpbash.ex

Please notice on the bash script we are sending both the user and password of the ftp server in order to complete the connection.You could use any valid ftp command on the script like in the example, the command send is a native ftp command, with this basic script im sure you could start a nice learning curve of the usefull features of bash, its a must know for any serious *nix programmer or administrator.

Thursday, July 12, 2007

How to Use VI editor ?

The VI editor is the most common editor on any unix environment, wether your favorite flavor of *nix is solaris or linux or freebsd vi is a must know to develop properly on the unix environments.

Modes :-

Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.

Quitting :-

:x Exit, saving changes
:q Exit as long as there have been no changes
ZZ Exit and save changes if any have been made
:q! Exit and ignore any changes

Inserting Text :-

i Insert before cursor
I Insert before line
a Append after cursor
A Append after line
o Open a new line after current line
O Open a new line before current line
r Replace one character
R Replace many characters

Motion :-

h Move left
j Move down
k Move up
l Move right
w Move to next word
W Move to next blank delimited word
b Move to the beginning of the word
B Move to the beginning of blank delimted word
e Move to the end of the word
E Move to the end of Blank delimited word
( Move a sentance back
) Move a sentance forward
{ Move a paragraph back
} Move a paragraph forward
0 Move to the begining of the line
$ Move to the end of the line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file
:n Move to nth line of the file
fc Move forward to c
Fc Move back to c
H Move to top of screen
M Move to middle of screen
L Move to botton of screen
% Move to associated ( ), { }, [ ]

Deleting Text :-

Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are:

x Delete character to the right of cursor
X Delete character to the left of cursor
D Delete to the end of the linedd Delete current line
:d Delete current line

Yanking Text :-

Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are

:yy Yank the current line
:y Yank the current line


Changing text :-

The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For wxample cw changes a word. A few other change commands are:

C Change to the end of the line
cc Change the whole line

Putting text :-

p Put after the position or after the line
P Put before the poition or before the line

Buffers :-

Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.

Markers :-

Named markers may be set on any line in a file. Any lower case letter may be a marker name. Markers may also be used as limits for ranges.
mc Set marker c on this line

`c Go to beginning of marker c line.
'c Go to first non-blank character of marker c line.

Search for strings :-

/string Search forward for string
?string Search back for string
n Search for next instance of string
N Search for previous instance of string


Replace :-

The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).

:s/pattern/string/flags Replace pattern with string according to flags.
g Flag - Replace all occurences of pattern
c Flag - Confirm replaces.
& Repeat last :s command

Regular Expressions :-

. (dot) Any single character except newline
* zero or more occurances of any character
[...] Any single character specified in the set
[^...] Any single character not specified in the set
^ Anchor - beginning of the line$ Anchor - end of line
\< Anchor - begining of word
\> Anchor - end of word
\(...\) Grouping - usually used to group conditions
\n Contents of nth grouping

[...] - Set Examples [A-Z] The SET from Capital A to Capital Z
[a-z] The SET from lowercase a to lowercase z
[0-9] The SET from 0 to 9 (All numerals)
[./=+] The SET containing . (dot), / (slash), =, and +
[-A-F] The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z] The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z] In the first position, the SET from Capital A to Capital ZIn the second character position, the SET containing all letters

Regular Expression Examples /Hello/ Matches if the line contains the value Hello

/^TEST$/ Matches if the line contains TEST by itself
/^[a-zA-Z]/ Matches if the line starts with any letter
/^[a-z].*/ Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/ Matches if line ends with 2134
/\(2135\)/ Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/ Matches if there are zero or more numbers in the line
/^[^#]/ Matches if the first character is not a # in the line

Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specified

Counts :-

Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with thismethod, say to insert the same line 100 times.

Ranges :-

Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.

:n,m Range - Lines n-m
:. Range - Current line
:$ Range - Last line
:'c Range - Marker c
:% Range - All lines in file
:g/pattern/ Range - All lines that contain pattern


Files :-

:w file Write to file
:r file Read file in after line
:n Go to next file
:p Go to previos file
:e file Edit file
!!program Replace line with output from program

Other :-

~ Toggle upp and lower case
J Join lines
. Repeat last text-changing command
u Undo last change
U Undo all changes to line

Tuesday, July 10, 2007

Linux Disk utilities

Here is a list of Linux Disk utilities


fdisk /dev/hda
(= "fixed disk". As root.) Linux hard drive partitioning utility (DOS has a utility with the same name). In the example above, I specified that I would like to partition the first harddrive on the first IDE interface, hence "hda". If I were you, i would backup any important data before using fdisk on any partition. I do not not know anybody who likes fdisk (either Linux or DOS edition)--I prefer easier to use cfdisk, see next command.

cfdisk /dev/hda
(as root) Hard drive partitioning utility, menu-based. Easier to use then the plain-vanilla fdisk (see the previous command). Physical drives can contain primary partitions (max 4 per disk), and logical partitions (no restriction on number). A primary partition can be bootable. Logical partitions must be contained within "extended partitions"; extended partitions are not usable by themselves, they are just a container for logical partitions. When partitioning a disk, I typically: (1) create a primary partition (2) make the primary partition bootable (3) create an extended partition, (4) create logical partition(s) within the extended partition.

sfdisk -l -x more
(as root) List the partition tables (including extended partitions) for all drives on my system.

parted /dev/hda
A partition manipulation utility for Linux (ext2), and DOS (FAT and FAT32) hard drive partition. It is for creation, destroying, moving, copying, shrinking, and extending partitions. You should really like to backup your data and carefully read info parted before using it.


fdformat /dev/fd0H1440
mkfs -c -t ext2 /dev/fd0
(=floppy disk format, two commands, as root) Perform a low-level formatting of a floppy in the first floppy drive (/dev/fd0), high density (1440 kB). Then make a Linux filesystem (-t ext2), checking/marking bad blocks (-c ). Making the filesystem is an equivalent to the high-level formatting. I can also format floppies to different (also non-standard) densities; try ls /dev/fd0 .I am also able to format to the default density (normally 1440k) using fdformat /dev/fd0.


badblocks /dev/fd01440 1440
(as root) Check a high-density floppy for bad blocks and display the results on the screen. The parameter "1440" specifies that 1440 blocks are to be checked. This command does not modify the floppy. badblocks can be also used to check the surface of a hard drive but I have to unmount the filesystem first to do a full read-write check:
mount [to find out which device contains the disk partition I wish to check for bad blocks] umount /dev/hda8 [unoumnt the selected partition]
badblocks -n /dev/hda8 [check the selected partition in a non-destructive read-write mode, so that my data is not erased!]
mount /dev/hda8 [mount the partition back since no info on bad blocks was printed]
If bad blocks are found, they can be marked on the hard drive so that will not be used using: e2fsck -c /dev/hda8

fsck -t ext2 /dev/hda2
(=file system check, as root) Check and repair a filesystem, e.g., after an "unclean" shutdown due to a power failure. The above example performs the check on the partition hda2, filesystem type ext2. You definitely want to unmount the partitions or boot Linux in the "single mode" to perform this (type "linux single" at the LILO prompt or use init 1 as root to enter the single user mode). If errors are found during the filesystem checkup, I accept the defaults for repair.

tune2fs -j /dev/hda2
(as root, only for kernel that support ext3--RH7.2) Adjust the tuneable parameter of an ext2 filesystem. The example above shows how to add a journal to a disk partition (hda2 in this example), effectively converting the file system to ext3 (journaling) filesystem. To complete the transition, you must also edit the file /etc/fstab and change the filesystem type from ext2 to ext3, else you may run into problems--ext2 will not mount an uncleanly shut down journaled filesystem! To check what is the type of the filesystem use mount (with no arguments) or cat /etc/mtab. Other options of tune2fs let you me add a volume label, adjust the number of mounts after which the filesystem check is performed (maximal mount count), or turn on time-based filesystem checks instead (less often used).

dd if=/dev/fd0H1440 of=floppy_image
dd if=floppy_image of=/dev/fd0H1440
(two commands, dd="data duplicator") Create an image of a floppy to the file called "floppy_image" in the current directory. Then copy floppy_image (file) to another floppy disk. Works like DOS "DISKCOPY".

mkbootdisk --device /dev/fd0 2.4.2-3
Make an emergency boot floppy. You are typically asked if you would like to make a boot disk during the system installation. The above command shows how to make it after install, on the first floppy drive (/dev/fd0). Your kernel name (needed in the command, here 2.4.2-3) can be determined either by running uname -a or ls /lib/modules .

dd if=diskboot.img of=/dev/sda
booting from a USB .Also check is sda definitely your USB drive? I would imagine that your own system drive might be /dev/sda as all SCSI, IDE and SATA drives use the /dev/sd? naming convention now.

Sunday, July 8, 2007

How to Change Password?

The command to change your password on unix is passwd. The system will prompt you to enter your current password, then a new one. Your chosen password will be checked against a password cracking program, and if it is too easy to crack you will have to try again. Use a mixture of upper and lower case letters, numbers and punctuation characters.