Saturday, 29 April 2017

free command usage and example

free 
It displays information about free and used memory on the system.

The total shows you the total memory assigned to machine but it excludes a small amount of RAM which is reserved for kernel for booting purpose

The first line, labeled Mem, displays physical memory utilization, including the amount of memory allocated to buffers and caches. 

A buffer memory, is a portion of memory that is set aside as a temporary holding place for data. Cache is a memory location to store frequently used data for faster access. Other difference between a buffer and a cache is that cache can be used multiple times where as buffer is used single time. 

Second line shows total buffers/Cache used and free.

Third line shows total swap memory available, used swap and free swap memory size available.

We can find more detailed information about total memory and current memory usage from the proc/meminfo file.

Syntax
free [options]

When used with no option it shows output in kilobytes.

Options
-b Will displays output in bytes
-k Will displays output in kilobytes
-m Will displays output in megabytes
-g Will displays output in gigabytes
-s num Will update free command every num seconds
-l Will displays detailed lowhigh memory statistics
-V Shows version

Example


watch
The alternative is to use free with its -s option using the watch command, which by default runs a program provided to it as an argument every two seconds after first temporarily clearing the screen.

watch free

watch -n 1 -d free

The -d (difference) option highlights changes in output, and its -n option followed by the number one to increase the frequency to one per second.

last and lastb command usage and examples

last
The last command is used to show who has recently used the server and logged in and out. The last command reads listing of last logged in users from the system file called /var/log/wtmp.

Syntax
last [options]
If no options provided last command displays a list of all users logged in/out since /var/log/wtmp file was created.

 Options

user_name Shows specific user last logged in.
tty last will show only those entries matching tty. Names of ttys can be abbreviated, thus last 1 is the same as last tty1.
-t YYYYMMDDHHMMSS Will show state of logins as of the specified time.
-f file Will search specific file other than /var/log/wtmp
-F Will show full login and logout times and dates.
-R Will remove display of the hostname field.
-num Show num lines in the output of last command
-n num
-a Show the hostname in the last column.
-d Will translates the IP number back into a hostname
-i Similar to option –d but it displays the IP number in numbers-and-dots notation
-w Show full user and domain names in the output.
-x Show last shutdown entries and run level changes.

Example

The below example shows recently logged in and out users on your server

last

root pts/0 171.1.6.34 Tue Apr 28 05:59 still logged in

root pts/1 171.1.6.34 Tue Apr 28 04:08 still logged in

root pts/1 171.1.6.34 Sat Apr 25 06:33 - 08:55 (02:22)

root pts/0 171.1.6.34 Thu Apr 23 17:47 - 18:51 (01:03)

root pts/1 171.1.6.34 Thu Apr 23 14:02 - 14:51 (00:48)

root pts/0 171.1.6.34 Tue Apr 7 08:02 - 08:38 (00:35)

lastb
lastb is the same as last, except that by default it shows a log of the file /var/log/btmp, which contains all the bad login attempts.

Syntax
lastb [options]

If no options provided last command displays a list of all users logged in/out since /var/log/btmp file was created.

Wednesday, 26 April 2017

top command usage

top
top provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. Can be better configured than the standard top from the procps suite. Procps is the package that has a bunch of small useful utilities that give information about processes using the /proc filesystem. The package includes the programs ps, top, vmstat, w, kill, free, slabtop, and skill. Most features can either be selected by an interactive command or by specifying the feature in the personal or system-wide configuration file.

Syntax
top [options]

Options

-a 
Sort by memory usage

-b 
Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.

-c 
Starts top with the last remembered 'c' state reversed. Thus, if top was displaying command lines, now that field will show program names, and vice versa.

-d 
Delay time interval as: -d ss.tt (seconds. tenths) Specifies the delay between screens updates, and overrides the corresponding value in one's personal configuration file or the startup default. Later this can be changed with the ’d’ or 's' interactive commands. Fractional seconds are honored, but a negative number is not allowed. In all cases, however, such changes are prohibited if top is running in 'Secure mode', except for root (unless the ’s’ command-line option was used).

-h 
Show Library version and the usage prompt, then quit.

-H 
Starts top with the last remembered 'H' state reversed. When this toggle is On, all individual threads will be displayed. Otherwise, top displays a summation of all threads in a process.

-i 
Starts top with the last remembered 'i' state reversed. When this toggle is Off, tasks that are idled or zombie will not be displayed.

-m 
Reports USED (sum of process and swap total count) instead of VIRT

-M 
Show memory units (k/M/G) and display floating point values in the memory summary.

-n 
Specifies the maximum number of iterations, or frames, top should produce before ending.

