Wednesday, 6 August 2014

User & Group Account Management



useradd
userdel
usermod

groupadd
groupdel
groupmod

chage

Basic - Shell Builtins


alias
echo
 expr
 printf
sleep

The sleep command suspends execution of a process for at least the interval specified. The sleep instruction suspends the calling process for at least the specified number of seconds (the default), minutes, hours or days.

Usage:
sleep number[suffix]
or:
sleep option


Suffix:
s (seconds)
m (minutes)
h (hours)
d (days)




 test
true and false
unset
wait
 yes

Tuesday, 22 July 2014

Server Perfomance Tunnig

uptime

The uptime command provides the information about the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.


[root@subrat ~]# uptime

10:09:52 up 70 days, 18:58,  4 user,  load average: 0.05, 0.00, 0.00

top

htop

free

iostat

sar

Basic - Text Processing

banner

basename

comm

csplit

cut

diff

dirname

ed

ex

fmt

fold

head

iconv

join

less

more

nl

paste

sed

sort

spell

strings

tail

tr

uniq

vi

wc

xargs

Basic - Network Utilities

ping




host 

The host command in linux is used to resolve a host name into an Internet Protocol (IP) address or an IP address into a host name. It is being treated as a DNS Lookup utility.

- To find the IP address of a Hostname.

[root@unixlinux ~]# host google.com
google.com has address 74.125.68.138
google.com has address 74.125.68.139
google.com has address 74.125.68.100
google.com has address 74.125.68.101
google.com has address 74.125.68.102
google.com has address 74.125.68.113

- To find the hostname of an IP address.

[root@unixlinux ~]# host 10.132.88.33
33.88.132.10.in-addr.arpa domain name pointer ip-10-132-88-33.ap-southeast-1.compute.internal.

-To query of type ANY record for the domain.

[root@unixlinux ~]# host -a google.com
Trying "google.com"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13858
;; flags: qr rd ra; QUERY: 1, ANSWER: 19, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.                    IN      ANY

;; ANSWER SECTION:
google.com.             88403   IN      NS      ns4.google.com.
google.com.             88403   IN      NS      ns1.google.com.
google.com.             88403   IN      NS      ns2.google.com.
google.com.             88403   IN      NS      ns3.google.com.
google.com.             120     IN      A       74.125.130.138
google.com.             120     IN      A       74.125.130.139
google.com.             120     IN      A       74.125.130.100
google.com.             120     IN      A       74.125.130.101
google.com.             120     IN      A       74.125.130.102
google.com.             120     IN      A       74.125.130.113
google.com.             300     IN      AAAA    2404:6800:4003:c01::8b
google.com.             600     IN      MX      20 alt1.aspmx.l.google.com.
google.com.             600     IN      MX      30 alt2.aspmx.l.google.com.
google.com.             600     IN      MX      40 alt3.aspmx.l.google.com.
google.com.             600     IN      MX      50 alt4.aspmx.l.google.com.
google.com.             600     IN      MX      10 aspmx.l.google.com.
google.com.             86400   IN      TYPE257 \# 19 0005697373756573796D616E7465632E636F6D
google.com.             86400   IN      SOA     ns1.google.com. dns-admin.google.com. 2014021800 7200 1800 1209600 300
google.com.             3600    IN      TXT     "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"

Received 497 bytes from 10.132.88.2#53 in 5 ms

hostname

ifconfig

 inetd
ip


nslookup

netcat

 netstat

dig

telnet

ssh

scp

ftp
rdate
rlogin 
rsync

route
traceroute

elinks

wget

Friday, 20 June 2014

A Brief about Signals in Unix like OS

[root@subrat ~]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Basic - Process Management

 A process is a dynamically executing instance of a program. Linux is a multitasking operating system. This means that many processes can be running at the same time. As well as the processes we are running, there may be other users on the system also running stuff and the OS itself will usually also be running various processes which it uses to manage everything in general. Any application that runs on a Linux system is assigned a process ID or PID. This is a numerical representation of the instance of the application on the system.

Types of Processes:-
There are generally two types of processes that run on Linux. Interactive & Non-Interactive.
Interactive processes are those processes that are invoked by a user and can interact with the user. e.g. VI is an interactive process. Interactive processes can be classified into foreground and background processes. The foreground process is the process that you are currently interacting with, and is using the terminal as its STDIN (standard input) and STDOUT (standard output). A background process is not interacting with the user and can be in one of two states – paused or running.
Non-Interactive processes are those processes that are system process or Daemon. Daemon is the term used to refer to process that are running on the computer and provide services but do not interact with the console. Most server software is implemented as a daemon. e.g Apache, Samba.


Different States of Processes:-
Running: The process is either running (it is the current process in the system) or it is ready to run (it is waiting to be assigned to one of the system's CPUs).

Waiting: The process is waiting for an event or for a resource. Its of two types, interruptible and uninterruptible.
Interruptible waiting processes can be interrupted by signals whereas uninterruptible waiting processes are waiting directly on hardware conditions and cannot be interrupted under any circumstances.
Stopped: The process has been stopped or halted, usually by receiving a signal and can be restarted by some other process. A process that is being debugged can be in a stopped state.
Zombie: The process has been terminated, but information is still there in the process table. It is what it sounds like, a dead process.


Now let's start with the process management commands in Linux,

jobs

The jobs command displays the status of jobs started in the current session. If the prompt is returned with no information, then no jobs are currently running.

By default the jobs command displays the status of all stopped jobs, all running background jobs, and all jobs whose status has changed but not been reported by the shell.

Understanding the Job Number (jobID)

There are a various ways to refer to a job in the shell. The character % introduces a job specification. The JobID can be a process ID (PID) number, or you can use one of the following symbol combinations:

%Number : Use the job number such as %1 or %2.
%String : Use the string whose name begins with suspended command such as %commandName or %ping.
%+ OR %% : Refers to the current job.
%- : Refers to the previous job.


- To list all the jobs in the current session.
[root@subrat ~]# jobs
[1]   Running                 sar 60 > /home/subrat/sar.txt &
[2]-  Running                 sleep 10000 &
[3]+  Running                 sleep 1200 &


- To list process IDs in addition to the normal information.
[root@subrat ~]# jobs -l
[1]   1363 Running                sar 60 > /home/subrat/sar.txt &
[2]-  1366 Running                sleep 10000 &
[3]+  1369 Running                sleep 1200 &


- To list only the process ID of the job’s process group leader.
 Use: # jobs -p %job-id
[root@subrat ~]# jobs -p %3
1369
[root@subrat ~]# jobs -p %2
1366


bg

fg

ps

kill

The kill command is used to terminate a processes without having to log out or restart the system. Actually, kill command is used to send a signal to a process. By default, the message sent is the termination signal, which requests that the process exit.

Syntax:

        # kill PID
OR    # kill -s SignalName PID
OR    # kill -SignalName PID
OR    # kill -SignalNumber PID

As of now remember the Signal Name & Syntax. We have a different topic for Signal  details.

- To kill a single process or a background job, use the PID or the Job Number.
[root@subrat ~]# kill 1369
 OR
[root@subrat ~]# kill %3

- To kill more than one process at a time.
[root@subrat ~]# kill 1366 1363

- To kill a process forcefully, use -9 potion or call KILL signal.
[root@subrat ~]# kill -9 1363
OR
[root@subrat ~]# kill -s 9 1363
OR
[root@subrat ~]# kill -s SIGKILL 1363
OR
[root@subrat ~]# kill -s KILL 1363
OR
[root@subrat ~]# kill -SIGKILL 1363
OR
[root@subrat ~]# kill -KILL 1363

- To list all the Signals.
[root@subrat ~]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

- To find a Signal Name.
[root@subrat ~]# kill -l 2
INT
[root@subrat ~]# kill -l 19
STOP
[root@subrat ~]# kill -l 9
KILL


- To find a Signal Number.
[root@subrat ~]# kill -l KILL
9
[root@subrat ~]# kill -l SIGKILL
9
 
killall



The killall command is used to kill a process having too many instances and a number of child processes. This command takes process name as argument in-place of process number. 


pwdx

time

pidof

pgrep

pstree

chroot

disown

nice

renice


Thursday, 15 May 2014

Basic - File Permissions


  This is basically based on the concept of ownerships and permissions for files & directories. The purpose of implementing permissions on files and directories is to restrict the read, write & execute access to a file or directory from unauthorized user access. So that we can provide a kind of security on files & directories.

Permissions on Unix-like operating systems are managed in three distinct classes. These classes are known as user, group, and others.

Permission Classes:
User (u) - The owner of the file.
Group (g) - Other users belongs to the group. The owner may be a member of the file's group.
Others (o)- Users who are not the owner, nor a member of the group, considered as a file's others class.


Permission Types:
Read (r) - The read permission grants the ability to read a file. When set for a directory, this permission grants the ability to read the names of files in the directory (but not to find out any further information about them such as contents, file type, size, ownership, permissions, etc.) 
Write (w) - The write permission grants the ability to modify a file. When set for a directory, this permission grants the ability to modify entries in the directory. This includes creating files, deleting files, and renaming files.
Execute (x) - The execute permission grants the ability to execute a file. This permission must be set for executable binaries (e.g. a compiled C program) or shell scripts in order to allow the operating system to run them. When set for a directory, this permission grants the ability to access file contents and metainfo if its name is known, but not list files inside the directory (unless read is set).

How to check permission ?
Just issue ls -l command to a file or directory, you will be able to see the permission associated with that file.

# ls -l test.txt
-rwxr-xr-x. 1 subrat sysadmin 2225 May  7 14:32 test.txt

Look at the first field of the output. This field is the permission associated with the file or directory. Just separate it as below, you will be able to understand.

-rwxr-xr-x
 -       rwx         r-x         r-x
   |                       |                              |                                 |
File Type   User Permission     Group Permission   Others Permission

File Types - 
-  Regular File
d  Directory File
l  Linked File

In similar way, for directory issue ls -ld command to a directory.
# ls -ld test
drwxr-xr-x. 2 subrat sysadmin 12288 May 12 11:44 test
 

In the above example, for user, there is read, write & execute permission, for group, there is read & execute permission, and for others, there is read & execute permission.

Octal representation of Permissions -
The following octal numbers are being used for representing permission.
r -- 4
w -- 2
x -- 1

For e.g. You want to set permission as follows,
  For user as rwx (read, write, execute)
    User --- 4+2+1 --- 7
  For group as r-x (read, execute)
    Group --- 4+0+1 --- 5
  For others as r-- (read)
    Others --- 4+0+0 --- 4
So, the effective permission in octal will be 754.

Now we will learn how to change the permissions, ownership, group of a file or directory.

chmod

 The chmod command is used to change the access permission to a file or directory. It can be done by the owner of the file or by the user having admin privilege.

It can be done in two different ways, Normal way & Numeric way.
In Normal way, we have use the following different options,
     u=user, g=group, o=other, a=all (default)
In Numeric way, we will use the octal representation for effective permission.
       r=4, w=2, x=1
       e.g.  rwx = 4+2+1 = 7
        rw- = 4+2+0 = 6
        r-x = 4+0+1 = 5
        -wx = 0+2+1 = 3
        r-- = 4+0+0 = 4
        -w- = 0+2+0 = 2
        --x = 0+0+1 = 1
        --- = 0+0+0 = 0

Note: Using -wx option is of no meaning as because if a user do not have read permission, then what is the meaning of giving him write & execute permission.

Also we need to use the following symbols for adding & revoking access permission,
     + means add permission
  - means remove permission
  = means assign permission and remove the permission of unspecified fields


Here we go...

- Adding permissions for user/owner to file or directory.
# chmod u+r file.txt
# chmod u+w,u+x file.txt
# chmod u+r,u+w,u+x dir1

- Adding permissions for group and others to a file or directory.
# chmod g+r,o+r file.txt
# chmod g+w,o+x dir1

- Adding multiple permissions in a single line.
# chmod u+rwx,g+rx,o+x file.txt
# chmod a+r,u+wx,g+x dir1
# chmod u+rwx,go+r dir2
# chmod ug+rwx,o+rx dir3
# chmod ugo+rwx dir4

- Revoking permissions from user, group & others.
# chmod u-w,g-x file.txt
# chmod a-x,o-rw file.txt
# chmod g-w,o-rwx dir1
# chmod go-wx file.txt

The above all examples will add or remove the read, write & execute permissions to or from the existing permissions respectively of a file or directory. That means with the existing permissions, the operations will be done on file or directory accordingly & leave all other privileges as it is.

There is also another way, it won't look for the existing permission. It will simply overwrite the new permission you want to apply to a file or directory. This can be done with '=' operator.

# chmod ug=rwx,o=rx file1
# ls -l file1
-rwxrwxr-x. 1 subrat sysadmin 73740 Apr  9 06:42 file1

# chmod a=rwx file1
# ls -l file1
-rwxrwxrwx. 1 subrat sysadmin 73740 Apr  9 06:42 file1


This also can be done using the numeric way as follows,

# chmod 777 file2
# ls -l file2
-rwxrwxrwx. 1 subrat subrat 3324 Apr  9 06:43 file2

# chmod 755 scripts/
# ls -ld scripts/
drwxr-xr-x. 2 root root 4096 May 12 12:54 scripts/


Note: For directory at least provide the execute permission if you want others to enter to your directory.

- Apply permission to multiple files or directory in a single line.
# chmod 751 file1 file2 file3
# ls -l file1 file2 file3
-rwxr-x--x. 1 subrat sysadmin 73740 Apr  9 06:42 file1
-rwxr-x--x. 1 subrat sysadmin  3324 Apr  9 06:43 file2
-rwxr-x--x. 1 root   root      3744 Apr 10 09:40 file3
 

- To provide the recursive privileges for the directory, sub-directories & the files.
# chmod -R 755 scripts/



Friday, 2 May 2014

Basic - User Information & Activities on Server.

finger

The finger command displays information about the system users. We can view all information about user if we know the user account login name.

- To view details about a particular user.

# finger subrat
Login: subrat                         Name: Subrat Nayak
Directory: /home/subrat               Shell: /bin/bash
On since Mon Apr  1 18:45 (IST) on :0 (messages off)
On since Mon Apr  1 18:46 (IST) on pts/0 from :0.0
New mail received Fri May  1 10:33 2014 (IST)
Unread since Sat Apr  19 12:59 2014 (IST)
No Plan.


- To view login details & idle status about an user.
# finger -s root
Login     Name  Tty   Idle  Login Time   Office     Office Phone
root      root  *1    2d    Mon 13:45
root      root  *2    1d    Wed 19:53
root      root  *3          Thu 08:20
root      root  *ta   2     Fri 21:43
root      root  *tb   2     Sat 05:44


who

The who command shows currently who is logged on to the system. This is a useful command because it can check how many users currently logged in the system, who they are & what they are doing.

[root@subrat ~]# who
root    pts/0        2014-05-02 06:22 (subrat.com)
subrat  pts/1        2014-05-02 07:29 (subrat.com)
dinesh  pts/2        2014-04-30 19:17 (subrat.com)


- To view the date & time of last system boot.
[root@subrat ~]# who -b
         system boot  2014-04-19 15:12

- To view the system's current runlevel.
[root@subrat ~]# who -r
         run-level 3  2014-04-19 15:12


- To lists users logged in. After the login time, the who command prints the number of hours & minutes that the user has been idle.
[root@subrat ~]# who -u
root    pts/0     2014-05-02 06:22    .       17634 (subrat.com)
subrat  pts/1     2014-05-02 07:29  00:11     18109 (subrat.com)
dinesh  pts/2     2014-04-30 19:17   old      13276 (subrat.com)

Note: . means the user was active in the last minute.
         old means the user has been idle for more than 24 hours.


- To view all login names & the number of users logged on.
[root@subrat ~]# who -q
root subrat dinesh
# users=3

- To view only hostname & user associated with stdin. It the same as running who am i command.
[root@subrat ~]# who -m
root    pts/0        2014-05-02 06:22 (subrat.com)


- To list only the entries that correspond to processes via which the system is waiting for a user to login. The user name is always LOGIN.
[root@subrat ~]# who -l
LOGIN    tty2         2014-04-19 15:12              1214 id=2
LOGIN    tty1         2014-04-19 15:12              1212 id=1
LOGIN    tty3         2014-04-19 15:12              1216 id=3
LOGIN    tty4         2014-04-19 15:12              1218 id=4
LOGIN    tty5         2014-04-19 15:12              1222 id=5
LOGIN    tty6         2014-04-19 15:12              1224 id=6


w

The w command shows who is logged on in Linux system and what they are doing. Its function is identical to the who command but the w command output provides more detail than who command. Somehow, the first line is the output of uptime command which has explained in other post.

[root@subrat ~]# w
07:30:21 up 12 days, 16:18, 2 users, load average: 0.03, 0.00, 0.00
USER     TTY      FROM    LOGIN@  IDLE   JCPU   PCPU  WHAT

root     pts/0    subrat  06:22   0.00s  0.26s  0.23s sshd: root
subrat   pts/1    subrat  07:29   28.00s 0.02s  0.02s -bash
 

[root@subrat ~]# w -l
07:45:54 up 12 days, 16:34,  2 users, load average: 0.03, 0.00, 0.00
USER     TTY     FROM  LOGIN@   IDLE   JCPU   PCPU WHAT

root    pts/0   subrat 06:22   0.00s  0.27s  0.23s sshd: root
subrat  pts/1   subrat 07:29   16:01  0.02s  0.02s -bash
 

- To view login details without header.
[root@subrat ~]# w -h
root   pts/0  subrat 06:22   0.00s  0.27s  0.23s sshd: root

subrat pts/1  subrat 07:29   16:30  0.02s  0.02s -bash

whoami

The whoami command displays the user name (i.e., login name) of the owner of the current login session. After switch to other user, it will show the switched user's login name.

 root@subrat ~]# whoami
root

[root@subrat ~]# su - dinesh
[dinesh@subrat ~]$ whoami
dinesh


who am i

The who am i command allows you to display the login name, terminal name, & time of the login for that terminal. It is terminal dependent. After switch to user, unlike whoami, it will display the logged in user's information only, not about the switched user's information.

[root@subrat ~]# whoami
root
[root@subrat ~]# who am i
root    pts/0        2014-05-02 06:22 (subrat.com)
[root@subrat ~]# su - dinesh
[dinesh@subrat ~]$ whoami
dinesh
[dinesh@subrat ~]$ who am i
root    pts/0        2014-05-02 06:22 (subrat.com)
 

logname

The logname command displays the login name of the user who has loged in to that terminal. It is terminal dependent. After switch user, it won't display the switched user login name rather than the logged in user's name.

[root@subrat ~]# logname
root
[root@subrat ~]# su - dinesh
[dinesh@subrat]$ logname
root


users

The users command is used to display the user names of users currently logged in to the current host.
[root@subrat ~]# users
dinesh root subrat


id

The id command is used to display the system identifications of a specified user. We can find the information like user's UID, user's GID, all the secondary groups an user belongs to.

- To display the currently logged in user's UID, GID & other secondary groups associated with the user.

[root@subrat ~]# id
uid=0(root) gid=0(root) groups=0(root),2(daemon),3(sys),4(tty),5(operator),8(procview),12(everyone)


- To display the UID of currently logged in user.
[root@subrat ~]# id -u
0


- To display the GID of currenlty logged in user.
[root@subrat ~]# id -g
0


- To display all the secondary groups, the currenlty logged in user associated with.
[root@subrat ~]# id -G
2 3 4 5 8 12

- To display all the secondary groups names instead of ID, the currenlty logged in user associated with.
[root@subrat ~]# id -nG
daemon sys tty operator procview everyone


- To display the UID, GID & other groups associated of a particular user.
[root@subrat ~]# id subrat
uid=512(subrat) gid=512(subrat) groups=512(subrat),523(unix),531(sysadmin)


- To display the UID of a particular user.
[root@subrat ~]# id -u subrat
512

- To display the username instead of UID of a particular user.
[root@subrat ~]# id -nu subrat
subrat

- To display the GID of a particular user.
[root@subrat ~]# id -g subrat
512

- To display the GID of a particular user.

[root@subrat ~]# id -ng subrat
subrat


- To display all the secondary groups ID, an user associated with.
[root@subrat ~]# id -G subrat
523 531

- To display all the secondary groups name instead of ID, an user associated with.
[root@subrat ~]# id -nG subrat
unix sysadmin


groups

The groups command displays the groups a user is in.

- To display the currently logged in user's groups.
[root@subrat ~]# groups
root : root daemon sys tty operator procview everyone


- To display the groups of a particular user.
[root@subrat ~]# groups subrat
subrat : subrat unix sysadmin


members

The members command is used to list the usernames of the users belongs to a particular group.

[root@subrat ~]# members sysadmin
subrat kiran alert operator portaladmin

lid

The lid command is used to list the usernames and user ids of the users belongs to a particular group.

[root@subrat ~]# lid -g sysadmin
subrat(uid=512)
kiran(uid=534)
alert(uid=567)
operator(uid=900)

portaladmin(uid=902)

last

The last command is used to listing the last times a user logged in on server. It keeps the information from the first boot of the server.
[root@subrat.com ~]# last | less
[root@subrat.com ~]# last
subrat   pts/1        subrat.com       Fri May  2 07:29   still logged in
root     pts/0        subrat.com       Fri May  2 06:22   still logged in
...
...
dinesh   pts/0        subrat.com       Thu Apr 24 10:10 - 11:39  (01:28)
subrat   pts/1        subrat.com       Wed Apr 23 09:27 - 12:05  (02:37)
root     pts/0        subrat.com       Sat Apr 19 15:13 - 15:42  (00:28)
reboot   system boot  2.6.32-431.el6.x Sat Apr 19 15:12 - 11:00 (12+19:48)
...
...
reboot   system boot  2.6.32-358.el6.x Sun Dec 22 17:38 - 12:00  (18:21)


- To display the login details of a particular user.
[root@subrat.com ~]# last subrat
subrat   pts/1      subrat.com  Fri May  2 07:29   still logged in
subrat   pts/1      subrat.com  Mon Apr 14 11:01 - 13:07  (02:05)
subrat   pts/2      subrat.com  Wed Apr  9 14:20 - 14:42  (00:21)
subrat   pts/1      subrat.com  Wed Apr  9 13:54 - 14:22  (00:28)
subrat   pts/0      subrat.com  Wed Apr  9 13:33 - 13:39  (00:06)
...
...


- To hide the hostname field.
[root@subrat ~]# last -R subrat
subrat   pts/1        Fri May  2 07:29   still logged in
subrat   pts/1        Mon Apr 14 11:01 - 13:07  (02:05)
subrat   pts/2        Wed Apr  9 14:20 - 14:42  (00:21)
subrat   pts/1        Wed Apr  9 13:54 - 14:22  (00:28)
...
...


- To display the complete login & logout times.
[root@subrat ~]# last -F
subrat   pts/1    subrat.com Fri May  2 07:29:53 2014   still logged in
root     pts/0    subrat.com Fri May  2 06:22:59 2014   still logged in
dinesh   pts/0    subrat.com Mon Apr 28 12:46:20 2014 - Mon Apr 28 12:54:23 2014  (00:08)
subrat   pts/0    subrat.com Mon Apr 28 08:50:53 2014 - Mon Apr 28 10:45:33 2014  (01:54)
...
...


- To find users last logged in/out time from a specified time to back.
Use: last -t YYYYMMDDHHMMSS

[root@subrat ~]# last -t 20140314110100
dinesh   pts/2        subrat.com  Fri Mar  7 10:02 - 11:20  (01:17)
subrat   pts/1        subrat.com  Fri Mar  7 09:36 - 11:45  (02:08)
...
...
root     pts/0        subrat.com  Sun Dec 22 17:41 - 18:35  (00:54)
reboot   system boot  2.6.32-358. Sun Dec 22 17:38 - 12:00  (18:21)


- To display last shutdown time.
[root@subrat.com ~]# last -x shutdown
shutdown system down  2.6.32-431.1.2.e Sat Apr 19 15:12 - 15:12  (00:00)
shutdown system down  2.6.32-431.1.2.e Thu Feb 13 12:00 - 12:01  (00:01)

...
...
shutdown system down  2.6.32-358.el6.x Mon Dec 23 21:09 - 12:00 (51+14:51)


Friday, 11 April 2014

Basic - File Handling

file

The file command is a used for recognizing the type of data contained in a file.
 # file mon.tar
mon.tar: POSIX tar archive (GNU)

# file sanu/
sanu/: directory

# file file1
file1: ASCII text


- To check the types of all files in a directory.
# file /home/subrat/*
/home/subrat/file1:      ASCII text
/home/subrat/mon.tar:    POSIX tar archive (GNU)
/home/subrat/my.pkt:     ASCII text
/home/subrat/sanu:       directory
/home/subrat/sar.txt:    ASCII text


stat

The stat command is used to display status information of files and file systems.

- To check the status of a file.
# stat file1
  File: `file1'
  Size: 73740           Blocks: 152        IO Block: 4096   regular file
Device: caa1h/51873d    Inode: 5317        Links: 1
Access: (0600/-rw-------)  Uid: (  512/  subrat)   Gid: (  512/  subrat)
Access: 2014-04-11 09:40:37.095008865 +0530
Modify: 2014-04-09 06:42:21.635010321
+0530 
Change: 2014-04-09 06:42:21.635010321 +0530

- To check the status of a directory.
# stat /home/subrat/
  File: `/home/subrat/'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: caa1h/51873d    Inode: 8196        Links: 5
Access: (0700/drwx------)  Uid: (  512/  subrat)   Gid: (  512/  subrat)
Access: 2014-04-11 08:40:32.213008990
+0530
Modify: 2014-04-11 08:40:21.753009285
+0530
Change: 2014-04-11 08:40:21.753009285
+0530

- To check the status of a file system.
# stat -f  /dev/xvdk1
  File: "/dev/xvdk1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 69065      Free: 69025      Available: 69025
Inodes: Total: 69065      Free: 68440


locate

The locate command is often the simplest and quickest way to find the locations of files and directories. 

- To search a particular file.
# locate sar.txt
/home/subrat/sar.txt
/opt/apps/sar/sar.txt


- To locate the file irrepsective of its name.
# locate -i new.txt
/tmp/NEW.txt
/tmp/new.txt
/usr/share/doc/samba-common/WHATSNEW.txt.gz 


- To get the count of number of matching entry.
# locate -c sar.txt


Note: One limitation of the ‘locate’ command is its dependency on the database which can be updated by another utility ‘updatedb’. Hence, in order to get the latest and reliable results from ‘locate’ command the database on which it works should be updated at regular intervals.

updatedb

The updatedb command is used to refresh mlocate Database.
# locate out.mp

I have created some files with the name 'out.mp'. But I am not able to  find. So, once run 'updatedb', then tried searching.
 

# updatedb
# locate out.mp
/home/subrat/out.mp
/home/subrat/sanu/out.mp
/home/subrat/sanu/out.mp.bak


which

The which command is used to locate executable in the system. It returns the absolute path of the executable that is called. It allows user to pass several command names as arguments to get their paths in the system. “which” commands searches the path of executable in system paths set in $PATH environment variable.

Syntax: which command | program

# which java
/usr/bin/java

You can search for multiple commands or programs also as below.
# which echo telnet chmod java
/bin/echo
/usr/bin/telnet
/bin/chmod
/usr/bin/java


whereis

The whereis command is used to find the source, binary, and manuals sections for specified commands or files.

# whereis find
find: /bin/find /usr/bin/find /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gz


- To search only binary files.
# whereis -b find
find: /bin/find /usr/bin/find


- To search only mannual sections files.
# whereis -m find
find: /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gz


- To search only source code files.
# whereis -s date
find:


- To search for multiple commands.
# whereis date grep
date: /bin/date /usr/share/man/man1p/date.1p.gz /usr/share/man/man1/date.1.gz
grep: /bin/grep /usr/share/man/man1p/grep.1p.gz /usr/share/man/man1/grep.1.gz


whatis

The whatis command is used to display a single line information about a command. It accomplishes this by searching the short descriptions in the whatis database for each keyword provided to it as an argument.

# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents

# whatis find
find                 (1p)  - find files
find                 (1)  - search for files in a directory hierarchy



Basic - Commands beginner's should know

clear

The clear command is one of the most handy command used to clear your terminal screen. The clear command does not affect files or jobs, it simply clears the clutter from your terminal screen. Another nice thing about `CLEAR` is that it doesn't erase your terminal scroll buffer, so if you need to see something that was on your screen a while ago, you can still use your mouse wheel to scroll back.

# clear
or CTRL + L

date

The date command is used to display the current date and time or set the system date/time

 # date
Fri Apr 11 11:50:40 GMT 2014


- To set a new date and time
   # date -s "Fri Apr 11 11:00:00 GMT 2014"
or # date -s "11 APR 2014 16:00:00"
or # date --set="11 APR 2014 16:00:00"

- To set date only
# date +%Y%m%d -s "YYYYMMDD"
# date +%Y%m%d -s "20131120"

- To set time only
# date +%T -s "HH:MM:SS"
# date +%T -s "16:24:30"


cal

The cal command is used to print the current month's calender. With this command also we can see any year/month's calender.

# cal
     April 2014
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30


- To see a particular year's calender (e.g. 1995)
# cal 1995

- To see a particular month's calender of a year ( e.g. Feb 2005)
 # cal 2 2005

- To see last few months calender
# cal -3

uname

The uname command reports basic information about a computer's software and hardware. It privides the information about the name, version and other details about the current machine and the operating system running on it.

# uname
Linux

uname without any ooption prints the kernel name


- To get all the inrforamtion about the system  

# uname -a
Linux ip-10-132-73-9.ap-southeast-1.compute.internal 2.6.32-431.1.2.el6.x86_64 #1 SMP Sun Nov 24 09:37:37 EST 2013 x86_64 x86_64 x86_64 GNU/Linux


- To get the operationg system name
# uname -o
GNU/Linux

- To get the kernel name
# uname -s
Linux

- To get the hostname
# uname -n
ap-southeast-1.compute.internal

- To get the kernel release information
# uname -r
2.6.32-431.1.2.el6.x86_64


- To get the kernel version name
# uname -v
#1 SMP Sun Nov 24 09:37:37 EST 2013

- To get the machine hardware name
# uname -m
x86_64
- To get the processor type
# uname -p
x86_64


- To get the informaiton about hardware platform information
# uname -i
x86_64 
 


hostname

The hostname command is used to view or change the hostname of the system.

- To view the hostname of the system
# hostname

- To change the hostname
# hostname subrat.com
# hostname
subrat.com


- To see the IP of the host
# hostname -i

-To see the Fully Qualified Domain Name of the host
# hostname -f

history

The history command is used to see the command line history i.e. lists the executed commands.

- To display the command line history
# history | more
- To display the Time stamp with commands

# export HISTTIMEFORMAT='%F %T  '
# history | more
 127  2014-04-11 08:31:16  vim /etc/sysctl.conf
  128  2014-04-11 08:31:16  sysctl -p
  129  2014-04-11 08:31:16  vim /etc/sysctl.conf
  130  2014-04-11 08:31:16  cat /proc/self/mapped_base
  131  2014-04-11 08:31:16  history
  132  2014-04-11 08:31:16  vim /etc/sysctl.conf
  133  2014-04-11 08:31:16  sysctl -p
  134  2014-04-11 08:31:16  byobu
  135  2014-04-11 08:31:16  aws help

- To clear command line history
# history -c

alias

The alias command allows a user to create simple names or abbreviations (even consisting of just a single character) for commands regardless of how complex the original commands are and then use them in the same way that ordinary commands are used.

Syntax: alias name='command'

- To set an alias
# alias p="pwd"

- To see a list of aliases set up on your system
# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'

alias p='pwd'

unalias

The unalias command is used to remove entries from the current user's list of aliases.

# unalias p

# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'





Thursday, 10 April 2014

Basic - File Opetaions

pwd

The pwd is an acronym for Print Working Directory. The pwd command is considered as one of the most frequently used commands on Linux, UNIX like operating systems along with the ls, and cd commands.

Purpose for typing pwd command are,
  - Find the full path to the current directory.
  - Store the full path to the current directory in the shell variable.
  - Verify the absolute path.
  - Verify the physical path i.e exclude symbolic links.

# pwd
/home/subrat

ls

The ls command is one of the most frequently used command in Linux. It lists the directories and files present inside a directory.

- List the files & directories in bare format.
# ls

- Long listing of files & directories.
# ls -l
total 48
-rwxr-xr--. 1 root   root   15504 Apr  2 04:52 iostat.txt
-rw-rw-r-x. 1 subrat subrat 20480 Apr  4 03:45 mon.tar
drwxr-x--x. 2 subrat subrat  4096 Apr  4 07:22 oracle
drwxr-x--x. 2 root   root    4096 Apr  8 08:04 java
-rw-------. 1 root   root     592 Apr  2 04:51 sar.txt
lrwxrwxrwx. 1 root   root      25 Dec 23 21:31 al -> /etc/alternatives

The First character specifies the type of the file as specified below,

-  normal file
d  directory
s  socket file
l  link file

Then rest of the fields are described as below,

1st Field - File Permissions.Each 3 characters refers to the read, write, execute permissions for user, group and others.
2nd Field - Link Count. Number of directory present in a directory. For file the Link count is 1 & for directory , the link count is 2. For sub-directories, the link count is 1.
3rd Field - The owner of the file.
4th Field - The group of the file.
5th Field - Size of the file.
6th Field - Month of last modification.
7th Field - Date of last modification.
8th Field - Time of last modification.
9th Field - File Name.

- Listing all files including hidden files.
# ls -la

- Listing the files with their size.
# ls -ls

- Sort the files by their size.
# ls -lS

- Display files with inode number.
# ls -li

- List files with human readable format.
# ls -lh

- List files in reverse order.
# ls -lr

- List the files according to last modified Time.
# ls -lt

- Display File UID and GID.
# ls -n

- Visual Classification of Files With Special Characters.
# ls -F

Note for the following representations,

    / – directory.
    nothing – normal file.
    @ – link file.
    * – Executable file

- Display Files Recursively.
# ls -R

- List the contents of your home directory.
# ls ~

- List the contents of your root directory.
# ls /

- List the contents of the parent directory.
# ls ../

- List the contents of all sub-directories.
# ls */

- Display a list of directories in the current directory.
 # ls -d */

- Display the information of a specific file.
 # ls -l /path/to/file

- Display the information of a specific directory.
# ls -ld /path/to/dir

- Visual Classification of Files With Colors.
# ls --color=auto

ll

The ll command is the equivalent of 'ls -l'. This command is used to get detail information about files and directories in a directory.

# ll
total 140
-rw-------. 1 subrat subrat 73740 Apr  9 06:42 file1
-rw-------. 1 subrat subrat  3324 Apr  9 06:43 file2
-rw-------. 1 root   root   15504 Apr  2 04:52 iostat.txt
-rw-------. 1 subrat subrat 20480 Apr  4 03:45 mon.tar
-rw-------. 1 subrat subrat    56 Apr  9 05:47 my.pkt
-rw-------. 1 root   root     549 Apr 11 08:40 out.mp

Just like the different options of ls can be used for ll too. Try more options.

dir

The dir command is used to list all the directories and files present in a directory. It does the same operation like ls command. It does not support content highlighting. For more info visit man page for dir.

# dir
dir1 dir2 file.txt

mkdir

The mkdir command is used to create directory or directories.

- Creating Directory/Directories  in current working directory.
# mkdir Dir1
# mkdir Dir2 Dir3

- Creating Directory/Directories in a specified path.
# mkdir /home/subrat/Dir1
# mkdir /home/subrat/Dir2 /root/Dir3

- Creating nested directories.
# mkdir -p /opt/apps/oracle/data

The above command will first create opt, then apps, then oracle & finally data.

cd

- Change working directory from present working directory using relative path.
# cd subrat/

Note: Your relative path starts from your present working directory.

- Change working directory from present working directory to any location.
# cd /path/to/directory

Note: In this case you are starting from / .So, you are changing the directory using the absolute path.

- Change working directory to user's home directory.
# cd ~
# cd

- Change directory to previous working directory which we changed from.
# cd -

- Change working directory to parent directory.
# cd ..

- Changing working directory three levels up in the directory.
# cd ../../..

cp

The cp command is used to copy one or more files or directories from source to destination. The original file remains unchanged, and the new file may have the same or a different name.

Syntax: cp [option] source destination

Note: While copying make sure that your destination location should not contain the file or directory with the same name as your source file or directory. Else the new content will be overwritten.

- To make a copy of a file in the current directory as a new file.
# cp file.txt new-file.txt

- To copy a file from your current directory into another directory.

# cp file_name /path/of/new_file

- To copy a directory from one source to another.
# cp -r dir1 /path/of/new_dir

- To copy multiple files into a directory.
# cp file2 file3 file4 dir1

- To copy multiple directories.
# cp -r src/ bin/ /home/subrat/

- To explain what is being done while copying. i.e. verbose
# cp -v file1 /path/to/new_file

- To copy a file or directory and preserve the attributes like modification date, time, and access control list associated with the source file or directory.
# cp -p file_name /path/to/new/location/myfile
# cp -rp dir_name /path/to/new/location/mydir


- To copy all the files in a directory to a new directory.
# cp * /home/subrat/backup

- To copy a directory, including all its files and sub-directories, to another directory.
# cp -R dir1/* /home/subrat/backup

- To ask for Confirmation before overwriting (interactive mode)if a file exist with the same name in destination.
# cp -i test.c bak
cp: overwrite 'bak/test.c'? y

- To take a backup before copying into a destination.
# cp -b file.txt  /home/subrat/
# cd /home/subrat/
# ls -l
-rw-r--r-- 1 subrat unix 1020 Apr  8 13:36 file.txt
-rw-r--r-- 1 subrat unix 1038 Apr  8 13:15 file.txt~

- To copy a write-protected file forcefully.
# cp -f file.txt /path/to/file.txt

- To copy some files having a common pattern by using wild-cards as shown below. In this example, all txt extension files gets copied.
# cp dir1/*.txt /home/subrat/text/

mv

The mv command is used to move files and directories from one location to another.
Apart from moving the files, it can also rename a file or directory.

Syntax: mv oldname newname

- To rename a file.
# mv file file_new

- To move a file from source to destination.
# mv filename /path/to/destination

- To move a file from source to destination with a new name.
# mv filename /path/to/destination/new_filename

- To rename a directory.
# mv -r dir1 dir2

- To move a directory from source to destination.
# mv -r dir1 /path/to/destination

- To move a directory from source to destination with a new name.
# mv -r dir_old /path/to/destination/dir_new

- To take a backup of destination before Overwriting.
# mv --suffix=.bak file_name /path/to/destination/

The above command creates another file with .bak which holds the old data.

- To move a write-protected file forcefully.
# mv -f file /path/to/destination

- To move a file interactively.
# mv -i sample.txt /backup

It will Prompt for a Confirmation Before Overwriting.
# mv -i sample.txt sample1.txt
mv: overwrite 'sample1.txt'? y

- To move some files having a common pattern by using wild-cards as shown below.
# mv *.txt /home/subrat/

rmdir

The rmdir command is used to delete an empty directory or set of empty directories.

- To delete an empty directory.
# rmdir dir_empty
# rmdir /path/to/empty_dir

- To delete mutltiple empty directories.
# rmdir dir1 dir2  dir3

- To delete nested empty directories.
# rmdir -p dir1/dir2/dir3

rm

The rm command is used to remove files or directories. Remove your files wisely! The effects of an rm cannot be undone.

- To remove file/files.
# rm file1
# rm file1 file2
# rm /path/to/file/

- To remove directory/directories.
# rm -r dir

- To remove a write-protected file or directory forcefully.
# rm -f file

- To prompt the user for confirmation before removing each file and directory.
# rm -i file

- To remove some files having a common pattern by using wild-cards as shown below.
# rm -f *.txt