24 June 2010

Some useful Linux commands

Chaning IP address of the system :

Edit this file. Change the IP address and netmask according to your need.
E.g. /etc/sysconfig/network-scripts/ifcfg-eth0
IPADDR= -> change ip address
NETMASK= -> change netmask.
GATEWAY= -> Default gateway

Then restart the network service using /etc/init.d/network restart

Adding default gateway :

Add Gateway to file : /etc/sysconfig/network
Add or change line GATEWAY=

Or use (this will temporarily add gateway)
# route add default gw eth0

Then restart the network service using /etc/init.d/network restart

Find out the MX (mail exchange) server
Linux :
# dig gmail.com -t MX

Windows :
> nslookup -type=MX gmail.com

Creating User
# useradd -c 'comment'

Removing user
# userdel -r

Setting Password for user
# passwd

Checking Runlevel
# runlevel

reads /var/run/utmp file to locate runlevel record. Prints previous and current runlevel seperated by single space.

Creating directory :
$ mkdir
eg. $ mkdir pgadekar

If parent does not exits it will create parent.
$mkdir -p /

Creating file without using any editior:
$ touch

This will create a file with filename of zero size.

Checking ownership of the file
ls -l will show the long listing of the file.

e.g ls -l

-rw-rw-r-- 1 pgadekar pgadekar 0 May 5 17:04 file1
---------- - -------- -------- - ------------- -----
File Perm. L Owner Group S Last updated file name

Changing file/Directory permission
$chmod file/directory

e.g. $ chmod 207 asg-test
will change the direcotry permission of asg-test to -w----rwx

Change the owner of the file
#chown username
#chown -R kumar /var/tmp/pgadekar/asg-test

Some file types
use file command for checking the file type of the file.

$ file
Some example of file type
/bin/ls ELF 32 bit LSB executable
/var Directory
/dev/tty Character special (5/0)
/var/run/gdm_socket No file with this name.
/var/run/sshd.pid ASCII Text file
/home Symbolic link /var/home/
/home/ Directory.

Major and minor number of the devices
Use ls command for finding major and minor number.

$ ls -l
e.g. $ ls -l /dev/tty
output :
crw-rw-r-- 1 root tty 5, 0 May 5 16:00 /dev/tty
here 5, 0 shows the major and minor number.

$ ls -l /dev/hda
brw-rw---- 1 root disk 3, 0 Apr 25 12:40 hda
here 3, 0 shows the major and minor number.

All devices controlled by the same device driver have a common major device number. The minor device numbers are used to distinguish between different devices and their controllers.

Finding size of directory

$ du -sh

e.g $ du -sh /var/tmp
104K /var/tmp

Listing file system details
$ df -h
display all the partition on the system and disk usage of the file system.

Listing the partition of the system
# fdisk -l

This will list all the partition of the system.
Bootable Partition: '*' after the device shows that the device is bootable partition of the system.

Listing only files from directory
$ls -l | egrep -v '^d'
$find /var/tmp -type 'f'

Will display the files that are modified on 10th day from today.
$ find /var/tmp -daystart -mtime 10

Exit code of previous command
$echo $?
0 -> OK 1-> Minor 2-> Serious

Finding currenly logged used
w shows logged user,
$ w -h | cut -d ' ' -f 1 | sort | uniq

Finding the login logs of user
$ last

Last searches back through the file /var/log/wtmp (or the file designated by the -f flag) and displays a list of all users logged in (and out) since that file was created. Names of users and tty’s can be given, in which case last will show only those entries matching the arguments. Names of ttys can be abbreviated, thus last 0 is the same as last tty0.

Finding which user you are logged in as
$ whoami
Prints effective user ID.

Tell how long the system has been running.
$ uptime

Using ps
Listing custom columns
-e is for all the process, -o for custom columns
$ ps -eo pid,user

e.g: printing process id of the process start with 'sendmail'

$ ps -C sendmail

Using Top for checking the memory usage of the process User arrow key for sorting by the columns we needed.

$ top

$ iostat
header displays the statics of average of values. iowait display CPU idle time when i/o was happening.

Grouping files using tar:
$ tar []

: $tar -cvf /var/tmp/pgadekar/usr_local.tar /usr/local
this will create a tar file usr_local.tar from the files /usr/local.

Creating an archive from the file containing list of file & directories:
$ tar -cvf --file-from
$ tar -cvf -T

Listing the cron jobs
To list the jobs of other user you need root privileges
# crontab -l

1 comment:

  1. I think the best way to change IP is through a proxy. Everything you do on the internet is sent through the company's server so you want to be sure that you can find a trustworthy company behind the proxy. I would recommend using a proxy from a software called Private Proxy. The software will provide you with up to 10 different IP's from 4 different servers, for maximum security. Visit http://www.privateproxysoftware.com for more information.