Friday 27 June 2014

Short tutorial on "Kill" command (Originally by DevynCJohnson)

Kill command:-

"kill" command is one of the most useful commands to kill a certain process in linux system.Here is a short tutorial about its usage---

On Linux systems, numerous users often come across a program or process that locks up. The user will usually kill the software if the system does not do it first. Users may be familiar with some of the kill commands and signals, but does anyone understand all of them? There are four common kill commands and a total of 64 kill signals.

kill - The kill command will kill a process using the kill signal and PID given by the user. To use the SIGKILL signal with "kill", type one of the following for a process with a PID of 0710.

kill -9 0710
kill -SIGKILL 0710

The kill command accepts either the signal number or name (signals have both a number and name that can be referenced). The name must be in all caps.


killall - The killall command kills all process with a particular name. For instance, Nautilus may be running several times. To kill all of them type "killall -SIGQUIT nautilus". Also, if Firefox is running once and the user does not know the PID, use the killall command - "killall -9 firefox". The killall command also uses case-sensitive kill signals (they are all uppercase). Below demonstrates what will happen when the kill signal is typed in lowercase. Notice that the command uses the "s" as a parameter and then it does not know what to do with the rest of the information. It then tries to use "igkill" as a kill signal, but no such signal exists.

collier@Nacho-Laptop:~$ killall -sigkill nautilus 
igkill: unknown signal; killall -l lists signals.


pkill - This command is a lot like killall except it allows partial names. So, "pkill -9 unity" will kill any process whose name begins with "unity".


xkill - This command allows users to kill a command by clicking the window. In a terminal, type "xkill" and then the cursor will change. Next, click a window to kill. The application should disappear and leave the memory. Then, the cursor will return to normal.


There are many kill signals that each serve a particular purpose. Typing "kill -l" will list the kill signals. Notice that all kill signals begin with "SIG"; this means SIGnal.

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     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

NOTE: Yes, certain numbers are missing because those signals are not supported on my system, or they were discontinued. If you run the same command, you may have different numbers missing/available.

TIP: Everyone has signals 1, 3, 9, and 15. Those are the most common signals. It is important to learn those very well when administering Linux systems.

Kill signals are not only used to close locked-up applications, but also stop software from performing unallowed tasks. This means some of these kill signals are part of security. Surprisingly, kill commands not only stop/kill processes but they also pause, continue, and restart processes.


SIGHUP - The SIGHUP signal disconnects a process from the parent process. This an also be used to restart processes. For example, "killall -SIGUP compiz" will restart Compiz. This is useful for daemons with memory leaks. 

SIGINT - This signal is the same as pressing ctrl-c. On some systems, "delete" + "break" sends the same signal to the process. The process is interrupted and stopped. However, the process can ignore this signal.

SIGQUIT - This is like SIGINT with the ability to make the process produce a core dump.

SIGILL - When a process performs a faulty, forbidden, or unknown function, the system sends the SIGILL signal to the process. This is the ILLegal SIGnal.

SIGTRAP - This signal is used for debugging purposes. When a process has performed an action or a condition is met that a debugger is waiting for, this signal will be sent to the process.

SIGABRT - This kill signal is the abort signal. Typically, a process will initiate this kill signal on itself.

SIGBUS - When a process is sent the SIGBUS signal, it is because the process caused a bus error. Commonly, these bus errors are due to a process trying to use fake physical addresses or the process has its memory alignment set incorrectly.

SIGFPE - Processes that divide by zero are killed using SIGFPE. Imagine if humans got the death penalty for such math. NOTE: The author of this article was recently drug out to the street and shot for dividing by zero.

SIGKILL - The SIGKILL signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.

SIGUSR1 - This indicates a user-defined condition. This signal can be set by the user by programming the commands in sigusr1.c. This requires the programmer to know C/C++.

SIGSEGV - When an application has a segmentation violation, this signal is sent to the process.

SIGUSR2 - This indicates a user-defined condition.

SIGPIPE - When a process tries to write to a pipe that lacks an end connected to a reader, this signal is sent to the process. A reader is a process that reads data at the end of a pipe.

SIGALRM - SIGALRM is sent when the real time or clock time timer expires.

SIGTERM - This signal requests a process to stop running. This signal can be ignored. The process is given time to gracefully shutdown. When a program gracefully shuts down, that means it is given time to save its progress and release resources. In other words, it is not forced to stop. SIGINT is very similar to SIGTERM.

SIGCHLD - When a parent process loses its child process, the parent process is sent the SIGCHLD signal.  This cleans up resources used by the child process. In computers, a child process is a process started by another process know as a parent.

SIGCONT - To make processes continue executing after being paused by the SIGTSTP or SIGSTOP signal, send the SIGCONT signal to the paused process. This is the CONTinue SIGnal. This signal is beneficial to Unix job control (executing background tasks).

SIGSTOP - This signal makes the operating system pause a process's execution. The process cannot ignore the signal.

SIGTSTP - This signal is like pressing ctrl-z. This makes a request to the terminal containing the process to ask the process to stop temporarily. The process can ignore the request.

SIGTTIN - When a process attempts to read from a tty (computer terminal), the process receives this signal.

SIGTTOU - When a process attempts to write from a tty (computer terminal), the process receives this signal.

SIGURG - When a process has urgent data to be read or the data is very large, the SIGURG signal is sent to the process.

SIGXCPU - When a process uses the CPU past the allotted time, the system sends the process this signal. SIGXCPU acts like a warning; the process has time to save the progress (if possible) and close before the system kills the process with SIGKILL.

SIGXFSZ - Filesystems have a limit to how large a file can be made. When a program tries to violate this limit, the system will send that process the SIGXFSZ signal.

SIGVTALRM - SIGVTALRM is sent when CPU time used by the process elapses.

SIGPROF - SIGPROF is sent when CPU time used by the process and by the system on behalf of the process elapses.

SIGWINCH - When a process is in a terminal that changes its size, the process receives this signal.

SIGIO - Alias to SIGPOLL or at least behaves much like SIGPOLL.

SIGPWR - Power failures will cause the system to send this signal to processes (if the system is still on).

SIGSYS - Processes that give a system call an invalid parameter will receive this signal.

SIGRTMIN* - This is a set of signals that varies between systems. They are labeled SIGRTMIN+1, SIGRTMIN+2, SIGRTMIN+3, ......., and so on (usually up to 15). These are user-defined signals; they must be programmed in the Linux kernel's source code. That would require the user to know C/C++.

SIGRTMAX* - This is a set of signals that varies between systems. They are labeled SIGRTMAX-1, SIGRTMAX-2, SIGRTMAX-3, ......., and so on (usually up to 14). These are user-defined signals; they must be programmed in the Linux kernel's source code. That would require the user to know C/C++.

SIGEMT - Processes receive this signal when an emulator trap occurs.

SIGINFO - Terminals may sometimes send status requests to processes. When this happens, processes will also receive this signal.

SIGLOST - Processes trying to access locked files will get this signal.

SIGPOLL - When a process causes an asynchronous I/O event, that process is sent the SIGPOLL signal.


Users can use these kill signals using one of the four kill commands. When sending a signal to a process owned by another user (like root), the user needs admin privileges and must use the sudo command. Be careful though, misuse of these signals can cause system damage. For instance, using SIGTERM on a GUI process like Compiz, X11, XFCE, Unity, Gnome-shell, etc. will make the system unviewable.

Thank you!

Sunday 15 June 2014

Installing various applications in Ubuntu [via terminal]


There are various application software which a user sometimes need urgently.They can be installed via terminal in any version of ubuntu. Here are some of them-

VLC media player:-

Open the terminal by ctrl+alt+T and type the following commands
sudo add-apt-repository ppa:videolan/stable-daily
sudo apt-get update
sudo apt-get install vlc
and Done!

Flashplayer:- 

It is very easy,open the terminal and type the following command
sudo apt-get install flashplugin-installer
and then restart your browser to see the effect.

Adobe Reader:-

Though there is a 'Document Viwer' built in every Ubuntu distro to rad pdf files but nothing equals adobe reader.To install  it type the following commands in the terminal
sudo add-apt-repository "deb http://archive.canonical.com/[distro]partner"
sudo apt-get update
sudo apt-get install acroread
and Done!