-p 
Monitor only processes with specified process IDs. This option can be given up to 20 times, or you can provide a comma delimited list with up to 20 process IDs. Co-mingling both approaches is permitted. This is a command-line option only. And should you wish to return to normal operation, it is not necessary to quit and restart top -- just issue the '=' interactive command.

-s 
Starts top with secure mode forced, even for root. This mode is far better controlled through the system configuration file (see topic 5. FILES).

-S 
Starts top with the last remembered 'S' state reversed. When 'Cumulative mode' is On, each process is listed with the CPU time that it and its dead children have used. See the 'S' interactive command for additional information regarding this mode.

-u Monitor only processes with an effective UID or user name matching that given.

-U 
Monitor only processes with a UID or user name matching that given. This matches real, effective, saved, and file system UIDs.

-v 
Show library version and the usage prompt, then quit.

Output 
PID The process id.
USERNAME

Username of the process's owner (if -u is specified, a UID column will be substituted for USERNAME).
THR

The number of threads in the processes also labeled as NLWP.
PR
Current priority of the process.
NICE

Nice amount in the range -20 to 20, as established by the use of the command nice.
SIZE

Total size of the process (text, data, and stack) given in kilobytes.
VIRT Virtual memory used by process,  
RES

Resident memory: current amount of process memory that resides in physical memory, given in kilobytes.
SHR Shows the shareable memory
CPU
Percentage of available cpu time used by this process.
MEM Memory used by process as a percentage 
TIME
Number of system and user cpu seconds that the process has used.
COMMAND
Name of the command that the process is currently running.

Example
top
top - 20:50:55 up 167 days, 14:26, 82 users, load average: 0.13, 0.05, 0.01
Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0% user, 2.2% system, 0.0% nice, 97.8% idle
Mem: 514964k total, 507860k used, 7104k free, 30404k buffers
Swap: 1630588k total, 158708k used, 1471880k free, 275380k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3747
hope 11 0 2004 1608 1608 S 0.3 0.3 0:00.56 sshd 22245
hope 10 0 688 676 672 S 0.0 0.1 0:00.06 csh 14015
hope 10 0 1024 1024 848 R 0.0 0.2 0:00.00 top

Startup Defaults
The following startup defaults assume no configuration file, thus no user customizations. Even so, items shown with an asterisk ('*') could be overridden through the command-line.Startup Defaults

 Some more options in running top command

O To Sort field via field letter.
z Will display running process in color.
x Will highlight the values in the sort column with bold text.
b Will highlight with a different background color.
c Will display absolute path of running process.
1 Will display the load information about individual CPU cores.
V Will display the processes in a parent child hierarchy.
r Will change the priority of the process also called as renice.
R Will reverse the sorting order of the currently sorted column.
k We can kill a process after finding PID of process by pressing ‘k‘ option in running top command without exiting from top window.
P Will sort processes as per CPU utilization.
W Will save the running top command results under /root/.toprc
ctrl + z Will pause top so that we can continue using the terminal. To bring top back to the foreground, type fg.
f By default top displays only few columns out of many more that it can display. If you want to add or remove a particular column or change the order of columns.


Monday, 24 April 2017

groupadd command usage and examples

groupadd
As the name suggest this command is used to add groups to the system. groupadd command is used to create new group accounts. It updates the /etc/group file accordingly.

Syntax
groupadd [options] group_name

Options
-f --force This option causes the command to simply exit with success status if the specified group already exists. When used with -g, and the specified GID already exists, another (unique) GID is chosen (i.e. -g is turned off).
-g GID --gid GID Specify the number value which you want for group id. This is a unique value, unless the -o option is used. The value is non-negative. The default is to use the smallest ID value greater than 999 and greater than every other group. IDs between 0 and 999 are reserved for system accounts.
-K KEY=VALUE --key KEY=VALUE Overrides /etc/login.defs defaults (GID_MIN, GID_MAX and others). Multiple -K options can be specified.
-o --non-unique This option permits to add a group with a non-unique GID.
-p PASSWORD --password PASSWORD The encrypted password, as returned by crypt. By default password is disabled. This option is not recommended because the password (or encrypted password) will be visible by users listing the processes. You should make sure the password respects the systems password policy.
-r --system Create a system group. The numeric identifiers of new system groups are chosen in the SYS_GID_MIN-SYS_GID_MAX range, defined in login.defs, instead of GID_MIN-GID_MAX.

Creating a group using groupname
The groupadd command followed by groupname will create new group.

Example
groupadd superheroes

The above example will create a group called superheroes.
We can verify whether it is created successfully or not by checking the /etc/group file.

grep superheroes /etc/group
superheroes:x:1007:

Creating group with a specific groupid
If we don’t specify a groupid while using groupadd command, system will assign one automatically.

Example
groupadd superheroes -g 9197

grep superheroes /etc/group
superheroes:x: 9197:

The above example will create a group named superheroes with a group id 9197.

Overriding  /etc/login.defs default file 
When system assigns the automatic group id, it uses the GID_MIN, and GID_MAX value specified in the /etc/login.defs files.

egrep 'GID_MIN|GID_MAX' /etc/login.defs

GID_MIN 1000

GID_MAX 60000

If we want to set within specific values, we need to use -K option. 

Example
groupadd superheroes -K GID_MIN=6666 -K GID_MAX=7777

grep superheroes /etc/group
superheroes:x: 6197:

The above example will create the account with group id 6197, which is between the values 6666 to 7777 that we specified in the command.

renice command usage and examples

renice

Changing the priority of an already running process can be performed by renice command. renice command is very much similar to the nice command in terms of priority , however it does differ in terms of arguments. You can also give username, group name in renice command as a parameter. But the process can be set with priority range only from -20 to 19. A process with the nice value of -20 is considered to be on top of the priority. And a process with nice value of 19 is considered to be low on the priority list. The default niceness for processes is inherited from its parent process and is usually 0. The process priority can be seen by running one of the following commands

ps –al
ps -o pid,comm,nice -p actual_pid
top

The column that starts with "NI" shows the nice value i.e. the priority of the process. Normal users can only decrease the process priority. However root user can increase/decrease all process's priority. 

Syntax
renice [-n] priority [-p pid] [-g grp_name] [-u user_name]

Some of the examples are shown below:

Example
renice -4 -p 7853

This changes the nice value of the process id 7853 to -4, which will in turn increase its priority over other processes.

Example
renice 11 -p 7853

The above command will set the priority of the process id 7853 to 11.
In this case there is no dash (minus) for command option when specifying the nice value.

Example
renice 15 -p 7853 -u flash

The above command will set the priority of the process id 7853 to 15, and all the process owned by user “flash“to the priority of 15.

Example
renice 14 -g superhero

This changes the nice value of the all process owned by the group " superhero " to 14.

Example
renice 16 -u flash,batman

This will set all process owned by " flash "," batman" to 16.

nice command usage and examples

nice
Linux Kernel schedules the process and allocates CPU time accordingly for each of the process. Every running process has a priority assigned to it. When a process is started, by default it gets launched with the priority of '0'. We can change the process priority either nice or renice commands. We can only use nice command to change the process priority when the process is started. But we cannot use nice command to change the priority of an already running process.
Syntax
nice [option] [command [arg]...]
Option
-n, --adjustment=N add integer N to the niceness

By using nice command we can start a new process with required priority.But the process can be set with priority range only from -20 to 19. A process with the nice value of -20 is considered to be on top of the priority. And a process with nice value of 19 is considered to be low on the priority list. The default niceness for processes is inherited from its parent process and is usually 0.If we give a process a higher priority, then Kernel will allocate more cpu time to that process. Normal users can only decrease the process priority. However root user can increase/decrease all process's priority. Running nice command with no options will give the priority with which the current shell is running.
The process priority can be seen by running one of the following commands:

ps –al

ps -o pid,comm,nice -p actual_pid

top

The column that starts with "NI" shows the nice value i.e. the priority of the process.

Setting less priority to a process
Any greater number will have highest less priority than the preceding numbers.

Example
nice -n 19 tar cvzf archive.tgz largefile

Setting high priority to a process
Any smaller number will have highest high priority than the following numbers.

Example
nice -n -20 tar cvzf archive.tgz largefile

nice -20 tar cvzf archive.tgz largefile

nice --20 and nice -n 20 will do the same thing. Both the above commands will make the process priority to the value 20.

Setting permanent priority user specific on all processes
We can set the default nice value of a particular user or group in the /etc/security/limits.conf file.

Syntax
[username] [hard|soft] priority [nice value]

Sunday, 23 April 2017

unalias command usage and example

unalias

The unalias command purpose is to remove entries from the current user's list of aliases. Unalias can simultaneously accept any number of alias names as arguments. Also we can remove an alias by using the alias command to create a new alias with the same name. This overwrites the existing alias with that name. If you want to remove an alias permanently, you have to remove it from your bash file.

Syntax

unalias  [alias_name]

Option
-a
There is only one option which removes all aliases for the current user for the current shell for the current session, but it does not affect permanent aliases for future sessions.

Example
If there is an alias named c for the clear command, such alias could be removed with the following
unalias c
Now if you try to press c it won’t provide you the output of clear command.

alias command usage and example

alias
Aliases can be created by using the alias command.
The main purpose of alias is reducing the amount of typing that is necessary for commands or groups of commands that are long or tedious to typeIf you want to see the entire alias for the session you just need to type alias.

Syntax
alias [alias_name]="command"

