Logo

top ten useful linux commands

over 2 years ago | Manohar Amrutkar: majestic rails

1. List command

ls -a
// lists all
ls -l
// lists all files including hidden files like .htaccess
ls -l | more
// shows in paged format with pipe

2. Untarring the uploaded tar.gz files

This command is the most important command and often regularly used to uncompress files..

tar -xzf filename.tar.g

How to dump/Import tar.gz (zipped) file into MySQL database

SSH command to restore/import gzipped large databases to mysql on the fly with just one single command. This is so useful as large databases can only be imported to mysql this way! Remember to upload the database dump before you issue this command

gunzip <>

3. Installing packages

Installing softwares in linux the most basic and you will need to know this command very well .

yum install
// for centos and redhat
apt-get install
// for debian/ubuntu

4. To look for memory usage

top
// shows the memory, cpu load and other processes running
free
// shows the memory usage of linux

5. To see the disk usage.

df -h
// shows the disk usage and outputs the disk usage left

6. To view running processes

ps aux
// shows the running processes and its ID. You can use this ID to kill the process later.

ps aux | grep "process_name" [eg. ruby script/server]

//To see specific process

kill -9 pid

// to kill the process

7. Deleting all files within a folder

This perhaps is one of the most sought after command to delete all subfiles and subfolders in linux with just one command in a fly.

rm -Rf /my/path
// this deletes all files. so be careful!

8. Edit a file in easy way!

I love nano command. Its just so easy to edit a file with menu options and save changes. You can easily search for specific text inside the file with the nano editor.

nano /file/path
// text editor to edit files

9. Changing Permissions and Ownership

whats there in linux without permissions and ownership

chmod -R 755 /folder/path
// this recursively changes all files and folders of the path.
chown pbu /file/name
//changes the ownership of filename
chgrp mygrp /file/name
//changes the group to new one

If you dont know how to interpret the 755 or number like permissions here is an easy way to remember
Ex: 755 means rwxr-xr-x
7 -> rwx (for owner)
5 -> r-x (non writable for group)
5 -> r-x (non writable for world)
First we consider '7' in 755 as it is made up of 3 bit binary. To construct 7 we need to construct 111(which is 4+2+1 in decimal).
5 -> 101 ( which is 4+0+1)
5 -> 101 (which is 4+0+1)
Remember 755 is the decimal of the binary and it is always made up of 4+2+1.

10. To find out what ports are open

Finding which ports are open and closed are so important in terms of security point of your server. You might want to close unwanted services using different ports.

netstat -nap --tcp
// lists connections and what ports are open.
OR
nmap fuser localhost
// easy command

Install firewall to block unwanted ports running on your PC or in server.

FAQs

How do i change root password?
Just login as root or su root and type passwd, you will be asked for new password.

> passwd
// Be CAREFUL! If you forget your new password, you will lock yourself

I am installing a firewall in my linux server? How do i know which ports to block and to open?
When installing firewall be sure to block all unwanted open ports. A comprehensive collection of ports and services run at that port can be found here
Here is the short of ports which needs to be kept open for listening.

Common ports
21 => FTP
22 => SSH
25 => SMTP Mail Transfer
43 => WHOIS service
53 => name server (DNS)
80 => HTTP (Web server)
110 => POP protocol (for email)
143 => IMAP Protocol (for email)
443 => SSL for HTTP


Cpanel => 2077,2078,2082,2083,2086,2087,2089,2095,2096,3306, 6666.
Plesk => 8443
Webmin => 10000

If you have root permissions, you can login to your server control panel like this...

https://ipaddress:2087/
// for cpanel
https://ipaddress:8443/
// for plesk

Hope my ports compilation should be helpful for beginners who use linux.

investigating port on Windows XP and Linux

over 2 years ago | Manohar Amrutkar: majestic rails

How to Investigate a port in Windows XP

If you find anything suspicious going on in your computer, you can investigate a port in your PC with windows xp task manager program. Many worms, viruses and trojans mailiciously download harmful scripts to your computer using different ports that you may not know. The best way to protect your PC from such worms and trojans is by using a firewall. You can use comodo firewall which can be downloaded free of charge.


To investigate a port and see what programs are using, can be done easily in 2 steps
1. Go to Start > Run > command
2. At the command prompt type netstat -noa
Investigate port in windows xp
3. Look for connections with LISTENING and ESTABLISHED. Note that port numbers will be identified in the form of 127.0.0.1:1092 (IP address followed by colon is the port number). In this case 1092 is the port.
4. Note the PID (process id) for that port and this process id corresponds to program using it.
5. Next we will find out that PID with windows task manager program.
6. Go to ALT+CTRL+DEL then press task manager button. You will see the processes listed and the memory.
Note: By default with task manager it will not show the PID of the processes. You have to enable it from the options menu.

