Linux Command Record.

CHMOD:

chmod a+r file
read is added for all

chmod a-x file
execute permission is removed for all

chmod a+rw file
change the permissions of the file file to read and write for all.

chmod +rwx file
On some UNIX platforms such as BSD, this will restore the permission of the file file to default: -rwxr-xr-x.

chmod u=rw,go= file
read and write is set for the owner, all permissions are cleared for the group and others

chmod -R u+w,go-w docs
change the permissions of the directory docs and all its contents to add write access for the user, and deny write access for everybody else.

chmod = file
removes all privileges for all.

chmod 777 file
change the permissions of the file file to read, write, and execute for all.

chmod 664 file
sets read and write and no execution access for the owner and group, and read, no write, no execute for all others.

chmod 0755 file
equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). The 0 specifies no special modes.

chmod 4755 file
the 4 specifies set user ID and the rest is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1).

chmod 2755 file
the 2 specifies set group ID and the rest is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1).

chmod -R u+rwX,g-rwx,o-rwx directory
set a directory tree to rwx for owner directories, rw for owner files, --- for group and others.

chmod -R a-x+X directory
remove the execute permission on all files in a directory tree, while allowing for directory browsing.

—————————————————————————————————-

How to add a user to the sudoers list:

  1. Open a Root Terminal and type visudo (to access and edit the list)
  2. Using the up/down arrows, navigate to the bottom of the sudoers file that is now displayed in the terminal
  3. Just under the line that looks like the following:

    root ALL=(ALL) ALL

  4. Add the following (replacing user with your actual username):

    user ALL=(ALL) ALL

  5. Now press Ctrl+X and press Y when promted to save

That’s it, your new user now has root privileges!

Adding a new sudoer

—————————————————————————————————-

Ubuntu Change Password from Command Line:
ramesh@ramesh-desktop:~$ passwd
Changing password for ramesh.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

If you want to change password for another Ubuntu user, use the following method. Following example changes the password of user jsmith, from user ramesh.

ramesh@ramesh-desktop:~$ sudo passwd jsmith
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

—————————————————————————————————-

Find Out My Linux Distribution Name and Version

Method #1: /etc/*-release file

