July 23, 2011

Linux for hackers - Part I


Hackers & Linux share a very intimate relation. Linux offers hackers all the freedom they nee and hackers care only for that very thing!  You can do whatever you like with your system and that is the biggest factor where Linux wins over.  Achieving perfection, however, can be a daunting task given the enormity of this computer OS.
In this multi part series, I will be discussing what basic Linux utilities are needed on day to day basis. Also this will serve as a reference for me if I forget something ;)
One thing to note here is that this is not a “Linux for beginners” tutorial. This is “Linux for newbie hackers”

I have handpicked some of the most important things that one should must know. There is no order of any kind in these utilities.




GREP
‘grep’ is one of the most important and useful command. It is very helpful in awk,shell,sed and perl scripting.

What is grep used for? - Searching.
syntax: grep xyz file
grep searches for the pattern ‘xyz’ in ‘file’ and returns those rows which have that pattern present in them.
You can pass the output of any command to grep by piping it.
ls | grep hack
This command lists all the files in current directory which contain the string “hack”

The ultimate power of grep is understood when used with wild-cards & regular expressions.

Wild-card . (dot)
“.” matches exactly 1 character
for example:
$cat file
foo
too
look

$grep ‘.oo’ file
foo
loo

To search for strings having “.” in them, we use ‘\’ escape character

e.g. To search for “google.com”
$grep ‘google\.com’ file

Repetition *
$grep ‘lo*k’ file
look

character followed by * matches with any number of occurrences of that character.
“.*” matches with any string as expected

Regular expressions
Below table shows the use of regexp in grep with examples

RegexpDescriptionUseMatches
[ ]Matches a selection of characters. Range of characters can also be provided.
In short [ ] acts as a placeholder for 1 character within the brackets.
grep “[Gg]oogle” file
grep [a-c]d
google,Google
ad,bd,cd
^This can be used as negation.

This can also be used as the beginning of a line.
grep [^a]d


grep ^[ ]*hello
bd,cd,... but not ad


Matches lines having  hello at the beginning of the line.

hello world
     hello

This will not be matched: hi hello
$Matches the end of the linegrep hello$Matches lines with hello at the end.


This is it for grep in this intro, but there are a lot of things that can be done with regular expressions. Refer grep man page for that!

Managing users and groups
To add a new user to the system:
$useradd bob
When a user is added without mentioning the group, Linux automatically creates a new group with the same name as the user name just added.

$useradd -g admins alice
This will add the user alice to group of admins.

When each new user first logs in, they are prompted for their new permanent password.


To add a new group to the system:
$groupadd developers
This will add a group with name developers.

Changing password:
$passwd alice
Changing password for user alice
New password:
Retype new password:
passwd: all authentication tokens updated successfully.


Deleting a user:
$userdel bob
Deletes the user bob but keeps the data.

$userdel -r bob
Deletes the user as well as deletes all the data from that user’s home folder.



Starting/stopping services
All the installed services go to the /etc/init.d/ directory.
To check the status of a service:
$/etc/init.d/<service> status
replace <service> with the name of service you want to check.
example:
$/etc/init.d/apache2 status
Apache is NOT running.

To start a service:
$/etc/init.d/apache2 start
* Starting web server apache2                       [ OK ]
To stop a service:
$/etc/init.d/apache2 stop
* Stopping web server apache2                             
  ... waiting                            [ OK ]


Keeping the system updated
sudo apt-get update
sudo apt-get dist-upgrade


I think this is it for the part 1. I might have missed some very important things in this part but I will try to cover them in later parts.

No comments:

Post a Comment