Should you find the running process to be suspicious just end the process.
Tip: Use a firewall like ZoneAlarm or Comodo Firewall (both are free) to block suspicious programs running on your PC.

How to Investigate a Port in Linux

It is very easy to investigate a port and kill that process in your linux server or box.
  • First find the ports that are active and listening to.
  • Note the port and the process ID (PID)
  • Find which processes is running based on that PID
  • If you find anything suspicious, kill that process.
netstat -nap
// lists both TCP and UDP connections (listening and established)
Netstat - Investigate suspicious  port Linux
ps
// this command tells which program is running this process id
pidof
// alternate command
If you find this running program or process suspicious like trojan or worm, kill the process
kill

I got this over http://www.mysql-apache-php.com/investigate-port.htm I kept it over my blog to share as well for my knowledge management.

the very most important - how to search on google

over 2 years ago | Manohar Amrutkar: majestic rails

Basic search tips
More search tips

using rails environment specific commands

over 2 years ago | Manohar Amrutkar: majestic rails

It's for you only if are new to RoR, as you may get confused, Bcoz they all come in different shapes and sizes.

For mongrel/Else

mongrel_rails start -e production/ ruby script/server -e production

For rake command:
rake [task] RAILS_ENV=production

For ruby console:
ruby script/console production

why rails is majestic rails?

over 2 years ago | Manohar Amrutkar: majestic rails

* Ruby on Rails includes features that help in increasing developer productivity. Some of the main features include the following:


* MVC/AGILE architecture: Ruby on Rails is based on the MVC (Model View Controller) architecture that enables the data to be separated from presentation. It's an agile web development framework.

* Database Access Library: Ruby on Rails includes a database access library - Active Record - that simplifies data handling in databases. Active Record automatically maps tables to classes and rows to objects.

* Libraries for common tasks: Ruby on Rails includes a host of libraries that simplify the coding of common programming tasks such as form validations, sessions management, etc.

* AJAX Library: An extensive library of AJAX functions is provided in the Rails framework. Ruby code can be used to generate AJAX code. The associated java scripting required for AJX gets generated automatically.

* Convention over configuration: Ruby on Rails does not have any XML configuration files. It includes simple programming conventions that can be used to specify the configuration parameters.

* Customized URL: Custom or Search Engine Friendly URLs can be developed using the Ruby on Rails framework.

* Debugging: Detailed error logs are provided, making it easier to debug applications.

* Components: Components can be used to store reusable code. Components can be included to modularize templates.

* DRY: Don't Repeat Yourself. You will get it when you actually write the rails code.

How to capture Oracle's dbms_output in your Rails log file

over 2 years ago | Alex Rothenberg: Common Sense Software

I have been writing a rails application on top of a large existing Oracle database where each table has 5+ triggers that each call several stored procedures and each of those PL/SQL stored procedures is hundreds of lines long. Often a simple update statement fails with an ORA-xxxx exception coming from deep in the PL/SQL code and it can be tough to figure out what's gone wrong.

The usual way Oracle database folks figure out what's going on is to put print statements in their code. In oracle this looks like

  dbms_output.put_line('hi i hit this line of pl/sql');
When you're using an Oracle editor like TOAD or SQLDeveloper you have to turn output on and then will see anything that's printed.
  set serveroutput on;
This is great if you divide the application between Rails
and Database developers and assume each group can work independently to write perfect code but what about the real world!
Today I want to show you how I monkey patched the Oracle Enhanced Adapter to stick the dbms_output into
the rails log file.
Let's start with an simple example of a simple PL/SQL function that tells you if a string is more than 5 characters long (with some simple debugging print statements).
  CREATE OR REPLACE FUNCTION
MORE_THAN_FIVE_CHARACTERS_LONG (some_text VARCHAR2) RETURN INTEGER
AS
longer_than_five INTEGER;
BEGIN
dbms_output.put_line('before the if -' || some_text || '-');
IF length(some_text) > 5 THEN
dbms_output.put_line('it is longer than 5');
longer_than_five := 1;
ELSE
dbms_output.put_line('it is 5 or shorter');
longer_than_five := 0;
END IF;
dbms_output.put_line('about to return: ' || longer_than_five);
RETURN longer_than_five;
END;
Now we can run the following in TOAD
  set serveroutput on;
select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual;
select MORE_THAN_FIVE_CHARACTERS_LONG('short') from dual;
And we get this output
  MORE_THAN_FIVE_CHARACTERS_LONG('ALONGSTRING') 
---------------------------------------------
1

1 rows selected

before the if -a long string-
it is longer than 5
about to return: 1

MORE_THAN_FIVE_CHARACTERS_LONG('SHORT')
---------------------------------------
0

1 rows selected

before the if -short-
it is 5 or shorter
about to return: 0
To get the same in our Rails app we just need to monkey patch the OracleEnhancedAdapter by copying what's below into your project as config/initializers/oracle_enhanced_adapter.rb

  module ActiveRecord
module ConnectionAdapters
class OracleEnhancedAdapter < AbstractAdapter
DBMS_OUTPUT_BUFFER_SIZE = 10000 #can be 1-1000000
DBMS_LINE_MAX_SIZE = 1000
def enable_dbms_output
@enable_dbms_output = true
execute "BEGIN dbms_output.enable(#{DBMS_OUTPUT_BUFFER_SIZE}); END;"
end
def disable_dbms_output
@enable_dbms_output = false
execute "BEGIN dbms_output.disable(); END;"
end
def dbms_output_enabled?
@enable_dbms_output
end

protected
def log(sql, name)
super(sql, name)
ensure
log_all_dbms_output if dbms_output_enabled?
end

private
def log_next_line_of_dbms_output
dbms_output_text, status = @connection.exec "BEGIN dbms_output.get_line(:return, :status); END;", ' '*DBMS_LINE_MAX_SIZE, 1
got_text = (status == 0)
@logger.debug "DBMS_OUTPUT: #{dbms_output_text}" if got_text
got_text
end

def log_all_dbms_output
while log_next_line_of_dbms_output do
end
end
end
end
end

To use it let's show a quick script/console session
  >> ActiveRecord::Base.connection.enable_dbms_output
=> []
>> ActiveRecord::Base.connection.select_all("select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual")
=> [{"more_than_five_characters_long('alongstring')"=>1}]
And what's in log/development.log

SQL (27.0ms) BEGIN dbms_output.enable(10000); END;
SQL (25.9ms) select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual
DBMS_OUTPUT: before the if -a long string-
DBMS_OUTPUT: it is longer than 5
DBMS_OUTPUT: about to return: 1

This is admittedly a very simple example but I have used this in a real application where I am updating several related ActiveRecord objects and seeing the DBMS_OUTPUT inline with the various SQL update statements has been extremely useful in tracking down a real bug in the PL/SQL procedure that has been in our system for over a year!

I've submitted this as a patch to the Oracle Enhanced Adapter so perhaps it will be included at some point so you wont have to do the monkey patching yourself. I was somewhat surprised not to find anything similar out there so if you know of something please leave a comment.

How to capture Oracle's dbms_output in your Rails log file

over 2 years ago | Alex Rothenberg: Common Sense Software

I have been writing a rails application on top of a large existing Oracle database where each table has 5+ triggers that each call several stored procedures and each of those PL/SQL stored procedures is hundreds of lines long. Often a simple update statement fails with an ORA-xxxx exception coming from deep in the PL/SQL code and it can be tough to figure out what's gone wrong.

The usual way Oracle database folks figure out what's going on is to put print statements in their code. In oracle this looks like

  dbms_output.put_line('hi i hit this line of pl/sql');
When you're using an Oracle editor like TOAD or SQLDeveloper you have to turn output on and then will see anything that's printed.
  set serveroutput on;
This is great if you divide the application between Rails
and Database developers and assume each group can work independently to write perfect code but what about the real world!
Today I want to show you how I monkey patched the Oracle Enhanced Adapter to stick the dbms_output into
the rails log file.
Let's start with an simple example of a simple PL/SQL function that tells you if a string is more than 5 characters long (with some simple debugging print statements).
  CREATE OR REPLACE FUNCTION
MORE_THAN_FIVE_CHARACTERS_LONG (some_text VARCHAR2) RETURN INTEGER
AS
longer_than_five INTEGER;
BEGIN
dbms_output.put_line('before the if -' || some_text || '-');
IF length(some_text) > 5 THEN
dbms_output.put_line('it is longer than 5');
longer_than_five := 1;
ELSE
dbms_output.put_line('it is 5 or shorter');
longer_than_five := 0;
END IF;
dbms_output.put_line('about to return: ' || longer_than_five);
RETURN longer_than_five;
END;
Now we can run the following in TOAD
  set serveroutput on;
select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual;
select MORE_THAN_FIVE_CHARACTERS_LONG('short') from dual;
And we get this output
  MORE_THAN_FIVE_CHARACTERS_LONG('ALONGSTRING') 
---------------------------------------------
1

1 rows selected

before the if -a long string-
it is longer than 5
about to return: 1

MORE_THAN_FIVE_CHARACTERS_LONG('SHORT')
---------------------------------------
0

1 rows selected

before the if -short-
it is 5 or shorter
about to return: 0
To get the same in our Rails app we just need to monkey patch the OracleEnhancedAdapter by copying what's below into your project as config/initializers/oracle_enhanced_adapter.rb

  module ActiveRecord
module ConnectionAdapters
class OracleEnhancedAdapter < AbstractAdapter
DBMS_OUTPUT_BUFFER_SIZE = 10000 #can be 1-1000000
DBMS_LINE_MAX_SIZE = 1000
def enable_dbms_output
@enable_dbms_output = true
execute "BEGIN dbms_output.enable(#{DBMS_OUTPUT_BUFFER_SIZE}); END;"
end
def disable_dbms_output
@enable_dbms_output = false
execute "BEGIN dbms_output.disable(); END;"
end
def dbms_output_enabled?
@enable_dbms_output
end

protected
def log(sql, name)
super(sql, name)
ensure
log_all_dbms_output if dbms_output_enabled?
end

private
def log_next_line_of_dbms_output
dbms_output_text, status = @connection.exec "BEGIN dbms_output.get_line(:return, :status); END;", ' '*DBMS_LINE_MAX_SIZE, 1
got_text = (status == 0)
@logger.debug "DBMS_OUTPUT: #{dbms_output_text}" if got_text
got_text
end

def log_all_dbms_output
while log_next_line_of_dbms_output do
end
end
end
end
end

To use it let's show a quick script/console session
  >> ActiveRecord::Base.connection.enable_dbms_output
=> []
>> ActiveRecord::Base.connection.select_all("select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual")
=> [{"more_than_five_characters_long('alongstring')"=>1}]
And what's in log/development.log

SQL (27.0ms) BEGIN dbms_output.enable(10000); END;
SQL (25.9ms) select MORE_THAN_FIVE_CHARACTERS_LONG('a long string') from dual
DBMS_OUTPUT: before the if -a long string-
DBMS_OUTPUT: it is longer than 5
DBMS_OUTPUT: about to return: 1

This is admittedly a very simple example but I have used this in a real application where I am updating several related ActiveRecord objects and seeing the DBMS_OUTPUT inline with the various SQL update statements has been extremely useful in tracking down a real bug in the PL/SQL procedure that has been in our system for over a year!

I've submitted this as a patch to the Oracle Enhanced Adapter so perhaps it will be included at some point so you wont have to do the monkey patching yourself. I was somewhat surprised not to find anything similar out there so if you know of something please leave a comment.

GM's Volt: Car of the future?

over 2 years ago | Nilesh Naik: TechnoBites


Being an enthusiastic car lover, I always dreamt of driving around the city without shelling out much on the fuel. Well here comes a car from the General Motors that not only breaks the 100 mark barrier in fuel economy rating but does so by a large margin. The Chevrolet Volt, a plug-in hybrid car is estimated to get a whopping 230 miles per gallon. It basically consists of an electric engine with Lithium batteries & a internal combustion engine. You can charge its batteries by plugging in to your household electricity when you're home. It operates as an electric car until its batteries start to get low, and then it starts running a small gas motor to power a generator and knowing that Google helped develop this technology makes it even more interesting.

A dream car to own right? Not exactly, as it carries a not too attractive sticker price of ~ $40,000 largely due to high manufacturing costs. Its battery itself costing $8000.

Expected to roll out in 2011, it seems to have got competition even before hitting the roads. Nissan recently announced the 2010 Nissan LEAF dubbed as the possible "Volt killer" claiming 367 mpg - wow!. As a consumer, its great to see competition growing towards greener technology & helping the environment.

So would you go for it? Well analysts say that you'd have to drive at least 200,000 miles—or 158,000 miles before you start saving money. Huh! probably you would be having a different car by then. Or if you dont wont to spend so much of moolah on this new super machine just wait for the technology to improve over the years which could eventually bring its price down.

INDIA INSPIRED

over 2 years ago | Rohan Daxini: void TechFuels()

My List of indespensable developer tools

over 2 years ago | Rohan Daxini: void TechFuels()

Everyone collects dev tools, utilities and most folks have a list of a few that they feel are indispensable. Here's mine (of course all these on my home PC).