To find out what version of Linux (distro) you are running, enter the following command at the shell prompt:
$ cat /etc/*-release
Sample output from my RHEL v5.x server:

Red Hat Enterprise Linux Server release 5 (Tikanga)

Sample outputs from my Ubuntu Linux v7.10 server:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=7.10
DISTRIB_CODENAME=gutsy
DISTRIB_DESCRIPTION="Ubuntu 7.10"

Method #2: lsb_release Command To Find Out Linux Distribution Name/Version

The lsb_release command displays certain LSB (Linux Standard Base) and distribution-specific information. Type the following command:
$ lsb_release -a
Sample outputs:

No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 6.0.1 (squeeze)
Release:	6.0.1
Codename:	squeeze

How Do I Find Out My Kernel Version?

Type the following command:
$ uname -a
OR
$ uname -mrs
Sample outputs:

Linux 2.6.32-5-amd64 x86_64

Where,

  1. Linux – Kernel name
  2. 2.6.32-5-amd64 – Kernel version number
  3. x86_64 – Machine hardware name (64 bit)

Say hello to /proc/version

Type the following command to see kernel version and gcc version used to build the same:
$ cat /proc/version
Sample outputs:

Linux version 3.2.0-0.bpo.1-amd64 (Debian 3.2.4-1~bpo60+1) (ben@decadent.org.uk) (gcc version 4.4.5 (Debian 4.4.5-8) ) #1 SMP Sat Feb 11 08:41:32 UTC 2012

—————————————————————————————————-

Installing MySQL Server on CentOS

sudo yum install mysql-server
sudo /sbin/service mysqld start

—————————————————————————————————-

Show All Running Processes in Linux

How do I see all running process in Linux operating systems using command line or GUI options?
You need to use the ps command. It provide information about the currently running processes, including their process identification numbers (PIDs). Both Linux and UNIX support the ps command to display information about all running process. The ps command gives a snapshot of the current processes. If you want a repetitive update of this status, use top, atop, and/or htop command as described below.

Tutorial details

Difficulty
Easy (rss)

Root privileges
Yes

Requirements
ps/top/htop

Estimated completion time
5 minutes

Apart from ps command, you can also use the following commands to display info about processes on Linux:

  1. top command : Display and update sorted information about processes.
  2. atop : Advanced System & Process Monitor.
  3. htop : Interactive process viewer.

The ps command

Type the following ps command to display all running process:
# ps aux | less
Where,

  • -A: select all processes
  • a: select all processes on a terminal, including those of other users
  • x: select processes without controlling ttys

Task: see every process on the system

# ps -A
# ps -e

Task: See every process except those running as root

# ps -U root -u root -N

Task: See process run by user vivek

# ps -u vivek

Task: top command

The top program provides a dynamic real-time view of a running system. Type the top at command prompt:
# top
Output:

Fig.01: top command: Display Linux Tasks

Fig.01: top command: Display Linux Tasks

To quit press q, for help press h.

Task: display a tree of processes

pstree shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.
$ pstree
Sample outputs:

Fig.02: pstree - Display a tree of processes

Fig.02: pstree – Display a tree of processes

Task: Print a process tree using ps

# ps -ejH
# ps axjf

Task: Get info about threads

Type the following command:
# ps -eLf
# ps axms

Task: Get security info

Type the following command:
# ps -eo euser,ruser,suser,fuser,f,comm,label
# ps axZ
# ps -eM

Task: Save Process Snapshot to a file

Type the following command:
# top -b -n1 > /tmp/process.log
Or you can email result to yourself:
# top -b -n1 | mail -s 'Process snapshot' you@example.com

Task: Lookup process

Use pgrep command. pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. For example display firefox process id:
$ pgrep firefox
Sample outputs:

3356

Following command will list the process called sshd which is owned by a user called root:
$ pgrep -u root sshd

Say hello to htop and atop

htop is interactive process viewer just like top, but allows to scroll the list vertically and horizontally to see all processes and their full command lines. Tasks related to processes (killing, renicing) can be done without entering their PIDs. To install htop type command:
# apt-get install htop
or
# yum install htop
Now type the htop command at the shell prompt:
# htop
Sample outputs:

Fig.03: htop - Interactive Linux / UNIX process viewer

Fig.03: htop – Interactive Linux / UNIX process viewer (click to enlarge)

atop program

The program atop is an interactive monitor to view the load on a Linux system. It shows the occupation of the most critical hardware resources (from a performance point of view) on system level, i.e. cpu, memory, disk and network. It also shows which processes are responsible for the indicated load with respect to cpu- and memory load on process level; disk- and network load is only shown per process if a kernel patch has been installed. Type the following command to start atop:
# atop
Sample outputs:

Fig.04: atop command in action

Fig.04: atop command in action

—————————————————————————————————-

Kill Process in Linux or Terminate a Process in UNIX / Linux Systems

Linux and Unix-like operating system come with the kill command to terminates stalled or unwanted processes without having to log out or restart the server.

The kill command sends the specified signal such as kill process to the specified process or process groups. If no signal is specified, the TERM signal is sent. Please note that kill command can be internal as part of modern shells built-in function or external located at /bin/kill. Usage and syntax remain similar regardless internal or external kill command.

Tutorial details

Difficulty
Easy (rss)

Root privileges
Yes

Requirements
kill

Estimated completion time
10 minutes

A list of common Term singles

Linux and Unix-like operating system supports the standard terminate signals listed below:

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

What is a PID?

A Linux or Unix process is running instance of a program. For example, Firefox is a running process if you are browsing the Internet. Each time you start Firefox browser, the system is automatically assigned a unique process identification number (PID). A PID is automatically assigned to each process when it is created on the system. To find out PID of firefox or httpd process use the following command:

  pidof httpd
pidof apache2
pidof firefox  

OR use the combination of ps command and grep command:

  ps aux | grep httpd
ps aux | grep apache2
ps aux | grep  firefox  

Sample outputs:

Fig.01: Find the process ID (PID) of a running firefox program and apache2 server.

Fig.01: Find the process ID (PID) of a running firefox program and apache2 server.

kill command syntax

The syntax is:

  kill [signal] PID
kill -15 PID
kill -9 PID
kill -SIGTERM PID
kill [options] -SIGTERM PID  
What Linux or Unix permissions do I need to kill a process?

Rules are simple:

  1. You can kill all your own process.
  2. Only root user can kill system level process.
  3. Only root user can kill process started by other users.
kill command examples

In this example, I am going to kill lighttpd server.

Step #1: Find out the PID (process id)

Use the ps or pidof command to find out PID for any program. For example, if process name is lighttpd, you can use any one of the following command to obtain process ID:

  pidof lighttpd  

Sample outputs:

3486

OR

  ps aux | grep lighttpd  

Sample outputs:

lighttpd  3486  0.0  0.1   4248  1432 ?        S    Jul31   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lighttpd  3492  0.0  0.5  13752  3936 ?        Ss   Jul31   0:00 /usr/bin/php5-cg
Step #2: kill the process using a PID

The PID # 3486 is assigned to the lighttpd process. To kill the lighttpd server, you need to pass a PID as follows:
# kill 3486
OR
$ sudo kill 3486
This will terminate a process with a PID of 3486.

How do I verify that the process is gone / killed?

Use the ps or pidof command:
$ ps aux | grep lighttpd
$ pidof lighttpd

A note about sending stronger signal # 9 (SIGKILL)

If no signal specified in the kill command, signal # 15 (SIGTERM), is sent by default. So thekill 3486 command is same as the following command:
# kill -15 3486
# kill -SIGTERM 3486

OR
$ sudo kill -15 3486
$ sudo kill -SIGTERM 3486

Sometime signal # 15 is not sufficient. For example, lighttpd may not be killed by signal #15 due to open sockets. In that case process (PID) # 3486 would be killed with the powerful signal # 9:
# kill -9 3486
# kill -SIGKILL 3486

OR
$ sudo kill -9 3486
$ sudo kill -SIGKILL 3486

Where,

  • -9 or -SIGKILL – A special kill signal that nearly guarantee to kill the process with the iron fist.
How can I kill two or more PIDs?

The syntax is as follows to kill two or more PIDs as required can be used in a single command:

  kill  pid1 pid2 pid3
kill -15  pid1 pid2 pid3
kill -9  pid1 pid2 pid3
kill  -9 3546 5557 4242  

Say hello to killall command

This is a Linux only command. to kill processes by name. So no need to find the PIDs using the ‘pidof process’ or ‘ps aux | grep process’ command. Do not use killall command on Unix operating systems. This is a Linux specific command.

The syntax is
  killall Process-Name-Here  

To kill the lighttpd server, enter:
# killall -15 lighttpd
OR
# killall -9 lighttpd
To kill the Firefox web-browser process, enter:
# killall -9 firefox-bin

As I said earlier, the killall command on UNIX-like system does something else. It kills all process and not just specific process. Do not use killall on UNIX system.

—————————————————————————————————-

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s