Thursday, August 16, 2012

LINUX / UNIX TIPS & TRICKS

# 1.Sometimes when on a Linux / Unix system, you will encounter a problem with running out of disk space.


When you check with df -h command you find there is enough space on the server . This is because there are actually two different resources on a Linux filesystem.
Disk space – this is the total amount of space allotted to all of your files.
Inodes – this is the amount of files you can create on the filesystem.


You might be running out of inodes. This will usually happen if you have a lot of small files. You can find out by running

df -i

And you will get output similar to this:

---> root@tanishka# df -i
Fil
esystem        Inodes       IUsed      IFree            IUse%         Mounted on
/dev/mapper/VolGroup00-LogVol00
                      2080768   46917     2033851         3%                     /
/dev/xvda1     26104          35           26069          1%                 /boot
none              262144          1          262143         1%                /dev/shm


On some linux filesystems, you can change this value on a running filesystem. It’s not recommended to try doing so on ext2/ext3, and the option doesn’t even exist on some later versions of tune2fs. The best way to increase this value is to copy your data off, rebuild the filesystem and allocate more inodes(using the -N) option, and copying it back on. Or you could find another drive or storage medium and mount it underneath your root directory, thereby giving you another disk full of inodes to use. The path you’ll want to take is dependent on your own circumstances and goals.

# 2. How can I identify what file systems my current kernel can handle?


The kernel provides a list of file system types it is able to mount via the /proc file system. To view the list, run the command cat /proc/filesystems. The output will look something like: 

nodev proc
ext3
ext2
vfat
iso9660
nodev nfs
nodev smbfs


In this output, the entry vfat means you can mount FAT/VFAT (Microsoft Windows) partitions. The entries ending with smbfs and nfs mean you can interact with file servers that use SMBFS (Microsoft's Server Message Block File System, accessed via Samba) or NFS (Network File System). The iso9660 indicates that you can mount standard CD-ROM file systems, and ext3 and ext2 indicate that you can mount those kinds of Linux file systems.

In the first column, nodev indicates that the file system is not associated with a physical device, like the /proc file system itself, which has information about state of the running kernel.

#3. I have downloaded an RPM in a Windows machine using Internet Explorer then transferred it to my Linux machine. When I try to install it with the RPM command, I immediately get a prompt back and it does not install. So how can I fix this issue ?


When downloading a file, Internet Explorer sometimes places square brackets in the resulting filename. When the rpm command is run on a file with square brackets, it fails with no error message and does not install the package. The square brackets are a part of the Linux bash shell and are reserved for a technique called file globbing.

 To fix this, rename the RPM with the mv command. When specifying the filename of the RPM at a shell prompt, type the first few letters, then use the Tab key to auto-complete the name. This ensures that the brackets in the filename are properly delimited. Rename the file to so that it does not contain square brackets. For example: 

mv kernel-2\[1\].4.21-20.EL.i686.rpm  kernel-2.4.21-20.EL.i686.rpm

Alternatively, you can enclose the file in single quotes, like so:

mv 'kernel-2[1].4.21-20.EL.i686.rpm'  kernel-2.4.21-20.EL.i686.rpm

After renaming the file, the rpm command can be used to install the package successfully.


#4. How can  I change the MTU for my network interface?


The MTU can be set by editing the configuration file for the device. To see the devices you have use the ifconfig -a command. The output should look similar to the following:

root@tanishka:~# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:30:48:fc:23:40
inet addr:192.168.2.71 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::230:48ff:fefc:2340/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:331886304 errors:3569 dropped:0 overruns:0 frame:3569
TX packets:347890316 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:214976371051 (214.9 GB) TX bytes:223013785955 (223.0 GB)
Interrupt:27 Base address:0x4000

eth1 Link encap:Ethernet HWaddr 00:30:48:fc:23:41
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:21 Base address:0x2000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:4381514 errors:0 dropped:0 overruns:0 frame:0
TX packets:4381514 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1646215405 (1.6 GB) TX bytes:1646215405 (1.6 GB)


Alternatively, you can use the ip link list command with the following output: 

root@tanishka:~# ip link list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:30:48:fc:23:40 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 00:30:48:fc:23:41 brd ff:ff:ff:ff:ff:ff
4: vboxnet0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff

The interface eth0 has an MTU set to 1500 bytes. To change the setting temporarily (to 1200 in this example) issue the following command as root: 

ip link set dev eth0 mtu 1200

To make the setting permanent for eth0, edit the configuration file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the line MTU=1200 as shown below:

DEVICE=eth0
MTU=1200
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet


Then, restart the interface (as root):

service network restart eth0

#5. How To Debug script ?


Sometimes it can be difficult to debug scripts. For example, a script only fails if it’s being executed by an application and you have no way of telling the application how the script should be executed to redirect the output. Or you simply don’t want to redirect the output of the script each time you execute it.

Adding the following lines at the beginning of the script can be very useful:


export PS4='$0.$LINENO+ '
exec > /tmp/script.log
exec 2>&1
set -x

Example:


 #cat test
#!/bin/bash
export PS4='$0.$LINENO+ '
exec > /tmp/script.log
exec 2>&1
set -x
ls -ld /etc
ls -ld /boot
echo "This is a test"


$ ./test
$ cat /tmp/script.log
./test.6+ ls -ld /etc
drwxr-xr-x 83 root root 7512 2006-07-22 16:49 /etc
./test.7+ ls -ld /boot
drwxr-xr-x 5 root root 1960 2006-07-22 15:30 /boot
./test.8+ echo 'This is a test'
This is a test
$

These lines will turn on debugging and all information will be redirected to the log file. So you won’t have to redirect the output each time you run the script, e.g. “./script > /tmp/script.log 2>&1″. In some cases you can’t do that if the script is invoked by an application.

The PS4 builtin shell variable describes the prompt seen in debug mode. The $0 variable stands for the name of the script file itself. $LINENO shows the current line number within the script. The exec command redirects I/O streams. The first exec command redirects stdout stream 1 to /tmp/script.log. 2>&1 redirects stderr stream 2 to stdout stream 1. And “set -x” enables debugging.



#6. How to create a Linux Fork Bomb ?


fork bomb is a form of denial-of-service attack against a computer system

[root@server ~]# fbomb(){ fbomb | fbomb & } ; fbomb



#7. How to block all ports except port 21,22,80,8080 ?

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
iptables -A INPUT -p tcp -m multiport --dport 21,22,80,8080 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

These will allow nothing in, nothing out, nothing forwarded, except the four specified TCP ports, replies, and associated traffic. You'd also=20 need "insmod ip_conntrack_ftp" for both passive and active FTP to work,=20 so that all data communications would be RELATED to the control port 21.=20 (and "insmod ip_conntrack" if you don't have it already, for the state=20 match to work)



#7. How to flush memory ?

To free dentries and inodes:
#echo 1 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:
#echo 2 > /proc/sys/vm/drop_caches

#echo 3 > /proc/sys/vm/drop_caches




DISCLAIMER: The information provided on this website comes without warranty of any kind and is distributed AS IS. Every effort has been made to provide the information as accurate as possible, but no warranty or fitness is implied. The information may be incomplete, may contain errors or may have become out of date. The use of this information described herein is your responsibility, and to use it in your own environments do so at your own risk.


Copyright © 2012 LINUXHOWTO.IN



2 comments: