Quantcast
Channel: Webminal - Latest topics
Viewing all 482 articles
Browse latest View live

Welcome to Webminal community

0
0

@system wrote:

Welcome to Webminal! This site is dedicated to Webminal community to share, learn GNU/Linux skills and seek help.

Please feel free to post queries related to Linux commands or scripting languages or Linux admin related questions. Share your Linux skills with rest of the community in the form of posts, How-To's and article or tips.

Due to security reason, you need to register here again, Sorry! - We try to keep webminal separate from other community servers! When it comes to security, we are little paranoid :relieved:

And please do remember this while interacting with forum members!

Posts: 6

Participants: 2

Read full topic


How to find how much time you spent on your system?

0
0

@laks wrote:

Ever wondered, how often you logged into your linux system? and how much time you spent on it ?

Just type

last -aFw

It will show the listing of last logged in sessions. If your system has multiple user accounts, then you will see their timing too.

You can also use

 ac -ad

to know about summary user login times.

Posts: 1

Participants: 1

Read full topic

About the Linux Tips category

0
0

@laks wrote:

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won't appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?

Posts: 1

Participants: 1

Read full topic

Linux Command-line Journey Day-1

0
0

@laks wrote:

This tutorial originally written for Online Linux Terminal 'www.webminal.org' - So better open
your terminal and read on!

  • Know your location
  • Where Am I ?
  • What's a command
  • How to create a new directory?
  • Analyzing ls command output
  • Move into a directory
  • What's . and .. ?
  • Inode numbers
  • Absoulte and Relative path names

Know your location:

Assume, your user name is "efg".
When you first login to a linux system, it will display "Last login: " Time and
date and content of the file /etc/motd (this file is owned by the root user) and
then it will greet you with a bash prompt, typically you will get something like -


[efg@fedori ~]$

"fedori" here refers the hostname. "$" denotes the bash shell is ready to accept your commands.

Where Am I ?

Let's understand how you landed in this location. By default, when you login, you
will be redirected to a directory specified in the /etc/passwd file. This entry is
created when the system-admin created an account for you and all /etc/ are
configuration files. The /etc/passwd file has an entry for you, like -


efg:x:500:500:EFGname:/home/efg:/bin/bash

Let's split them to understand it-


efg - login name
x - Password (place holder in modern system, user password will be stored in another
encrypted file /etc/shadow)
500 - unique user id
500 - groub id
EFGname - real name
/home/efg - home directory
/bin/bash - shell type

The summary of this entry would be - after verifying password for user "efg" in the /etc/shadow file,
allow user "efg" to login and place him under "/home/efg" with bash as the shell type.

You can get your current (working) directory using the command "pwd" (pRINT wORKING dIRECTORY).

What's a command

First of all, what is a command? It's a binary (also known as executable) file
kept under a specific location. Guess where? Near the washing machine? No ....
near the TV? ..no again.
Okay. Here is a hint, you can use another command called "whereis" to find the
location of the command. Lets try it first:


[efg@fedori ~]$ whereis pwd
pwd: /bin/pwd

pwd binary can be found under the /bin directory. Wait a second, before running the pwd command.
There is more than one location where the binary files are kept.
You can view then using:

[efg@fedori ~]$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin

When you type something on bash prompt,

[efg@fedori ~]$something

Bash assumes you entered a command called "something" and check it first on the
current directory and then paths specified by the PATH environmental variable
-so first it goes to "/usr/local/bin" and finds there is no file called "something",
then it checks /usr/bin and then /bin, etc.If it didn't find any file name it'll
display the "command not found" error message as shown below-
`
[efg@fedori ~]$ something
bash: something: command not found

[efg@fedori ~]$ pwd
/home/efg

`
So you are in the right place, at your home directory as expected by your /etc/passwd entry.
How to "clear" above errors messages from the screen? ..there is command called ..
you figure this out yourself, the question itself has the "answer". :smile:

How to create a new directory?

You are at your home. It's nothing but a directory you can create your own
directory using the command,

[efg@fedori ~]$touch dir0

If no error message is displayed after executing this command,then your directory is created.

Then in order to check the presence of the directory, use the "ls" command.

[efg@fedori ~]$ls
dir0

It should display your newly created directory.

Another way to create a directory is by using "mkdir" (mAkE dirECTORY)

[efg@fedori ~]$mkdir dir1

If no message is displayed after executing this mkdir command {it means it went without error}.
Then in order to list directory contents, use the "ls" command.

[efg@fedori ~]$ls
dir0 dir1

displays your newly created directories.

Analyzing ls command output

ls - command will display directory contents. If you want to know about it's permission,use "ls -l"

[efg@fedori ~]$ls -l
-rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0
drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

Let's understand this output first, see the second line, can you see 4096, yes? yes? Okay,
then ignore it for now :smiley: on its right side, you will see "date and time and directory name".
-date/time here refers creation time for this entry.And on its left side, you have four entries separated
by white fedori(blank fedori).drwxrwxr-x 2 efg efg
efg efg says user named efg who belongs to the group called efg is owner of this directory and group
member can also use this directory.

So what's is a group? We will talk about this while dealing with root user commands. For now just think of it
as your friends.

Lets check the first entry -

drwxrwxr-x

It denotes the permissions for this directory. In GNU-Linux,
the first "d" letter mention its a directory.
In general,rwx stands for read, write and execute.
then we have rwx repeated twice.
First three letters, (rwx) always denotes owner and second three (rwx) always refers
to group or friends and the final three,(r-x) always refers to all others or unknown
people who uses this machine. "-" here denotes,others don't have write permission,
they can only view/search this directory.

Now let's examine the first line,

-rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0

Yes, I can hear you screaming, "It doesn't have 'd' letter at it's begining? What happened?"
Remember the command used to create this one, "touch" - I lied about it :)- touch
creates an empty file not a directory. So there is only one way to create directory,
that is to use "mkdir" command.

A touching note: -if the file doesn't exists, the touch command will create an empty file,
if file already exists, it updates its timestamp.

Okay,back to exploration-

Since "dir0" is regular file - it don't have a "d" at beginning, instead uses "-". Only special
files are denoted by first entry (a directory is also a special file).
rw-, says owner "efg" can read and write into this file and he can't execute it. Similarly next three
fields "rw-",users belong to group "efg" can read and write.Final three entries "r--" says other can read/view

this file content ,but they can't modify or execute it.

since touch creates an empty file 0 - denotes its size. and date and time of creation along with file name is
displayed on the rest of the line.

Now we have two entries on our home current directory and lets visualize them.-Close your eyes and think
about it! ..wait don't sleep :smile:

/home/efg/ (our working directory.)
|
|-------------dir0
|-------------dir1/

how to move "into" newly created directory? Can you guess the command for it? If you think "into" as the answe
r,
congrats!!!, you are dead wrong :smile:

Move into a directory

use "cd" command,which stands for "cHANGE dIRECTORY",to move into a specific directory.

Before you use cd command on directory, what will happen, if you use it with our file
dir0? Let's try that first -

[efg@fedori ~]$ cd dir0
bash: cd: dir0: Not a directory

"cd" is wise, it checks "dir0" and realizes it don't have "d" on it's beginning and says
this error message. Good.Now move on,

[efg@fedori ~]$cd dir1

We moved into a whole new directory. We can verify this by using "pwd" command and list the directories
content using ls and ls -l . Enjoy.

Oh oops ..there is nothing! Try it again but this time pass 'a' as an option for ls command
"ls -la" . a surprise awaits you!

[efg@fedori dir1]$ ls -al
total 8
drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 .
drwx------ 5 efg efg 4096 2010-08-13 08:15 ..

What's . and .. ?
================
Whats this "." dot and ".." dot dot refers here? Let's wear the hat of Sherlock Holmes and
investigate this case as "the Curious case of . and .. " Two striking things are both entries
has "d" in front and same time stamp.

Check the timestamp of "dir1" when it's created using mkdir. Got it? both are same,so these
two entries "." and ".." are created when mkdir command was used. What they actually refer to?
Here comes the ls command to the rescue again, now pass "i" option to it.

[efg@fedori dir1]$ ls -lia
total 8
1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 .
1966452 drwx------ 5 efg efg 4096 2010-08-13 08:15 ..

Inode numbers
=============
Numbers in front are called "Inode" numbers. These numbers are actually used by linux kernel.
Each file has a unique inode number.We use filename or directory names but kernel uses only these numbers.

Repeat the same procedure for the /home/efg directory, you can use ls command and pass
the directory name as argument:

[efg@fedori dir1]# ls -lia /home/efg
1966452 drwx------ 5 efg efg 4096 2010-08-13 21:22 .
172034 drwxr-xr-x 13 root root 4096 2010-08-13 08:56 ..
1966461 -rw------- 1 efg efg 109 2010-08-13 08:10 .bash_history
1966457 -rw-r--r-- 1 efg efg 18 2010-08-13 08:10 .bash_logout
1966455 -rw-r--r-- 1 efg efg 176 2010-08-13 08:10 .bash_profile
1966453 -rw-r--r-- 1 efg efg 124 2010-08-13 08:10 .bashrc
1966460 -rw-rw-r-- 1 efg efg 0 2010-08-13 08:13 dir0
1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

More Surprises ! Who created all those files that begin with a dot?. Our investigation takes a new turn,
looks like this case will take sometime to solve. -Don't worry! Its not a judical enqury, so we will have verd
ict soon :smile:

Let's focus on how "." and ".." files created under dir1 first and we can check others file, a little later.


1966463 drwxrwxr-x 2 efg efg 4096 2010-08-13 08:15 dir1

this entries inode number is same as "." file under "dir1". So the file /home/efg/dir1/. denotes the directory
itself.And Just look above under "ls -lia /home/efg" here we have inode number same as /home/efg/dir1/.. - so initia
l verdict is,all directories have two files named "." and ".." where "." refers to that directory itself and ".." refers to
its parent directory.

Absolute and Relative path names

These ('.' and '..') are used for relative path names, so far we have used absolute path names.
Absolute path names begin with / (root) example : /home/efg/dir1
Relative path names begin with "." or ".." or without "." or ".." or "/"
Check your current path - using pwd command it will tell its "/home/efg/dir1"
you can access dir0 file using relative path like,


[efg@fedori dir1]$ls -l ../dir0

You can access files under current directory using "."

since we don't have any files here,first create a file using touch

[efg@fedori dir1]$touch giis.txt

and access it using

[efg@fedori dir1]$ls -l ./giis.txt

here is the final relative path method,which won't use . or .. or /

simply call,

[efg@fedori dir1]$ls -l giis.txt

This marks end of Day-1. Now feel free to learn Day-2 today itself or go to bed or go back to windows :smile:

Posts: 1

Participants: 1

Read full topic

About the commandline category

0
0

@laks wrote:

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won't appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?

Posts: 1

Participants: 1

Read full topic

About the Bash category

0
0

@laks wrote:

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won't appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?

Posts: 1

Participants: 1

Read full topic

Linux Command-line Journey Day-2

0
0

@laks wrote:

We will continue Day-2 of our Linux Command-line journey.

  • Understanding and manipulating file contents:
  • Understanding cat command
  • Input/Output Redirection
  • Head and tail of file

Understanding and manipulating file contents:

Okay, now lets understand the dot "." files under /home/efg namely ".bash_history" ,
".bash_logout" , ".bash_profile" , ".bashrc".

Go back to /home/efg using command

[efg@fedori dir1]$cd ..

Did you understand what above command really mean? ...what? NO? then re-read topic
"Move into a directory".Then Came back ..I'll count till 10000 and wait for you.
10000,9999,9998...3,2,1,0. okay.Now lets first understand ,.bash_history ,file is used
to keep track of shell commands. You can view the content of file using "cat" command.

[efg@fedori ~]$cat .bash_history

will display commands and everything you have typed in shell prompt. "history" command used
to read this file and display it on screen.

[efg@fedori ~]$history

It can used by root user to keep track of what you are doing.. I can hear you "aaawk!! how to remove
this feature? Can I delete this file?" yes you can delete this file using the "rm" command.

[efg@fedori ~]$rm .bash_history

This will prompt you for confirmation. Since any deleted files can't be recovered, unless you have
fail-safe tools or hoping that FS didn't remove its final references from the journal log.

But the sad news is, this bash_history will be created automatically whenever you type something into
bash prompt. But this file is useful, we will see that in next section.

".bash_profile" , ".bashrc" files have environmental variable specific to this user. Say for example,
you want to change bash prompt


[efg@fedori ~]$

to it look like root# - so that you can fool your friend, saying you got root access. So edit your
.bash_profile and add following line -


export PS1="root#"

How to append this entry to file .bash_profile? Read on...

Understanding cat command:

we have used "cat" command view the content of .bash_history earlier.Lets understand some basic terms of GNU-L
inux.

your kepboard (or anyother input device) is referred as STDIN - standard input device,
your screen (or anyother output device) is referred as STDOUT - standard output device,
finally normally screen is also referred as STDERR- standard error device.

by default cat reads from stdin and writes into stdout device. (ie) it reads from your keyboard and write into
screen.

To verify this simply type cat,


[efg@fedori ~]$cat

now type something as input (cat is reading from keyboard) and press "Enter" key,it will display whatever you
typed on the screen itself,which is your
default stdout.

Did you now realize what's stdio and stdout? so now type something like junk word m5do5(btw, to come out of "c
at" command press "ctrl" and "d" simulateously -which
tells the cat command - end of input,take me back to bash prompt) ...were where we? at some junk m5do5 :smile:


[efg@fedori ~]$ m5do5
bash: m5do5: command not found

Error message is displayed on default stderr ,which is your screen.

So when you said,"cat .bash_history" ,you are telling cat command ,"hey cat,this time ,treat given file as std
in".
and cat did the same,it read your file content and displayed on stdout.

Input/Output Redirection

you can also override stdout,and ask "cat,please treat file1 as my input device and redirect the output to fil
e2 instead of default stdout,which is screen."

">" is called redirection operator for output stream.


[efg@fedori ~]$cat .bash_history > history_file.txt

">" refers to append the content to existing file.

So to append the environment variable,

[efg@fedori ~]$echo "PS1=\"root#\"" >> .bash_profile

above command will set the enviromental files. echo command by default,displays output in stdout,we told echo
to redirect it to file.
\ is used here as an escape charater.we will see about escapte character,little later.

Check your file .bash_profile ,the last line would be PS1="root#" using cat command,but wait there is two othe
r common method to view content of file.

head & tail of file


[efg@fedori ~]$head .bash_history

head command will display ,first 10 lines of a file ,if you want to change number of line ,pass and option "-4
" .

[efg@fedori ~]$head -4 .bash_history

will display first four lines from the file.

tail,You guessed it correctly, is exact opposite of head.it will display last 10 lines and you can manipulate
it by passing the number,


[efg@fedori ~]$tail -1 .bash_history
PS1="root#"

Now logout and re-login again

[efg@fedori ~]$logout

Now provide your username & passwd. Eureca ! you will see this-
*
root#
*
".bash_logout" can used to have commands that ends to be executed when you log off. How about putting the comm
and to delete .bash_history in .bash_logout?

wait,.bash_history or history command is indeed useful one.

When you run history command ,it will display commands along with numbers on its right. If you want run one of
those commands again,you can simply using

[efg@fedori ~]$!1

will the run command show by:

[efg@fedori ~]$head 1 .bash_history

That's it for today. Its a short day. see you on Day-3

Posts: 1

Participants: 1

Read full topic

Linux Command-line Journey Day-3

0
0

@laks wrote:

Today Day-3, we will walk through file related stuffs.

  • More File operations
  • Taking file backup with cp
  • Verifying file integrity
  • How to move file from one location to another?
  • Protecting files with chmod

More File operations:

Now lets start manipulating files itself,rather than its data.

Taking file backup with cp

How to create copy of the file?,Say,we need to take a backup of .bash_history file from your home directory to
another directory dir1.

[efg@fedori ~]$cp .bash_history dir1

now verify the dir1 contents -

[efg@fedori ~]$ls -la dir1

You should find the file now it two places one under your home directory and another under "dir1".

Verifying file integrity

Seems to be there but how can we 100% sure ,that file integrity is not compromised?
you can use 'head' and 'tail' to verify few starting and ending lines,if they are fine - we are okay!.
what if something in middle got messed up? but wait they universally (yeah,even martians use this trick) acce
pted
method. Use md5sum.
`
[efg@fedori ~]$md5sum .bash_history
8599e55388ecef39d8905b1c67b044ef .bash_history

[efg@fedori ~]$md5sum dir1/.bash_history
8599e55388ecef39d8905b1c67b044ef dir1/.bash_history
`
As you can see both numbers/checksum are same.Thus we can be sure both files are same.

How to move file from one location to another?

you can simply move a file to another directory

[efg@fedori ~]$mv giis.txt dir1

now verify the dir1 contents -

[efg@fedori ~]$ls -la dir1

Since we moved (not copied) this file will be available only one location which is "dir1".

Here is a question for you,If you copying 10MB file into a directory and also moving (mv) 15GB file into a dir
ectory "dir1".
Which operation will complete first ? is it 10MB copy? or 15GB move?

Protecting files with chmod:

As we seen above,someone can use cp and mv command to copy/move your files to another location.How to prevent
,someone from accessing your files?
Remember 'ls -l' which we used long long ago? yeah,use it again.
`
[efg@fedori ~]$touch secretfile

[efg@fedori ~]$ls -l secretfile
-rw-rw-r--. 1 efg efg 0 2011-05-04 22:40 secretfile
`
As you can see owner and group is given read/write permission and 'others' given read-only permission.
Clearly other can't write something into your 'secretfile' since 'w' is missing for others.
but they can copy your file to some other location. In order to avoid that ,we can use 'chmod'.


[efg@fedori ~]$ chmod o-r secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-rw----. 1 efg efg 0 2011-05-04 22:40 secretfile

o stands for others and '-r' means remove read permission.
If you want to remove rw from groups too,then use 'g' followed '-rw'


[efg@fedori ~]$ chmod g-rw secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile

If you are someone like "Leonard" (hero from memento) ,and you want to extra careful ,even you do not to mess
up with it ,
then you yourself can remove its write permission like -


[efg@fedori ~]$ chmod u-w secretfile
[efg@fedori ~]$ ls -l secretfile
-r--------. 1 efg efg 0 2011-05-04 22:40 secretfile

After some time (15 minutes later :D) , suddenly realize you got to modify the this file,so need to add 'w' to
yourself.
When removing permission we used '-' ,now we want to add them so whatelse the symbol would be other '+'?

so do it like -

[efg@fedori ~]$ chmod u+w secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile

Links
=====
For example ,you created a new directory named "dir2"

[efg@fedori ~]$ mkdir dir2

and want to make sure that 'secretfile' is accessible from both dir1 and dir2.
(though secretfile has 0 size ,just assume it has around 5GB data thus size=5GB)

If you cp the 5GB secretfile to dir2 and dir1.On the whole it consume 15GB (5 each for secretfile at home,dir1
and dir2)
Since file contents are same, its not a good idea to let them occupy 15GB.
Here is where links will be useful.How about creating links from dir1 and dir2 instead of cp them?

[efg@fedori ~]$ ls -lt secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile

links are created by ln command like -

[efg@fedori ~]$ ln secretfile dir1/secretfile

check the dir1 contents -

[efg@fedori ~]$ ls -lt dir1/secretfile
-rw-------. 2 efg efg 0 2011-05-04 22:40 dir1/secretfile

yeah,we have it.

[efg@fedori ~]$ ls -lt secretfile
-rw-------. 2 efg efg 0 2011-05-04 22:40 secretfile

now wait a second , rewind 10 lines - check the first ls
it shows "1" before owner "efg" - right? But now check the last ls output
it shows "2". Why?
That's because secretfile is being linked from two location now after ln command.
One from original home and another from dir1.

So now if we create another link to dir2 - like

[efg@fedori ~]$ ln secretfile dir2/secretfile

Now the link count increased by 1,its new count 2+1 which is ..hmmmm...where the heck is my calculator ?!
yeah..got it..its 3.So the ls should shows as 3.


[efg@fedori ~]$ ls -lt dir2/secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 dir2/secretfile
[efg@fedori ~]$ ls -lt dir1/secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 dir1/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 secretfile

and it does :smiley:

Now If you change the secretfile at home ,this will be reflected at dir1 and dir2.
For example , I'll append some data


[efg@fedori ~]$ echo "This is some random data for the file" >> secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 3 efg efg 38 2011-05-04 22:42 secretfile

Okay,now go and check dir1 and dir2 ,
`
[efg@fedori ~]$ cat dir2/secretfile
This is some random data for the file

[efg@fedori ~]$ cat dir1/secretfile
This is some random data for the file
`
Since all the links has single copy of the file,whatever you do via one link will be reflect via remaining lin
ks too.
Similarly , if you change from dir2 it gets reflected at dir1 and home and so on.

Say if you delete the file from dir1 ,the count will decrease by 1 and count will be 2.

[efg@fedori ~]$ rm -rf dir1/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 2 efg efg 38 2011-05-04 22:42 secretfile

true.
and If you delete it from dir2 too,then count will become 1.


[efg@fedori ~]$ rm -rf dir2/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 1 efg efg 38 2011-05-04 22:42 secretfile

true again
If you delete it from home ,then file will be gone forever.

Above linking is known as "hard link". There another linking method known as "soft link".
The major different between the two is , hard link is twice as heavy as soft link :stuck_out_tongue: ..hehe just kidding.

Lets begin with an example for soft link,


[efg@fedori ~]$ ln -s /home/efg/secretfile dir1/sym_file

we used the same ln command but added an option "-s" to create symbolic link or soft link.

Here is the dir1 contents , notice the first letter 'l' before permissions ,which refers to the file named "sy
m_file" is soft link.


[efg@fedori ~]$ ls -l dir1/
lrwxrwxrwx. 1 efg efg 21 2011-05-07 22:52 sym_file -> /home/efg/secretfile

And also note that secretfile's link count didn't increase to "2".

[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 38 2011-05-06 09:27 secretfile

Though, you can access the file ,modify it via "sym_file", removing "sym_file" won't remove the original file.
What i mean,is

[efg@fedori ~]$rm -rf dir1/sym_file1

won't affect secretfile by any means.

But if you delete the original file first ,then "sym_file" is known as "broken link" since it points to a file
which does not exist.

Posts: 1

Participants: 1

Read full topic


Linux Command-line Journey Day-4

0
0

@laks wrote:

Today will have rather short Journey on Day-4.

  • Locate or find a file
  • Which one is that?
  • Search for a text inside a file or files
  • Counting the impossible
  • How to sorting file contents

Locate or find a file :

I have created a file named "hideme" , but I don't where resides now.How to locate it?
fastest way is to use "locate" command,


[efg@fedori ~]$ locate hideme
/home/efg/dir1/hideme

another way is to use a 'find' command,

[efg@fedori ~]$ find /home/efg -name hideme
/home/efg/dir1/hideme

which scans the directory /home/efg for file 'hideme'.

the difference between locate and find is that,locate command maintains database(which was created by updatedb
),it actually check that database for file.
updatedb is command which runs from time to time and update filename and its path on the database which was la
ter used by locate command.

But find command doesn't use any database,It simply the scans the directories everytime.
This is very basic usage of find command,but its very powerful command,next section(advanced find) deals with
it.

which one is that?

If you have multiple binary installed on different path.I mean,for example,assume you have php5 installed on /
usr/bin and php4 installed on /usr/local/bin.


[efg@fedori ~]$ whereis php
php: /usr/bin/php /usr/local/bin/php

below command uses php to print "Hello world".

[efg@fedori ~]$ php -r 'echo "Hello World\n";'
Hello World

How to find out which get executed when your simply typed "php".

to figure out that we can use,

[efg@fedori ~]$ which php
/usr/local/bin/php

thus when you simply use "php" it invokes binary from /usr/local/bin.

How which command works?.Remember in our first section, when we talked about PATH environmental,

[efg@fedori ~]$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin

which command uses above search path.Though we have php on /usr/bin and /usr/local/bin ,since /usr/local/bin c
omes before /usr/bin,
that binary got executed.

Search for a text inside a file or files:

So now we know, how to find a file. But how to find a whether specific text available on a file or not?

For example,secretfile contents is -

[efg@fedori ~]$ cat secretfile
This is some random data for the file

In order to find whether the string "data" available on that file. we can use grep command.
grep command is used to search for given pattern on a file,If the pattern is found,it will print that whole li
ne.

[efg@fedori ~]$ grep 'data' secretfile
This is some random data for the file

To understand more about grep , we need to add more data to secretfile.

[efg@fedori ~]$ cat >> secretfile
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
^D

Lets check the contents again -

[efg@fedori ~]$ cat secretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..

Lets try the same grep command , with option -n which display the line number too.

[efg@fedori ~]$ grep -n 'data' secretfile
1:This is some random data for the file
3:Note that we used >> to append data to the file.

Suppose we want to print all lines except the line containing the word 'data' - Use v , invert option like-

[efg@fedori ~]$ grep -v 'data' secretfile
This will go as second line.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..

grep has lot of option like printing lines that begin with specific text or ending with specific text etc.
check out 'man grep' or 'info grep' for all available option.(huh...Now i can move on ,If someone messes with
grep command,
i can blame him for not reading man or info pages :wink: )

Counting the impossible

Here is content again, the task is to count the number of lines it has ,

[efg@fedori ~]$ cat secretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..

5? thats Right.pretty easy one. How about counting number of words ? thats slightly difficult. Then how about
counting number of characters ? Thats even harder. But wait there is one easy way to complete this task.
Just two word command named 'wc'.

[efg@fedori ~]$ wc secretfile
5 44 218 secretfile

secretfile has 5 lines , 44 words and 218 characters. Feel free to validity the result :stuck_out_tongue:

How to sorting file contents:

I'm not going to say anything about this sort command - you should be able to figure out yourself.
You know original content of secretfile ,this is what i get when used sort .

[efg@fedori ~]$ sort secretfile
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..
Note that we used >> to append data to the file.
This is some random data for the file

Okay we are done! short day, isn't it ?

Posts: 1

Participants: 1

Read full topic

Linux Command-line Journey Day-5

0
0

@laks wrote:

Lets begin Day-5,

  • How to identify file type without opening?
  • First cut it then paste and finally fold a file
  • FileSystem hierarchy structure
  • Split one big file into multiple small files
  • How to check for free disk fedori?
  • Remember this !!!!
  • How long the machine is up and running?
  • Who is it?
  • What's your hostname and version

How to identify file type without opening?

Do you remember ,often you get some junk mails , claiming they have sent you a 'special' attachment.
You have downloaded it but not sure whether to open it or not,because your afraid of virus (wait...virus? they
are irrevelant with linux ..lets
try another example).

hmm..you have downloaded a file named "something.pdf" but when you tried to open with pdf reader ,it didn't op
en.
what to do now?the best way is to check to the file type before opening or playing them using "file" command.

Here comes few examples -

`
[efg@fedori C-pdf]$ file Glade-Tutorial.pdf
Glade-Tutorial.pdf: PDF document, version 1.4

[efg@fedori ~]$ file MalgudiDays.mp3
MalgudiDays.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1,
128 kbps, 44.1 kHz, St ereo
`
[OT, did you heard about "Malgudi Days" by R.K.Narayan,My favorite one is "A Tiger for Maigudi" a story from t
iger's point of view. :smiley: ]


[efg@fedori ~]$ file extlib.ext3
extlib.ext3: Linux rev 1.0 ext3 filesystem data

file command don't use extention part to determine the file type. For example, we created dummy file named "e
xt2fs.pdf" like


[efg@fedori ~]$ echo "this file contains some random text" > ext2fs.pdf

we can't determine the file with 'ls' output -

[efg@fedori ~]$ ls -ltr ext2fs.pdf
-rw-rw-r--. 1 efg efg 36 2011-05-08 13:32 ext2fs.pdf

Now lets try 'file' -

[efg@fedori ~]$ file ext2fs.pdf
ext2fs.pdf: ASCII text

As you can see ,file says just a "ASCII text" file not pdf.

Its always better to verify the downloaded file type before running or opening them.

First cut it then paste and finally fold a file:

From our secretfile,if you want cut first column then try

[efg@fedori ~]$ cut -f1 -d' ' secretfile
This
This
Note
If
I'm

option f1 means cut first column and d refers to delimiter. you want to cut second column use -f2 instead of -
f1.

I created two files (1.txt and 2.txt)

[efg@fedori ~]$ cat 1.txt
file1: line1
file1: line2
[efg@fedori ~]$ cat 2.txt
file2: line1
file2: line2

In order to merge the lines of these two files we do paste -

[efg@fedori ~]$ paste 1.txt 2.txt
file1: line1 file2: line1
file1: line2 file2: line2

finally if we use fold, it will wrap each line to specified width,

below will wrap the text to 5 characters per line.

[efg@fedori ~]$ fold -w 5 1.txt
file1
: lin
e1
file1
: lin
e2

FileSystem hierarchy structure

Now its time to understand linux filesystem hierarchy structure.Its not hard, good news is you already learned
them without realizing it :).
Remember , when you first logged in ,it checked /etc/passwd file and finds the entry "/home/efg" ?

Normally Configuration files go to /etc directory. and all Users home directory will be /home/user-name except
admin's home which is /root.

And also remember when we searched for commands location with $PATH which gave something like-
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/efg/bin

/usr/local/bin and /usr/bin will have user commands/appliations.
/usr/sbin will contain root user commands.

Here is more -

/tmp - any temporary files created and deleted in memory RAM. If you create some file
at /tmp directory and reboot the machine.These files won't be there when the machine
comes up.
/var - will contain log files
/proc - Real time process information stored by kernel.
/dev - will contains device files
/boot - this directory content is used for booting process.
/lib - contains system library files and kernel modules
/opt - third party softwares can be installed here.
/root - again, system administrators home directory. (try ls /root and check out your admin's
secret contents :D ,if possible. )
/mnt - where external devices can be mounted.

All above directories are subdirectory of / - the root directory

Split one big file into multiple small files :


[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 218 2011-05-08 12:27 secretfile

the file has 218 bytes as size. If we want to split this file to 2 files of 109 bytes each with command like
-

[efg@fedori ~]$ split -b 109 secretfile
[efg@fedori ~]$ ls -l
If you do a 'ls' you will find two new files with perfix 'x' with 109 bytes each -
-rw-------. 1 efg efg 218 2011-05-08 12:27 secretfile
-rw-rw-r--. 1 efg efg 109 2011-05-16 10:04 xab
-rw-rw-r--. 1 efg efg 109 2011-05-16 10:04 xaa

Lets check out their contents


[efg@fedori ~]$ cat xaa
This is some random data for the file
This will go as second line.

Note that we used >> to append data to the

second file content is -

[efg@fedori ~]$ cat xab
file.

If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..

as you can see secretfile content is now placed in two files.This split command will be very useful when you w
ant to
transfer one big file from one machine to another.

Anybody has questions ? No ..oh..yeah someone there thinking how to merge these contents again? something like
'merge' command?
hmm..good question..but wait..I just checked 'man merge',it won't fit into this.
the simple way is to use cat and append files , like

[efg@fedori ]$ cat xa* > newsecretfile
[efg@fedori ]$ cat newsecretfile
This is some random data for the file
This will go as second line.
Note that we used >> to append data to the file.
If you use > ,it will overwrite any existing contents.
I'm going to end this by pressing ctrl-D now..

that's it.

How to check for free disk fedori?

On windows , probably you mighti do somethin like 'right-click' on D: or C: and choose properties and there yo
u can see the size.
From linux command line is a matter of just two letters - yeah 'df'

[efg@fedori ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 140G 123G 9.6G 93% /
/dev/sda5 194M 100M 85M 55% /boot

As you can see ,I have used upto 93% of root partition and boot partition occupies 55%.
I have passed -h option to df command to make it more readable.

In order to check for free fedori on RAM, do run "free" command.

Remember this !!!!

No one (no not even linus :smiley: ) would remember all options of all commands.So try and make some guess for examp
le

-h may be human readable format.
-a may be run all
-r may be recursive
-i may be ...what ? hmm..install or interactive
-s may be size
-b may be block
-d may be directory
-l may be links
-v may be verbose or version

So If first-time interviewer asks you about 'how do you do that with command X' - Tell him bluntly, "First che
ck man commandname and then use it.No one can remember all options of commands."

How long the machine is up and running?

uptime gives you the answer.


[efg@fedori ~]$ uptime
20:30:50 up 21 min, 3 users, load average: 0.24, 0.07, 0.09

First column denotes current time , next field shows its running for "21 minutes" (yeah...Just came back from
office and start the machine)
and the 3 users currently logged in. and approximate load average of the system in past 1,5,15 minutes.

Who is it?

I like this song from MJ :D..no not that,I was thinking about "how to find out 3 users reported by uptime outp
ut."
and the command is ..who..whoo..who..who (I like that song too :smile: )


[efg@fedori ~]$ who
efg tty1 2011-05-17 20:10
efg pts/0 2011-05-17 20:11 (:0.0)
efg pts/1 2011-05-17 20:15 (:0.0)

It was me ,opened up different terminals.Not a intruders.
there is one more way for doing the same - use 'w'

it combines the output for uptime and also lets us know what each user currently doing

[efg@fedori ~]$ w
20:41:18 up 31 min, 3 users, load average: 0.08, 0.16, 0.14
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
efg tty1 - 20:10 30:42 0.04s 0.01s /bin/sh /usr/bin/startx
efg pts/0 :0.0 20:11 3.00s 2.90s 7.81s gnome-terminal
efg pts/1 :0.0 20:15 0.00s 0.04s 0.00s w

What's your hostname and version


Okay,So far you have done all this on which machine and what OS or kernel version ?
Yeah..Its was pretty late, we didn't think about it till now , right? Anyway its better late than never.

To find hostname

[efg@fedori ~]$ hostname
fedori

to find OS and kernel details run :

[efg@fedori ~]$ uname -a
Linux fedori 2.6.32.21-168.fc12.x86_64 #1 SMP Wed Sep 15 16:12:07 UTC 2010 x86_64 x86_64
x86_64 GNU/Linux

I'll let you to decode that uname output. Check man uname for any help.
So far we talked about file related stuffs mainly,next we will focus on process.

Posts: 1

Participants: 1

Read full topic

About the mysql category

0
0

@laks wrote:

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won't appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?

Posts: 1

Participants: 1

Read full topic

A Quick mysql tutorial in 7394 hours!

0
0

@laks wrote:

A Quick mysql tutorial in 7394 hours!

So you are one of those tranditional/rigid user who believe
in SQL and arguing/fighting against NOSQL?

Welcome to a Quick mysql tutorial in 7394 hours!!
(if you don't know mysql and computer and english.)

yeah, recenly conducted bogus scientific research results show
that you'll need approx.7394 hours to complete this tutorial
especially if you don't have any clue about what's mysql and
computer and english!

Lets login to mysql client(use your loginname and database below),


mysql -u yourname -p yourdatabase

Now press Enter key then provide password and see what happens

mysql

Did it say?

Welcome to the MySQL monitor
and more info like mysql version and others?

Now you are logged into interactive mysql prompt.

Lets first create a database: but wait, before that if you don't know what's a
database. Assume database as directory or folder where you store critical
information in a specific format.
you can simply use


create database universe;

will create it.(you can't run create database
command, since we already created one just for
you!..ok..ok..stop clapping your Hands)

Now inorder to use that database; type


use yourdatabasename;

use

Did it say?

Database changed

great!.You are ready to record some
data into this database.

To store information,you must provide
a name and format.This is known as
table. To create a table named 'planets'
type,


create table planets(name varchar(15),position int, has_moon boolean);

Press enter key it should display
Query OK, 0 rows affected

create

Congrats! you successfully created table
with format.

To see list of tables available on this
database type

show tables;

show

Wait a second, let me guess .hmm..

thinking

It should have displayed something like


+-------------------+
| Tables_in_universe|
+-------------------+
| planets |
+-------------------+

right?,yes I know I know , I have IQ 180!
(+ or - 170 :P)

to view the format/structure of this
table.do type,

describe planets;

describe

Did you see the following?

+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| name | varchar(15) | YES | | NULL | |
| position | int(11) | YES | | NULL | |
| has_moon | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

This is what we used in create table command
right?If you find anything missing report a
bug to mysql community :stuck_out_tongue:

So we created a 'directory/folder' (read as database)
and 'filename' (read as table) with specfic
'extention' (read as format).Now its time
to 'write' (read as insert) some 'data' (read as record).

Lets first add our home (earth).We know its
at third position from sun and has a moon.

insert into planets values ('earth',3,1);

insert

Now lets add our possible future home,


insert into planets values ('mars',4,2);

mars has 2 moons ! Just take a break, imagine
how is it to live in a place with two moons! :smile:

yes,that would be cool :wink:

okay,now we wrote two records into the table.

To retrieve these data from table,type


select * from planets;

select

it shows


+-------+----------+----------+
| name | position | has_moon |
+-------+----------+----------+
| earth | 3 | 1 |
| mars | 4 | 2 |
+-------+----------+----------+

great!
(still dreaming about 2 moons sight? get
back to earth!)

Lets assume,you retrieve only those
record which has only one moon.For this
just append where condition to above
select query.

select * from planets where has_moon=1;

we can modify the above query a little bit more,
we know we are selecting records with has_moon=1,
so ignore that field. show the rest of columns.

select name,position from planets where has_moon=1;

See - Its simple!. Now do

update planet;

update

that should have displayed some error message.

Because you (yes,its you not my fault :P) didn't
tell 'what' column to update and 'where' to
update.

OMG,we entered some wrong data for earth,It appears recently
it moved to 2 place in our solar system! need proof? go
outside at 13:00.So we need to edit that record. How to do
that? Here comes 'update' to your rescue.


update planets set position=2 where name="earth";

Now go and view results again via select query.
Now its time to trash something.Lets delete mars!

Say good bye to "Spirit".We going to destory it.


delete * from planets where name="mars";

delete

RIP Spirit (2003-2013)

Thats it for a short tutorial on mysql!
Stayed tuned for more and play around
with mysql commands.!

Happy learning, No hacking please :smiley:

Posts: 1

Participants: 1

Read full topic

DIY : Create Linux User by Hand

0
0

@laks wrote:

You know how to create Linux user account with useradd and passwd.But do you know how to create it without using them? No, you can't adduser which is nothing but an symlink to 'useradd'

Why we need this? We believe Linux System-Admin should posses in-depth knowledge on things, rather than just depending on few binaries! So If you want to know what's going on behind the scenes. Here we go: Our task is to create user named giis and setup password. Lets first update the /etc/passwd file with below entry

echo "giis:x:25000:25000:Giis:/home/giis:/bin/bash" >> /etc/passwd

As you can see there are 7 fields separated by : where each field refer to
`

Field1: giis - Login name
Field2: x - Password place holder
Field3: 25000 - Unique User id
Field4: 25000 - Group id
Field5: giis - Real name
Field6: /home/giis - Home directory
Field7: /bin/bash - Shell type
`
The summary of this entry would be - after verifying password for user giis in the /etc/shadow file, allow user giis to login and place him under /home/giis with bash as the shell type. For a valid login, these fields must be sane. Above entry takes care of Field 1,3,5 and 7. We need to restore sanity for Field 2, 4 and 6.

We can setup Field-4 (group-id) by creating group-id under /etc/group file like

echo "giis:x:25000" >> /etc/group

Above places an entry into group file which assigns, group name giis with Group-Id (aka gid ) 25000 Now we need to bring some sanity to field-6 (home-dir) by running a command :

mkdir /home/giis

which creates home directory and we set its permission appropriately with chown
`
chown giis:giis /home/giis


As you can see below, new created home directory has rwx for own giis and non-writable to group/others.

# ls -ld /home/giis drwxr-xr-x. 2 giis giis 4096 Sep 25 22:38 /home/giis
`

Finally we take care of field-2. It's little complex than others.For any valid user password, it needs to be stored in encrypted format. Lets use below python statement to create a password secret897 and encrypted with sha256 algorithm using salt $6$salt1234 The end result will be encrypted string of secret897 with slat, which will be stored on /etc/shadow file. We stored encrypted password on temporary shell variable named pass

pass=
python -c 'import crypt; print crypt.crypt(\"secret897\", \"$6$salt1234\")'

change the permission of shadow file as writable by root

chmod 600 /etc/shadow

and just append the encrypted password along with username giis into the shadow file

echo "giis:${pass}:::::::" >> /etc/shadow

then revert the permission

chmod 000 /etc/shadow

Now its time to log into the newly created account giis with password secret897 , simply type
login It should prompt for your username and password, then place you under /home/giis with bash prompt like bash-4.2$ Now verify your login identity using whoami and home directory using pwd
Everything Fine! right?

Posts: 1

Participants: 1

Read full topic

How to prevent accidental or intentional file deletion?

0
0

@laks wrote:

Lets create a secret file with top secret content

echo "Earth is our home!!!!" > secretfile.txt

Now some intruders got root access wants to destory
your secretfile with simple command like

#rm -f secretfile.txt

how to prevent him? . Though we can't him completely,
we might make him to think for a moment before he
realizes what going on. If he is an average intruder,
he may give up too :smiley:

What we need to do - is set an immutable flag like

#chattr +i secretfile.txt

Now even he tries to delete that file as root, he will
get

# rm -rf secretfile.txt
rm: cannot remove
secretfile.txt': Operation not permitted

message. Probably he might do an 'ls -l' command to
check everything is fine or not. But he will see

# ls -l secretfile.txt
-rwxrwxrwx. 1 root root 18 Sep 12 13:33 secretfile.txt

Permission looks fine, but owner might have set immutable flag on it.
If he is an well-experienced hacker, he might guess 'immutable'
flag and run

# lsattr secretfile.txt
----i--------e- secretfile.txt

then think, "hmm, idiots ,they think simple immutable flag can prevent
me? aha" and removes the flags within 5 seconds with a command like

# chattr -i secretfile.txt

and does

#rm -f secretfile.txt
`
There goes our file :frowning: but hacker will be happy :smiley:

Posts: 1

Participants: 1

Read full topic

How to change sshd port and do it in wrong way?

0
0

@laks wrote:

Its a matter of two step process! First

Step 1) You need to have root (or sudo) permission to change/edit this file


/etc/ssh/sshd_config

Step 2) As you know sshd default listens on port 22, so find the line


#Port 22

and replace the port number to something else


#Port 12345

That's it . right ?

Now login to your server with above port


ssh -p 12345 user@serverip

You must get an error message like


ssh: connect to host serverip port 12345: Connection refused

Why it didn't work ?

There are two mistakes we did while changing /etc/ssh/sshd_config file.

Mistake 1) We forgot to un-comment the line


#Port 12345

Every line beginning with # will be treated as comment. So you must un-comment that line first


Port 12345

Mistake 2) We didn't restart the sshd service. You must restart the sshd server to ensure these new changes are applied.


service sshd restart

And now login to your server with above port


ssh -p 12345 user@serverip

It should prompt you for password..

Posts: 1

Participants: 1

Read full topic


How to compile C code and execute it?

0
0

@laks wrote:

If you know vim/vi editor open a hello.c and save
this content

int main(){
printf("hello linux world!");
return 0;
}

If you don't know how to use vi/vim editor - just type


cat > hello.c (now start typing about program and press ctrl+d to save and quit)

Now you have your first C code. Just try to compile with

gcc hello.c

It will create a filename a.out - To run the binary do

./a.out

! got it?

If you want to create binary with different name just use -o option during compilation,
like

gcc hello.c -o hello

Now you will have a binary named 'hello', execute it as

./hello

Posts: 3

Participants: 2

Read full topic

About the Admin category

0
0

@laks wrote:

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won't appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?

Posts: 1

Participants: 1

Read full topic

How to install GNU C-compiler?

0
0

@laks wrote:

If you use rpm based distro like Fedora/Centos/RHEL,


yum -y install gcc

On dpkg based distro like Ubuntu/Debian/Mint


apt-get install gcc

That's it. Now verify the gcc version using command

gcc --version

Posts: 1

Participants: 1

Read full topic

How to write simple while loop in bash?

0
0

@laks wrote:

 COUNTER=0
 while [  $COUNTER -lt 10 ]; do
     echo The counter is $COUNTER
     let COUNTER=COUNTER+1 
 done

We initialize the loop counter on line-1 and increment the counter on line-4. The echo statement is executed until the the while loop condition is satisfied. (i.e) counter less than 10.

Posts: 1

Participants: 1

Read full topic

Linux Command-Line Journey Day-6 (Process)

0
0

@laks wrote:

Linux Process Basic commands

So far,we have seen about file related commands. Lets start with
process-related command.

What's a process? Wiki says "In computing, a process is an instance of a computer
program that is being executed."

Lets talk about Linux process and its commands.

Can you find name of this system? Without looking bash prompt!?
Okay, I know you looked at the prompt and figured out its named
'fedori'. The proper way is to use

hostname

Go ahead and type it. Did it display

*fedori*

again?

Great!. You will be thinking "Wait a second, we wanted to discuss about
process not about hostname of system."

Yes, I agree with you. But again, how did you got the hostname?
Angry response will be hostnameto be more precise /bin/hostname
file.

Let me ask one more question, /bin/hostname is a file or process?
Of course its a file, always remember In Linux everything is file.

Its a special file, go ahead use the 'file' command you learned in
previous lessons.

file /bin/hostname

It tell you something like

*/bin/hostname: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
*

Its a ELF 32-bit executable file. We ignore rest of the output.
So this /bin/hostname is not a process? Yes, its just an executable file.

When we say its executable file. /bin/hostname contains instructions
stored in a binary format. These instruction will be Kernel to do some
task or action.

Lets go ahead view those instructions /bin/hostname. Go ahead (never be
afraid of Linux commandline)

cat /bin/hostname

We got combination readable and unreadable characters. Somewhere within
those output, we are telling Kernel to read a file name '/etc/hosts'
and search for entry '127.0.0.1'.(which typically points to host machine
name) and display that content.

When we type an executable '/bin/hostname' on bash prompt, Its content
whatever you saw few lines above is loaded into RAM. Kernel follows those
instruction and takes action.

Finally, here's our definition of a Process.

"Process is nothing but a  file-content which is residing in RAM"

Now lets revisit the Wiki :

"In computing, a process is an instance of a computer program 
that is being executed."

Here, an instance of a computer program means its residing in RAM
and being executed means Kernel follows its instruction.

I hope it makes some sense now.

Our 'fedori' has lot of Process executed by Users or by Kernel itself.
Each process has different states. Let's display some process currently,
residing at RAM.

To view them we can use command called 'ps'. Its similar to 'ls'
except that ls will show files on disk. 'ps' will show files on RAM

ps

Gave us below output. Lets spend sometime on these.

*`
 PID TTY          TIME CMD
27447 pts/9    00:00:00 bash
29731 pts/9    00:00:00 ps
`*

Understanding more about process:

It has 4 field PID,TTY,TIME,CMD

Lets start with easy one,

CMD as name indicates, its tells what's the executable file
name currently residing in-memory. Its has unique id,which is called 'pid'.

PID - Each process has unique number assigned to it. Since Kernel needs to
know 'who' is responsiable for these instruction.

Why can't Kernel get this information from CMD itself? think about it.
We will discuss answer little later :smile:

TTY - Its terminal allocated to the process. If Kernel needs to ask something
or print something it will use this terminal for this process.

Finally,
TIME - It refers to how time CPU took to execute this process's instruction.
Its denoted in [dd-]hh:mm:ss format

We have two process with id 27447 and 29731.

Parent process:

We saw 'hostname' was loaded into RAM and executed by Kernel. But who instructed
Kernel to find and load '/bin/hostname' into RAM in the first place?

There must be some process sitting already in RAM, waiting for you
to type characters and press 'Enter' key. right? So only job of
this guy is to watch for any input (on TTY) and analyze and tell Kernel whether,
Kernel needs to take any action on the input request.

In real world, this looks like a 'Manager' job. Who wants other to do the task.Its like,
BOSS telling, "My work is wait for some work to arrive, when it arrives I'll assign to
my employee/programmer to complete that task and report it to me" :stuck_out_tongue:

In this case, 'Manager' is Bash shell. He assigns tasks to poor-hardworking Kernel.
But wait, Kernel is kind of a 'Super Programmer'. HE decides whether to complete the task
or refuse the task depending on his own evaluation. :wink:

Okay, so this 'Bash shell' is called parent process which instructed Kernel to load
hostname and execute it. Parent process has its own unique pid. Can we list the
parent process of a process? Yes, we can,that the whole point of above two paragraphs :stuck_out_tongue:
Lets do that:

type:
bash

Yes, its kind of 'secondary manager'. Now type 'ps' again

*`
  PID TTY          TIME CMD
27447 pts/9    00:00:00 bash
31400 pts/9    00:00:00 bash
31414 pts/9    00:00:00 ps
`*

Can you note the similarity? The process id '27447' remains the same.
He's your primary bash shell. He instructed Kernel to create another Bash shell.
It has id 31400 finally 31414 is the id of 'ps' the command used to print
above output.

with ps we can pass an option '-o ppid' to get the parent id.Lets get the
parent of 31400 process.

ps -o ppid 31400


*`
 PPID
27447
`*

Yeah,we got the expected output. If you are not convinced, you can also print
name like

ps -o ppid,cmd 31400

*`
 PPID CMD
27447 bash
`*

We call 27447 as parent process and 31400 as child process. Also note
child pid is higher than parent pid. Most often, this will remain true.
Unless Kernel runs out of pid and starts re-using it.

Can we find the Parent of our primary Bash prompt (ie. grandparent of 31400)?

ps -o ppid,cmd 27447

If you keep repeating this you will end up on '1'. which is the parent of all
process.Its called one and only init process. Do you know you created this
process?, its the Kernel!!!

So our 'Manager' Bash job thinks he assigns job to 'Super Programmer' Kernel,
but that fact is 'Kernel is assigning job to Bash via proxy leader named init

Okay, Its time for my dinner! We will talk more about process in next lessons.

Posts: 1

Participants: 1

Read full topic

Viewing all 482 articles
Browse latest View live




Latest Images