LAMP server:-

This is very useful and I think necessary for web developer in Linux.To install it via terminal follow the following processes-
sudo apt-get install tasksel
sudo tasksel install lamp-server 
Now a window will come with various questions and mysql username and password.To begin with,give username 'root'.
Done!
Check the stuffs are correctly installed or not from the following steps.
Enter http://localhost in your browser and if the installation was correct then it will show
"It Works!" and other stuffs.
Now write a php file,namely test.php and type the code
<?php phpinfo() ?>
Save it in /var/www folder
Now type the url http://localhost/test.php and you will see a list of stuffs there.
For details see http://in1.php.net/manual/en/install.php

Thank you! more stuffs will be updated soon.

Saturday 14 June 2014

Changing root password in various version of ubuntu [12.04 and above]

There are various ways to change ubuntu 'root' password.But the procedure varies according to the version.
On ubuntu 12.04 it is easy to change the root password.It can be done via terminal.Open it by pressing ctrl+Alt+T.Then type
sudo  -i
passwd
There will appear "Enter New Unix Password:",give it there and done!

But in above versions of ubuntu,i.e.,12.10 and above, sometimes this process becomes useless.It'll give output
passwd:permission denied 
password remains unchanged
To overcome this follow the following process:
1.Select 'recovery mode' from the grub menu
2.Select the option 'root',then a terminal will come in front of you
3.Then type 'mount -o remount,rw /'
4.Then type 'passwd' and give the new password. [or passwd username]
5.Then reboot
Done!
Apply it,for any problem report here.For details follow this link https://help.ubuntu.com/community/LostPassword

Ubuntu cheat sheet

Privileges
sudo command – run command as root
sudo -s – open a root shell
sudo -s -u user – open a shell as user
sudo -k – forget sudo passwords
gksudo command – visual sudo dialog (GNOME)
kdesudo command – visual sudo dialog (KDE)
sudo visudo – edit /etc/sudoers
gksudo nautilus – root file manager (GNOME)
kdesudo konqueror – root file manager (KDE)
passwd – change your password

Display
sudo /etc/init.d/gdm restart – restart X and
return to login (GNOME)
sudo /etc/init.d/kdm restart – restart X and
return to login (KDE)
(file) /etc/X11/xorg.conf – display
configuration
sudo dexconf – reset xorg.conf configuration
Ctrl+Alt+Bksp – restart X display if frozen
Ctrl+Alt+FN – switch to tty N
Ctrl+Alt+F7 – switch back to X display

System Services
start service – start job service (Upstart)
stop service – stop job service (Upstart)
status service – check if service is running(Upstart)
/etc/init.d/service start – start service(SysV)
/etc/init.d/service stop – stop service (SysV)
/etc/init.d/service status – check service(SysV)
/etc/init.d/service restart – restart service(SysV)
runlevel – get current runlevel

Package Management
apt-get update – refresh available updates
apt-get upgrade – upgrade all packages
apt-get dist-upgrade – upgrade with package
replacements; upgrade Ubuntu version
apt-get install pkg – install pkg
apt-get purge pkg – uninstall pkg
apt-get autoremove – remove obsolete packages
apt-get -f install – try to fix broken packages
dpkg --configure -a – try to fix broken
packages
dpkg -i pkg.deb – install file pkg.deb
(file) /etc/apt/sources.list – APT repository list

Network
ifconfig – show network information
iwconfig – show wireless information
sudo iwlist scan – scan for wireless networks
sudo /etc/init.d/networking restart – reset
network for manual configurations
(file) /etc/network/interfaces – manual
configuration
ifup interface – bring interface online
ifdown interface – disable interface

Special Packages
ubuntu-desktop – standard Ubuntu environment
kubuntu-desktop – KDE desktop
xubuntu-desktop – XFCE desktop
ubuntu-minimal – core Ubuntu utilities
ubuntu-standard – standard Ubuntu utilities
ubuntu-restricted-extras – non-free, but useful
kubuntu-restricted-extras – KDE of the above
xubuntu-restricted-extras – XFCE of the above
build-essential – packages used to compile
programs
linux-image-generic – latest generic kernel
image
linux-headers-generic – latest build headers

Firewall
ufw enable – turn on the firewall
ufw disable – turn off the firewall
ufw default allow – allow all connections by
default
ufw default deny – drop all connections by
default
ufw status – current status and rules
ufw allow port – allow traffic on port
ufw deny port – block port
ufw deny from ip – block ip adress

Application Names
nautilus – file manager (GNOME)
dolphin – file manager (KDE)
konqueror – web browser (KDE)
kate – text editor (KDE)
gedit – text editor (GNOME)

System
Recovery - Type the phrase “REISUB” while
holding down Alt and SysRq (PrintScrn) with about 1 second between each letter. Your system will reboot.
lsb_release -a – get Ubuntu version
uname -r – get kernel version
uname -a – get all kernel information

Friday 13 June 2014

Grub is not coming (screen remains black!) and other grub issues [Ubuntu 12.04]

Problem:-  Sometimes after installing Ubuntu along with any other OS makes grub invisible,rather a black screen.

Solution:-  This problem occurs due to low resolution of your graphics (or monitor).This can be solved via terminal.
Open the terminal by pressing ctrl+Alt+T



















Now type in the terminal the following command:-
sudo vi /boot/grub/grub.cfg
Then a 'sudo' password is wanted,give your password there.
Now find out the line
"set gfxmode=" [It may be 'set gfxmode=auto' there]
Now put set gfxmode=640*480
And put the following line underneath it
 set gfxpayload=keep
That is it will look like
set gfxmode=640*480
set gfxpayload=keep
Now reboot and see that the grub is coming or not!

Many times this process becomes useless,then keep intact the grub.cfg to its initial form and follow the following process

sudo gedit /etc/default/grub
Now uncomment the line #GRUB_GFXMODE=640x480 by removing # ,then save and quit gedit.And now type
sudo update-grub
and reboot.
If grub menu still does not come then comment out the line in the same file written just above GRUB_HIDDEN_TIMEOUT=0 by putting a # before it,save and quit.
And type in terminal
sudo update-grub
and reboot.

Thursday 12 June 2014

A beginner's tutorial on Vim Editor-IV

Part-III:-

Now continuing from the previous articles,
There are so many handy editing commands in vim that one can't imagine.Here all of you know that editing means deleting,copying,pasting texts.
We'll see it one by one now,(all things are done in command mode)
Deleting Commands:
the main key to start deleting is 'd'.Now appending characters with 'd' makes sense in case of deleting.
dd -> To delete a line 
dw -> To delete a word
d0 -> To delete from the cursor from the beginning of the line
d$ -> To delete from the cursor to the end of the line
dgg -> To delete from the beginning of the file
dG ->  To delete  to the end of the file
If you delete or do anything out of mistake,you can undo it by pressing 'u' and again if you change your mind then press 'ctrl-r'
Again you can delete 'n' number of lines or words,just press 'ndd' or 'ndw' respectively.
Copying And Pasting:
yy -> This will copy a line
p -> This will paste the copied line after the current line
P -> This will paste the copied line on the current line
Again as before you can copy 'n' number of lines by pressing 'nyy'.
Enjoy it by using it! This is most probably the last article in the series.It is sufficient for beginners.But there are lot more things about this interesting editor.If you are interested there are lot of good resources.
One good book is "Learning the vi Editor" by Arnold Robbins, Linda Lamb.
Thank you!

A beginner's tutorial on Vim Editor-III

Part-III

Continuing from the previous lessons....
Replacement Commands:-
Sometimes it's need to replace a particular word with another text globally or specifically in a file.The terms 'globally' and 'specifically' is understood by the following example:
Suppose a text is written as follows:
Hello guys,this is Saptarshi Nag.
I want to say you "hello,world".
Now one wants to use 'hi' instead of 'hello's in global respect,i.e.,one wants to write
Hi guys,this is Saptarshi Nag.
I want to say you "hi,world".
This is global replacement
Again one may want to replace the only one of the 'hello's by a 'hi'.Suppose:
Hi guys,this is Saptarshi Nag.
I want to say you "hello,world".
Now both are possible through last-line mode.From previous lessons you know how to go into last-line mode.
For global replacement type in last -line method
%s/text/replacement/g (g stands for globally)
So here in the global replacement it would be
%s/hello/hi/g
But for specific replacement it'll be
%s/text/replacement/gc (gc stands for globally with confirmation)
Here for every replacement system wants to know if user wants to replace text.If yes press 'y' else 'n'.You'll see it by using it.
Therfore for the last example you should give
%s/hello/hi/gc
Then the answer to the confirmation of the first replacement will be 'y' then for the second one is 'n'.

A beginner's tutorial on Vim Editor-II

Part-II

In command mode one can move the cursor using keyboard but not using the 'arrow keys'.In fact we can perform so many jobs in command mode.
Movement Basics:-
h ->moves cursor left
j ->moves cursor down one line
k ->move cursor up one line
l ->moves cursor to the right
0 ->moves cursor to the beginning of a line
$ ->moves cursor to the end of the line
w ->moves cursor forward one line
b ->moves cursor backward one line
G ->moves to the end of the file(i.e.,shift+g)
gg ->moves to the beginning of the file
Adding a digit will multiply the movement,i.e.,5w will move the cursor forward 5 words but of course use it effectively.
Saving and Qutitting:-
To save a piece of text or code one has to go to the last line mode,i.e, press : in command mode.You'll see a ':' in the last line of the screen
To write
w <filename> //filename is not necessary if it is given previously
To write and quit Vim
wq
To quit without saving
q!
To write over another file
w! <filename>
To quit if nothing has been changed
q
Searching:-
One can easily search a character through keyboard in command mode
/<text> -> searches forward from the cursor position
?<text> ->searches backward from the cursor position
eg. ?hello
searches the text 'hello' forward from the cursor position
If there are other 'hello's ahead you can find them pressing 'n' and if there are other 'hello's before you can find them pressing 'N'
Now apply these commands to your text written in vim to get the perfect effect.

A beginner's tutorial on Vim Editor-I

Part-I


 The one of the most handy tools in linux is the vi editor.We can write any text,script,code here and run through the command line in the linux environment (provided the corresponding compiler or interpreter exists in the linux system).
Now we generally use graphical editor('gedit') to write a script or code due to its comfortable feature of syntax highlighting which is not a feature of common vi editor.So to make it more user friendly linux improved it,namely created 'vim'.
Vim=Vi improved
Now it has the feature of syntax highlighting and many more features mixing those of gedit and vi.
Generally ubuntu does not contain vim but we can download it for free through command line
$ sudo apt-get install vim
and done.
And now we can open the editor through command-line
$ vim <filename> //we can omit filename here and give it after writing something in editor
Now as vi editor it has three modes:
1.Command mode
2.Insert mode
3.Last line mode
In command mode we can edit,delete and do many operations.While opening vim it is always in command mode.
In insert mode we can write text on the editor and edit the text.
Last line mode is a part of command mode which is used to save the file and quit the environment of vi editor etc.
Now we move from one mode to one mode through mere keyboard
command mode -> insert mode= press 'i'
command mode ->last line mode=press ':'
insert mode -> command mode= 'Esc'
insert mode -> last line mode='Esc' then ':'
Again, for convenience we can into insert mode by pressing 'o' or 'O'
'o' -> enters into insert mode and opens a line below cursor
'O'->enters into insert mode and opens a line above cursor
whereas pressing 'i' enters into insert mode and opens a line on the cursor position.
Now let's write some text there.

Login-keyring password prompting after booting [Ubuntu]

Problem:-  'Login-Keyring' window is coming after starting Ubuntu.(Really annoying!)

Solution:-  Let's solve the problem via terminal. Terminal is such stuff in any Linux which make you powerful to perform tasks.Now the solution goes here

You can go to 'Dash Home'  by clicking the left hand top icon of the desktop and write 'terminal' there.


Now if you click there a 'black window' will open,this is what we call 'terminal'



You can also open it from Keyboard by pressing 'Ctrl+Alt+T'.
Now write the following code there:
cd /usr/lib/gnome-keyring
sudo rm gnome-keyring-prompt gnome-keyring-prompt-3
Now a 'sudo' password is wanted by the system,give the user password there.
Done!
Now reboot the computer and enjoy the effect! :)