Search This Blog

Basic Linux Commands



  1.   To know server IP                                                                                               -  if config
  2.   To give static IP                                                                                                   -  net config
  3.   To restart network                                                                                               -  service network restart
  4.   To shutdown                                                                                                        -   init 0
  5.   To restart                                                                                                              -  init 6
  6.   To ping ip address                                                                                              -   ping dest ip address
  7.   To know current date & time                                                                              -      # date
  8.   To know users who have logged into linux system currently                           -      # who
  9.   To know login time and system name for connection being used                  -  # who am i
  10.   To get help                                                                                                            -  # man who
  11.   To display initial part of text file                                                                           -   # head (-count) (file name)
  12.   To display last part of text file                                                                              -   # tail     (+/- number) (file name)
  13.   To display full path for current directory                                                             -  # pwd 
  14.   To create a new directory                                                                                   -   # mkdir directoryname
  15.   To change from working directory to another directory                                   -   # cd new directory name
  16.   To display list of files in current working directory                                            -    # ls
  17.   To list contents of files we specify                                                                      -    # cat <file name>
  18.   To establish an additional filename for the same ordinary file                      -    # ln firstname secondname
  19.   To list files in long format                                                                                   -   # ls   -l or  # ls cha*
  20.   To list all entries,including hidden files                                                               -  # ls-a
  21.   To put a slash after each directory                                                                     -  # ls-p
  22.   To list in order of last access time                                                                     -  # ls-u
  23.   To list in order of last modification time                                                            -  # ls-t
  24.   To list directory files instead of its contents                                                     -  # ls-d
  25.   To remove a directory specified                                                                       -  # rmdir directory name
  26.   To remove a file                                                                                                   -  # rm -i filename
  27.   To delete the entire contents of directory as well as directory it self            -   # rm -r directory  name
  28.   To copy a content of one file to another file                                                    -   # cp file1  file2
  29.   To move one file to another file                                                                        -   # mv file1 file2
  30.   Group of files begining with 'cha' and want to list these files  -  
  31.   Group of files ending with 'ing' and want to list these files                              -   # ls  *ing
  32.   To know version of linux                                                                                      -   # uname -r
  33.   To know which processor we are using                                                             -  # uname -m
  34.   To search a string/word in file                                                                             -  l name
  35.   To insert a text in vim editor press                                                                     -  i key
  36.   To save a file and quit after changes                                                                 -  : wq !
  37.   To quit without saving file                                                                                       -  : q !
  38.   To compile the file  -  
  39.   Replace old copy of file with new oneand quits                                                    -  : x
  40.   Makes changes and quits from the command mode                                            -  zz
  41.   It deletes the line in which cursor is positioned                                                     -  dd
  42.   It deletes the line from current cursor position to end of the line                          -  D
  43.   To create a file in vi editor                                                                                      -  # vi filename .pl or .c
  44.   To return from command mode to vi editor                                                          -  Esc key
  45.   To specify command in esc mode                                                                        -  " : "
  46.   To undo command in command mode                                                                 -  " u "






Permissions

The Unix operating system (and likewise, Linux) differs from other computing environments in that it is not only a multitasking system but it is also a multi-user system as well.
What exactly does this mean? It means that more than one user can be operating the computer at the same time. While your computer will only have one keyboard and monitor, it can still be used by more than one user. For example, if your computer is attached to a network, or the Internet, remote users can log in via telnet or ssh (secure shell) and operate the computer. In fact, remote users can execute X applications and have the graphical output displayed on a remote computer. The X Windows system supports this.
The multi-user capability of Unix is not a recent "innovation," but rather a feature that is deeply ingrained into the design of the operating system. If you remember the environment in which Unix was created, this makes perfect sense. Years ago before computers were "personal," they were large, expensive, and centralized. A typical university computer system consisted of a large mainframe computer located in some building on campus and terminals were located throughout the campus, each connected to the large central computer. The computer would support many users at the same time.
In order to make this practical, a method had to be devised to protect the users from each other. After all, you could not allow the actions of one user to crash the computer, nor could you allow one user to interfere with the files belonging to another user.
This lesson will cover the following commands:

  • chmod - modify file access rights
  • su - temporarily become the superuser
  • chown - change file ownership
  • chgrp - change a file's group ownership

File permissions

Linux uses the same permissions scheme as Unix. 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).
To see the permission settings for a file, we can use the ls command as follows:

[me@linuxbox me]$ ls -l some_file

-rw-rw-r-- 1 me   me   1097374 Sep 26 18:48 some_file
We can determine a lot from examining the results of this command:

  • The file "some_file" is owned by user "me"
  • User "me" has the right to read and write this file
  • The file is owned by the group "me"
  • Members of the group "me" can also read and write this file
  • Everybody else can read this file
Let's try another example. We will look at the bash program which is located in the /bin directory:

[me@linuxbox me]$ ls -l /bin/bash

-rwxr-xr-x 1 root root  316848 Feb 27  2000 /bin/bash
Here we can see:

  • The file "/bin/bash" is owned by user "root"
  • The superuser has the right to read, write, and execute this file
  • The file is owned by the group "root"
  • Members of the group "root" can also read and execute this file
  • Everybody else can read and execute this file
In the diagram below, we see how the first portion of the listing is interpreted. It consists of a character indicating the file type, followed by three sets of three characters that convey the reading, writing and execution permission for the owner, group, and everybody else.
permissions diagram


chmod

The chmod command is used to change the permissions of a file or directory. To use it, you specify the desired permission settings and the file or files that you wish to modify. There are two ways to specify the permissions, but I am only going to teach one way.
It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here's how it works:

rwx rwx rwx = 111 111 111
rw- rw- rw- = 110 110 110
rwx --- --- = 111 000 000

and so on...

rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4

Now, if you represent each of the three sets of permissions (owner, group, and other) as a single digit, you have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set some_file to have read and write permission for the owner, but wanted to keep the file private from others, we would:

[me@linuxbox me]$ chmod 600 some_file
Here is a table of numbers that covers all the common settings. The ones beginning with "7" are used with programs (since they enable execution) and the rest are for other kinds of files.


Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
755 (rwxr-xr-x) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
700 (rwx------) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
666 (rw-rw-rw-) All users may read and write the file.
644 (rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
600 (rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.

Directory permissions

The chmod command can also be used to control the access permissions for directories. In most ways, the permissions scheme for directories works the same way as they do with files. However, the execution permission is used in a different way. It provides control for access to file listing and other things. Here are some useful settings for directories:


Value Meaning
777 (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
755 (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
700 (rwx------) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.

Becoming the superuser for a short while

It is often useful to become the superuser to perform important system administration tasks, but as you have been warned (and not just by me!), you should not stay logged on as the superuser. In most distributions, there is a program that can give you temporary access to the superuser's privileges. This program is called su (short for substitute user) and can be used in those cases when you need to be the superuser for a small number of tasks. To become the superuser, simply type the su command. You will be prompted for the superuser's password:

[me@linuxbox me]$ su Password: [root@linuxbox me]#
After executing the su command, you have a new shell session as the superuser. To exit the superuser session, type exit and you will return to your previous session.
In some distributions, most notably Ubuntu, an alternate method is used. Rather than using su, these systems employ the sudo command instead. With sudo, one or more users are granted superuser privileges on an as needed basis. To execute a command as the superuser, the desired command is simply preceeded with the sudo command. After the command is entered, the user is prompted for the user's password rather than the superuser's:

[me@linuxbox me]$ sudo some_command Password: [me@linuxbox me]$

Changing file ownership

You can change the owner of a file by using the chown command. Here's an example: Suppose I wanted to change the owner of some_file from "me" to "you". I could:

[me@linuxbox me]$ su Password: [root@linuxbox me]# chown you some_file [root@linuxbox me]# exit [me@linuxbox me]$
Notice that in order to change the owner of a file, you must be the superuser. To do this, our example employed the su command, then we executed chown, and finally we typed exit to return to our previous session.
chown works the same way on directories as it does on files.

Changing group ownership

The group ownership of a file or directory may be changed with chgrp. This command is used like this:

[me@linuxbox me]$ chgrp new_group some_file
In the example above, we changed the group ownership of some_file from its previous group to "new_group". You must be the owner of the file or directory to perform a chgrp.




Navigation :
  1. To print working directory :  pwd
  2. To change directory            :  cd
  3. To list files and contents of directories :  ls
  4. To list files/direc in long format : ls –l
  5. To list the hidden files/direc : ls –a
  6. To view text files : less  text_file
  7. To know what kind of a file it is : file name_of_file
Manipulating Files:
  1. To Copy files and directories :   cp file1 filenew
  2. To move or rename the files/directories : mv  file1  filenew
  3. To remove the file/directories :   rm file
  4. To make directory : mkdir

Working with commands:
  1. Display Information about command type: type  command(ls,cp..etc)
  2.  To locate the command : which command (ls…etc)
  3. To display the reference page: help –m  command(cd..etc)
  4. To display on-line command interface : man ls
I/O Redirection :
  1. To redirect standard output to a file   : “ > “    ( ls  >  file_list.txt )
  2. To redirect standard output to a file   : “ < ”    (sort <  file_list.txt > sorted_file.txt)
  3. With pipeline the output of one command is fed into input of another :
             ls –l | less   
  rpm –qa| grep –i filename :   Rpm –qa  list  for all rpm files installed on system and  Grep Accweb  it  find any containing the string “accweb” and output them.

  1. Filters:
sort : Sorts standard input then outputs the sorted result on standard output.
grep : Examines each line of data it receives from standard input and outputs every line that contains a specified pattern of characters.
tail : Outputs the last few lines of its input. (Ex : tail –f logs.txt)
Permissions:
  1. to change the permissions of folders/files : Chmod  XXX filename
  2. Temporarily become super user : su & sudo
  3. Change file ownership of user :  chown  user filename
  4. Change file ownership of group : chgrp  group filename
  5. Change file ownership of user & group : chown  user:group filename

Job Control:

  1. To know the process running  : ps  or jobs
  2. to know the processes running on system   : Ps  aux
  3. to find process with user  :           Ps  –u  name of User
  4. to kill the process   : Kill -9  process name
 -9   Kill signal
-1   hang-up signal
-2 interrupt signal
-15 termination signal


Wildcards:
  1. Matches any characters  :  *
  2. Matches ant single character:  ?
  3. All filenames  :  *
  4. All filenames that begin with the character "g" :  g*
  5. All filenames that begin with the character "b" and end with the characters ".txt" : b*.txt


Txt Editor:

  1.  to edit the configuration/txt  file (command line)  : vi/vim 
  2.  gedit is the editor supplied  (GUI)  : gedit
Searching:
  1. Search  file by name  : Find -name "query"
  2. Search file by type   : Find  -type  type_descriptor_query
                 Type decriptor :
F -  regular file
d- directory
I -symbolic link
  1. Search  :  locate filename
  2. We can use  locate/grep for searching :
grep  "codes"  install.log   : searh for word/string/line  in a file   (grep –n ,display with line number, grep –I it search for case sensitive )
grep -i  "codes"(Search string)   /root/install.log (path of file)
[root@localhost ~]# grep "codes" install.log
Installing iso-codes-3.16-2.el6.noarch

Remote Administration :
  1. Can use ssh with a full hostname to connect to a remote machine : ssh hostname
  2. Connect to a remote system with your current username :ssh username@hostname
  3. Allows you to copy files from one computer/server to another computer/server : scp

Other Commands:
  1. To know which user is logged : whoami
  2. To know who recently  logged : users
  3.  
  4. history : for monitoring commands executed by user.
  5.    Service :  for  start/stop/restart/status (Service httpd start,service red5 stop,…etc)
  6. Top : to monitor system activities
  7. df    - to know disk file space on machine (df –h  size in GB,df –m  sizein MB )
  8. fdisk –l  - to  display with partitions
  9. Netstat : give information about network connections (nestat –tcp  display all tcp connections)
  10. Mount – for mounting formatted partitions

Basic Network Commands :   

  1. Netstat :   to look up the various active connections within a computer
  2. Traceroute : to know the number hop it travelled to reach destination.
  3. Nslookup:   It is use for DNS quiery.
  1. Dig : It is use for DNS quiery.It display full info.
  1. Ifconfig  :  It is very useful in determining what could be wrong with a network.to know IP address.
  2. Ifup:  enable interface
  3. Ifdown : disable interface.
  4. Ping :  PING is used to check for a response from another computer on the network.
  5. Ping  -c  hostadress :
  6.  



What command used to check Disk Space ?

1 Command to know disk space  :  df                
2 Command to know size in GB  :  df -h
3 Command to know size in MB  : df -m

How to change the Ownership of file/Group?

ls -lart tmpfile
-rw-r--r-- 1 himanshu family 0 2012-05-22 20:03 tmpfile

Before using command :

Command to change Ownership of file:  chown  root  tmpfile

Command to change Ownership of group : chgrp  friends tmpfile


After using command above:
ls -l tmpfile
-rw-r--r-- 1 root friends 0 2012-05-22 20:03 tmpfile



What command used for finding the process running?

Command to see processes running   :  ps
Command to find the process run by user :  ps -u  Sukesh

What  Command to kill a process ?
                            Kill -9    process name

-9   Kill signal
-1   hangup signal
-2  interrupt signal
-15  termination signal

Search for a file by name ?

Find -name "query"

Search the file by type?

Find -type type_descriptor query

Type decriptor :
F -  regular file
d- directory
I -symbolic link


How  to know which user is logged?

Command :   whoami

Who recently  logged in?

Command  : users

How do we transfer file from one server to another?

          Command used  to establish ssh connection :   ssh  username@ IP

How we move file from one folder to another?

                            Command :       Mv   file name   remote folder path

How do we copy file from one folder to another folder?

                            Command:      cp  filename  remote folder path

What command use for add interface?

                  Command :            ifup
What command used fo remove interface?

                  Command :      ifdown

To know Current working Directory ?

Command used  : Pwd

Command to give permissions?

Chmod XXX  filename

permissions diagram



777   rwx rwx rwx

rwx = 111 in binary = 7
rw- = 110 in binary = 6
r-x = 101 in binary = 5
r-- = 100 in binary = 4

Chmod 777 filename


******************************************************

******************************************************

         

No comments:

Post a Comment

REST API

  API  is an application programming interface. It is a set of rules that allow programs to talk to each other. The developer creates the AP...