Sunday, 22 March 2015

How to add user in linux

To add user, you can use useradd command in linux. Refer below syntax:

useradd [options] <user-name>

Example: useradd anil

While creating users as mentioned above, all the default options will be taken except group id. To view the default options give the following command with the option -D.

$ useradd -D
GROUP=1001
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
GROUPThis is the only option which will not be taken as default. Because if you don’t specify -n option a group with same name as the user will be created and the user will be added to that group. To avoid that and to make the user as the member of the default group you need to give the option -n.
HOMEThis is the default path prefix for the home directory. Now the home directory will be created as /home/USERNAME.
INACTIVE-1 by default disables the feature of disabling the account once the user password has expired. To change this behavior you need to give a positive number which means if the password gets expired after the given number of days the user account will be disabled.
EXPIREThe date on which the user account will be disabled.
SHELLUsers login shell.
SKELContents of the skel directory will be copied to the users home directory.
CREATE_MAIL_SPOOLAccording to the value creates or does not create the mail spool.

To create user with custom configurations using useradd Command:
The below example creates an account "anil" with home directory /home/linuxbasics, default shell as /bin/ksh and with comment “Linuxinanutshell admin”.

$ useradd -s /bin/ksh -m -d /home/linuxbasics -c “Linuxinanutshell admin” -g root anil

$ grep anil /etc/passwd
anil:x:1083:0:Linuxinanutshell admin:/home/linuxbasics:/bin/ksh

To change default options in configuration file:
As we mentioned earlier to change the default values during command execution, there is another option to change default values for account creation, which is defined in /etc/default/useradd file under most of the Linux distributions. Open this file in a text editor, ie.:
$ vi /etc/default/useradd

The default home directory defined by HOME variable, find line that read as follows:
HOME=/home

Replace with:
HOME=/linuxbasics/user

Save and close the file. Now you can add user using regular useradd command:
$ useradd anil


To change existing User's Home Directory:
You need to use the usermod command to set the user's new login directory.

Syntax: usermod -m -d /path-to-new-home-dir userName

-d dirnanme : Path to new login (home) directory.
-m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

Example: usermod -m -d /users/linuxinanutshell/anil anil
In above example set the user's new login directory to /users/linuxinanutshell/anil from /home/anil

To set Password for user:
$ passwd anil
Changing password for user anil.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

[Note: Always set the password immediately after user creation]

Saturday, 7 March 2015

Kill or terminate process in Linux

Kill command is used to kill a process in Linux.The kill command sends the specified signal such as kill process to the specified process. If no signal is specified, the SIGTERM signal is sent.

Syntax:

kill [signal] PID

A list of common signals:
SIGHUP-(1) Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files
SIGKILL-(9) Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process
SIGTERM-(15) Termination signal. This is the default and safest way to kill process

Example:
In example, we are terminating process java with -15 signal, which will terminate the process safely.

Points to remember:

  • You can kill all your own process.
  • Only root user can kill system level processes.
  • Only root user can kill process started by other users.

To kill multiple process in single command, mention all PID's in command:
kill -15 PID1 PID2 PID3


To kill process with its name:
You can kill any process by specifying its full name or partial name. So there is no need for you to find out the PID of the process to send the signal.
pkill java
Further, you can also kill process with its name using killall command but with killall, you need to specify full name of process.


Note: There are many kill signals that each serve a particular purpose. Typing "kill -l" will list the kill signals. 



Sunday, 1 March 2015

Find difference between 02 files in linux

To compare two text files in Linux, use the diff command. In nutshell, diff compares the contents of the two files from-file and to-file.
Syntax: diff [options] from-file to-file2
Following options can be used with diff command:
-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank lines.
-i Ignore changes in case; consider upper- and lower-case letters equivalent.
--new-file In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
-r When comparing directories, recursively compare any subdirectories found.
Example:
Suppose you have a file 'file1.txt': 
and another file 'file2.txt':
Now, if you execute the diff command 'diff file1.txt file2.txt', then it will display the output like this:
Lines "1d0" and "6a6" are the coordinates and types of the differences between the two compared files, while lines like "< aa" and "> kk" are the differences themselves. Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:

a – line added

c – line changed

d – line deleted

Left number of the character defines the line number in the first file, and Right number of the character defines the line number in the second file.
Output details:
1d0
< aa
It means that 1 line was deleted. < aa denotes that the aa line is present only in the first file.
3c2
< cc
---
> kk
It means that the line#3 has changed from "cc"[in first file] to "kk"[in second file].
6a6
> gg
It means that one new line added in the second file, it's "gg" at line#7.


Friday, 20 February 2015

how to get file/directory stat or information

The stat command is used to display detailed status of file, directory and file system.
Syntax: stat [options] FILE...

Example:
$ stat tmp/file1
Output:
File: tmp/file1
Size: 0 Blocks: 0  IO Block: 4096
Device: 12h/18d Inode: 3064192624  Links: 1
Access: (0660/-rw-rw----)  Uid: (    0/ UNKNOWN)   Gid: ( 1028/ UNKNOWN)
Access: 2015-02-28 15:39:03.000000000
Modify: 2015-02-28 15:39:03.000000000
Change: 2015-02-28 15:39:03.000000000

So, without options/arguments you can have the following details:

File
Size in Bytes
Blocks
Number of blocks used
IO Block
Size in bytes of every block
Device
The identifier number of your storage device (harddrive, etc.)
Inode
The inode number that the file or directory is linked to
Access/Modify and change Times
Note that the timestamps also include which time zone that accesses or modifications took place in

Arguments:
The following are some of the flags and arguments that can be used for the stat command:


Arguments
Details
-f  --filesystem
display filesystem status instead of file status
-c   --format
use the specified FORMAT instead of the default
-L  --dereference
follow links
-Z   --context
print the security context
--help
display the help and exit

Sunday, 1 February 2015

How to use find command in linux

Intro
Find utility searches for files in a directory. Basically files can be found under Linux in many different ways. Using the find utility is one of the best ways to find files. The find utility has a huge number of parameters which can be set so that Linux finds exactly those files that you were searching for.

Syntax
find  where_to_look  criteria  which_file_want_to_search

Example
$ find / -name report 2>/dev/null

/ Start searching from the root directory
-name Given search text is the filename rather than any other attribute of a file
report File name for which we are looking

Note
2> /dev/null is not related to find utility. Here, 2 indicates the error stream in Linux, and /dev/null is the device where anything you send simply disappears i.e. Above example discard all error messages.