It makes no difference whether double or single quotes are used when creating an alias.

Example
If we want to clear the screen by clear command then alias for it can be set by below alias command.

alias c="clear"

So just by pressing c will serve the purpose of clear command.

Creating multiple aliases simultaneously
We can create multiple aliases just by separating the two alias to be made by semicolon.

Example
alias p="pwd"; l="ls -al"

Multiple commands in one alias
Multiple commands can also be included in the same alias by inserting them within the quotation and separating them with semicolons.

Example
The alias cl could be created to first launch clear command and then immediately launch ls command.

alias cl="pwd; ls"

Calling alias within alias
Aliases can be created to call other aliases.

Example
The below example will only work provided there are two aliases c and l already present.

alias cl="c; l"

Example
If we have an alias c for clear previously defined but we don’t have alias for ls command and if we try cl then it will first perform clear command followed by the conventional ls command.

alias cl="c; ls"

Monday, 17 April 2017

All about the VI editor

The default editor that comes with the UNIX operating system is called vi (visual editor). It has two modes of operation:

1. Command mode commands which cause action to be taken on the file.
In the command mode, every single character typed is a command that performs operation on the text file being edited.

2. Insert mode in which entered text is inserted into the file.
In the insert mode, every character typed is added to the text in the file; pressing the Escape key turns off the Insert mode.

Syntax
vi file_name
It will open an empty file if not present already.

Commands that switch the editor to insert mode
a
It moves the cursor one position to the right before switching to insert mode, basically it will append
i
Insert text at current cursor position.
I
Insert text at the beginning of the current line.
o
Insert a blank line under the current cursor position and move the cursor to that line.
O
Insert a blank line above the current cursor position and move the cursor to that line.

Basic operations
Some of the popular vi commands are:

Moving through the text
h move the cursor to the left
l move the cursor to the right
k move the cursor up
j
move the cursor down
H
moves the cursor to the top line of the screen
M
moves the cursor to the middle line of the screen
L
moves the cursor to the last line of the screen
w
move the cursor forward one word
b
move the cursor backward one word (if you are in the middle of a word, b command will move you to the beginning of the current word)
e
move to the end of a word
(
move to beginning of current block
)
move to beginning of current block
0
move to the beginning of a line
$
move to the end of a line

Screen Movement
Ctrl-f
scrolls down one screen
Ctrl-b
scrolls up one screen
Ctrl-u
scrolls up half a screen
Ctrl-d
scrolls down a half a screen
SHIFT-G
put the prompt at the end of the file, Preceding G with a number will move you to a specific line in the file
1G
move to the beginning of the file

Delete
dw
deletes from the character selected to the end of the word
ndw
deletes n words at the right side of the cursor
dd
deletes the current line
ndd
deletes n lines starting from the current cursor position
D
deletes from the current character to the end of the line
x
deletes the character on which the cursor is positioned
nx
delete the character selected and the next (n-1) characters
X
deletes the character on left of your cursor
nX
deletes n character on left of your cursor
$-9,$d
deletes last ten lines

Copy, Paste, Replacement | Join and Undo
yw
copies a word into a buffer
nyw
copies n word into a buffer
yy
copies a line into a buffer
nyy
copies n line into a buffer
p
paste line or words copied earlier on the line below the cursor
p
paste line or words copied earlier on the line above the cursor
r
replaces the current character with the next character you type and returns to command mode
cw
Changes and replaces the current word with text that you type. Press ESC to get out of replacement mode
:1,$s/word/newword/g
word with newword throughout the file

J join the line on which cursor is placed with another line
nJ join n lines
:u undoes the last change you made in the file. Using it again will "undo the undo"
U undoes all recent changes to the current line


Display line number
:set number shows the line numbers
:n moves to line n of the file
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen
^g provides the current line number, along with the total number of lines, in the file at the bottom of the screen

Search
/pattern search the string(pattern) in the file and position the cursor on the first match below its position
n search in a forward direction
N search in a backwards direction
?pattern search backward for occurrence of the pattern

Save and Quit
:w save the file
:q exit the editor
:q! forces the exit when you want to quit a file containing unsaved changes
:e! reads the original file back in so that you can start over
:wq save and exit
ZZ save file and exit VI
:w newfile
save the text to newfile
:wq! overrides read-only permission
:1,100w newfile write lines 1 to 100 to newfile
:$-9,$w newfile write the last ten lines to newfile (the dollar sign denotes last line)


Some more commands
:recover recover a file after an unexpected interruption
vi –r filename
R puts you in overtype mode until you press ESC
. Repeats the last command executed
:! command
execute external command from VI
:sh start a shell from VI, Return to VI by entering exit or ctrl d
:sp horizontally split current file
:vsp
vertically split current file
:r! command insert output of command to the file