Monday 1 February 2016

Linux Basic Commands (Part-2)

Process Management
Due to multitasking behavior of Linux operating system there are running many processes at the same time. Every process has its id. Along with OS own processes running there are user’s processes are also running on the machine. If Administrator would like to get a snapshot of what is currently happening on the machine may use top command. But top shows only real time view which fits on the screen. Here is another command i.e ps stand for processes. It will shows you the processes running on current terminal but it takes aux argument will show complete system view which may be more helpful. System Admins use grep with ps usually pipe the output. Any process can be killed by kill command. Examples are mentioned below for more detail;
1.  top (shows what is currently happening with system)
2.  ps aux | less (shows the all running processes with process ids even by other users)
3.  ps aux | grep ‘firefox’ (output of ps will be piped through grep to search for specific process i.e firefox.)
4.  pstree (displays the process in tree format)
5.  kill 1234 (kills the process having id 1234)
6.  kill -9 1234 (forefully kills the process having id 1234)
Package Management
Package Management System is collection of software tools that automates the process of installing, upgrading, configuring and removing programs of OS in a consistent manner. Following are the commands to handle the various packages in linux;
1.  rpm –qa sendmail (checks currently installed package of sendmail)
2.  rpm –qi sendmail (list all the information about sendmail package)
3.  rpm –qd sendmail (lists the documentation files and their location of particular installed package)
4.  rpm –ivh /mnt/cdrom/mail/sendmail.rpm (check the particular package if not then install it from mounted cd rom)
5.  rpm –Uvh /mnt/cdrom/mail/sendmail.rpm (check the particular package installed then upgrade it to newer version.)
6.  rpm –e sendmail (To remove/uninstall the particular package)
System Services Configuration
Running linux systems have a number of background processes executing at any time. These processes are also known as services or daemon run as part of an application. OS services are like sshd and application daemons are httpd. These services are supposed to continuously to make sure that our websites, mail, databases and other applications are always running and up. Furthermore System Admins needs to continuously run these services without failing and start automatically after when system crash or reboots. A reboot can be planned restart, due to patch update or unexpected system behavior.  Crash may stop the process unexpectedly and application may be unresponsive. 
These Linux services can be made largely self-healing by changing the way they are handled by service management daemons, also known as init systems. After crash or unexpected reboot services need to start automatically via following commands;
1.  netstat –at (To check the status of the daemons listing on different ports)
2.  Chkconfig --level 3 bind on (bind service will be automatically start on run level 3 when system reboots)

Device Configuration
For hardware configuration you can use following commands;
1.  Sysreport (will generate a hardware list of your computer and save it in tar file)
2.  Kbconfig (for keyboard layout)
3.  Mouseconfig (for mouse)
4.  Printconfig (for printer configuration)
5.  Timeconfig (for time zone configuration)
Hardware information
1.  Like everything, there are commands to check hardware information of linux system;
2.  lscpu (displays cpu information)
3.  lshw (displays detailed and brief information about different hardware units.)
4.  lspci (displays details about all pci busses)
5.  lsscsi (lists the scsci/sata drives and optical devices.)
6.  lsusb (lists usb devices and details about devices connected)
7.  free –m (check the amount of ram used, free and total)
User Management and File Permissions/Ownerships
The control of users and groups is a core element of Linux System Administration. A user may be a human being or account used by specific applications identified by a unique numerical number called user ID (UID). Users within a group can have read, write and execute permissions or any combination of read/write/execute permissions of files owned by group.
A group is an organization unit tying users together for a common purpose, which can be reading, writing, or executing permissions for files owned by that group. Similar to UID, each group is associated with a group ID (GID). Each file and directory on your system is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file (i.e., run the file as a program). A user who creates a file is also the owner and primary group owner of that file. Following are the commands for creating users and groups;
1.  Groupadd mailusers (create new group named mailusers)
2.  useradd –a  /bin/false –g mailusers imran (create new user named imran and make him member of group mailusers)
3.  passwd imran (to change the password of new created user imran)
4.  passwd –l imran (to lock the password of the user imran)
5.  passwd –u imran (to unlock the password of the user imran)
6.  userdel –r imran (to delete the user named imran with his home directory)
7.  chmod 770 myfile.txt (change the permissions of the file myfile.txt to read, write, and execute for owner and group)
8.  chown –R /imran:mailusers /project (change the ownernship of the directory /project to user imran and set the group permissions to group mailusers)
Note: I will cover Permissions topic in detail in my next blog separately.

Modules Management
The Linux kernel allows drivers and features to be compiled as modules rather than as part of the kernel itself. This means that users can often change features in the kernel or add drivers without recompiling, and that the Linux kernel doesn't have to carry a lot of unnecessary baggage. Here are few commands for listing, inserting and removing modules;
1.  lsmod (list the current modules loaded)
2.  modprobe modulename (add/insert a module)
3.  rmmod (remove a module)
Routing table and naming
Routing table guides incoming and outgoing traffic to and from your system towards network. Linux kernel supports multiple routing tables. Following are the commands to handle with routing tables.
1.  route –n (to check the routing table)
2.  route add default gw 192.168.1.1 (add default gateway to your system)
3.  route del default (to delete default gateway)
4.  route add –net 192.168.1.0 netmask 255.255.255.0 dev eth0 (add route to specific network)
5.  route del –net 192.168.1.0 netmask 255.255.255.0 dev eth0 (add route to specific network)
6.  traceroute 192.168.1.1 (to trace number or hops to particular host)
7.  mtr 192.168.1.1 (to trace and ping at the same time to particular host)
8.  host imran.mydomain.com (to find host and its ip address on the network)
9.  nslookup imran.mydomain.com (to find host and its ip address on the network)
10.         dig imran.mydomain.com (to find host and its ip address on the network)
11.         hostname (to find FQDN of your system)
Miscellaneous
Here are some miscellaneous commands below;
1.  Man snmp (to find help on some topic i.e snmp)
2.  Info snmp (another command to find help on some topic i.e snmp)
3.  find /usr –name “*.doc” (to find a *.doc file in /usr directory)
4.  find /usr –group mailusers (to find files and directories owned by group mailusers in /usr directory)
5.  find / -user imran –exec rm ‘{}’ ‘; (to find and delete files owning by user imran who left organization for security reasons)
6.  uptime (check the system how long it is up)
7.  who –b (to find out when system last time booted)
8.  service sendmail status (to check the status of the service)

9.  cat /etc/redhat-release (to know about redhat version)

Part 1




Linux Basic Commands (Part-1)
Guys! I am going to start basic Linux How Tos. I would like to start these How Tos from basic linux commands reference guide, just a brief description of each command not much detail. The purpose of these How Tos is to get introduce the linux i.e Red Hat, CentOS and Fedora etc. to IT professionals who use these day to day and sometimes slips out of mind. This brief guide will help those get reference of those commands.
Directory listing
To list the directory contents and information about files (current directory by default) in linux you use command ls with many switches defined below.
1.  ls –l (use a long listing format)
2.  ls –h (human readable – print the size in human readable format i.e 1k, 234MB, 2G etc).
3.  ls –a (shows all - do not hide entries starting with)
4.  ls –r (shows reverse order while sorting 
5.  ls –t (sort files by modification time)
 
Note: ls commands have many more switches but most commonly used are mentioned here.

Disk usage
The du (Disk usage) is command used to check the information of disk usage of directories and files. The du command has also many parameters which can be used to get result in different formats. Most common are mentioned below;
1.  du /home/userdirectory (shows the disk usage summary of a user’s directory tree and each of its sub directories.
2.  du –h /home/userdirecotry (shows the size in human readable format)
3.  du –sh /home/userdirectory (shows the total disk usage size of a parituclar directory.
4.  du –a /home/userdirectory (shows the disk usage of all files and directories)

Mounting / un-mounting directories
The mount is used to check that currently mounted partitions on your machine;
1.  mount (currently mounted partitions on the machine)
2.  mount /dev/cdrom /mnt/cdrom (will mount the cdrom in mount point i.e /mnt/cdrom)
3.  mount –t nfs 192.168.1.1:/usr/directoryname /mnt/cdrom (will mount a NFS Share i.e /usr/directoryname from machine 192.168.1.1 to your local mount point /mnt/cdrom
4.  cat /etc/mtab (shows the mounted file system table)
5.  unmount /mn/cdrom(this command will unmount the particular device or directory)

File copying / moving / renaming / deletion / compression and decompression
Following commands will be used while we need to copy, move, rename, delete compress or decompress the files.
1.  cp –vr /home/userdirectory/* /mnt/dir1 (copies all files including subdirectories form /home/userdirectory to /mnt/dir1)
2.  mv /home/userdirectory/* /mnt/dir1 (copies all files including subdirectories form /home/userdirectory to /mnt/dir1)
3.  rm –fr /mnt/dir1/* (forcefully deletes all files including subdirectories in /mnt/dir1)
4.  tar –cvf /tmp/mytar.tar * (creates tar file in /tmp/mytar from the files in current directory)
5.  tar –xvf /tmp/mytar.tar (untar the file /tmp/mytar.tar in current directory.
Disk Checking and Formatting
Fdisk is text based utility used in linux distributions for the management of disk partitions. Using fdisk you can create new partitions, delete or change existing partitions on machine. You can create maximum 4 primary partitions and any number of logical partitions depending upon disk size. Following are the fdisk commands used;
1.  fdisk –l (used to view all available partitions on machine)
2.  fdisk –l /dev/sda (will show specific hard drive i.e sda)
3.  fdisk –s /dev/sda (will show the size of the partition i.e sda)
4.  e2fsck /dev/sda (perform disk check)

5.  e2fsck –p /dev/sda (perform disk check and automatically repair)

Part 2