Most useful commands for Linux

* Continuously check Apache error log file:
$ tail -f /var/log/httpd/error_log

* View first 15 lines from MySQL log:
$ head -15 /var/log/mysqld.log

* vi keyboard shortcuts
=> jump to end of line
$
=> start of line
0
=> Delete rest of line
D
=> Repeat the last command given:
. (dot)

=> add ‘maal’ to the end of every line. 1 is line 1, $ is the last line
:1,$ s/$/maal/

=> put ‘bingo’ at the start of lines 5-10
:5,10 s/^/bingo/

=> change foo to bar for all occurrences in the rest of the file from where the cursor is
:s/foo/bar/g

=> Delete current line and got into insert mode.
C

=> Remove the ^M from files that came from windows:
:se ff=unix

=> Turn on/off display of line numbers:
:set nu
:set nonu

=> if you want actual line numbers in your file:
:%!cat -n

=> find the word under cursor
* (star)

* screen command
Just type screen and your problems are solved 🙂
$ cat ~/.screenrc
# no annoying audible bell, please
vbell on

# detach on hangup
autodetach on

# don’t display the copyright page
startup_message off

# emulate .logout message
pow_detach_msg «Screen session of $LOGNAME $:cr:$:nl:ended.»

# advertise hardstatus support to $TERMCAP
termcapinfo xterm* ti@:te@

# make the shell in every window a login shell
shell -$SHELL

defscrollback 10000

# Extend the vt100 desciption by some sequences.

termcap vt* AF=E[3%dm:AB=E[4%dm
caption always
caption string ‘%{= wk}[ %{k}%H %{k}][%= %{= wk}%?%-Lw%?%{r}(%{r}%n*%f%t%?(%u)%?%{r})%{k}%?%+Lw%?%?%= %{k}][%{b} %d/%m %{k}%c %{k}]’

# keybindings

bind -k F5 prev
bind -k F6 next

netstat
* Display total number of internet (port 80) connections:
$ netstat -an |grep :80 |wc -l

* Display all ports your machine listening on:
$ netstat -ant | grep LISTEN

nmap
* Scan a machine on your LAN with nmap and know which ports are open on it:
$ nmap ip

find
* find top 10 largest files in /var:
$ find /var -type f -ls | sort -k 7 -r -n | head -10

* find all files having size more than 5 GB in /var/log/:
$ find /var/log/ -type f -size +5120M -exec ls -lh {} ;

* find all today’s files and copy them to another directory:
$ find /home/me/files -ctime 0 -print -exec cp {} /mnt/backup/{} ;

* find all temp files older than a week and delete:
$ find /temp/ -mtime +7-type f | xargs /bin/rm -f

* find and rename all mp3 files by changing their uppercase names to lowercase:
$ find /home/me/music/ -type f -name *.mp3 -exec rename ‘y/[A-Z]/[a-z]/

grep
* Print Apache’s documentroot directory name:
$ grep -i documentroot /etc/httpd/conf/httpd.conf

* View file contents without comments and empty lines:
$ grep -Ev “^$|^#” /etc/my.cnf

* print only IP address assigned to the interface:
$ ifconfig eth0 | grep ‘inet addr:’ | cut -d’:’ -f2 | awk ‘{ print $1}’

* How many email messages sent for a particular date:
$ cat /var/log/maillog | grep «status=sent» | grep «May 25» | wc -l

* Find out a running process/daemon from process list (thanks to staranneph for recalling this):
ps -ef | grep mysql

* You can also note cpu/mem usage by using above. like in below command output, you can see that Plesk’s statistics process is utilizing more than 18% cpu alone:
[root@myserver ~]# ps aux | grep statistics
root 8183 18.4 0.0 58384 2848 ? D 04:05 3:00 /usr/local/psa/admin/sbin/statistics

Backup MySQL Database to a file

Backing up your database is a very important system administration task, and should generally be run from a cron job at scheduled intervals. We will use the mysqldump utility included with mysql to dump the contents of the database to a text file that can be easily re-imported.

Syntax:

mysqldump -h localhost -u root -pmypassword databasename > dumpfile.sql

Example:

mysqldump -h localhost -u root -p2Uad7as9 database01 > dumpfile.sql

This will give you a text file containing all the commands required to recreate the database.

A «live» view of a logfile on Linux

This approach works for any linux operating system, including Ubuntu, and is probably most often used in conjunction with web development work.

tail -f /path/thefile.log

This will give you a scrolling view of the logfile. As new lines are added to the end, they will show up in your console screen.

For Ruby on Rails, for instance, you can view the development logfile by running the command from your project directory:

tail -f log/development.log

As with all linux apps, Ctrl+C will stop it.

Recover root password

Step 1: For Redhat based distros:

Boot from your first install CD and as the very first screen comes up hit F2 and type:

rescue ( For RedHat «linux rescue» ) and the computer will boot in rescue mode.

It will show a few alternatives, select : ¨mount the existing partitions¨ and go to the shell/console prompt.

( Booting Mandrake in «failsafe» from the Lilo menu does the same )

Step 1: For SUSE:

Boot from your first install CD and press F1 at the first screen, then choose «Rescue System» from the menu and at the prompt type «root» ( you do not need a password )

Step 1: For other distro’s

Boot from the first install CD ( or any Live CD like Knoppix ) and at the bootprompt type:

linux single

(For Knoppix: knoppix single )

Alternative for step 1

Sure with most Live and Rescue CDs you can also just boot in the live version, mount the partition and, as root, make changes to the needed files

Step 2

# cd /etc

( if you boot from knoppix first cd to the partition your lost-password-distro is on )

We need to change two files; ¨passwd¨ and ¨shadow¨:

# vi passwd

< i > ( puts vi in insertmode )

This is the first line:

root:x:0:0:root:/root:/bin/bash

Make it:

root::0:0:root:/root:/bin/bash

So just get rid of the ¨x¨ and leave the ¨:¨ ( colons )

Save the file:

Press Esc,

write –> :x,

Press enter

Second file to be changed:

# vi shadow

< i > ( insert mode )

The first line is a long scrambled line of characters, just make it:

root:::: ( four : )

Press Esc

Write –> 😡

Press enter

Now you can reboot your computer. Log in as normal user, open a console and type:

$ su

# passwd

Now set the new root password !

Log out as root:

Ctrl+d

Access Your MySQL Server Remotely Over SSH

So you’ve got MySQL on your web server, but it’s only opened to local ports by default for security reasons. If you want to access your database from a client tool like the MySQL Query Browser, normally you’d have to open up access from your local IP address… but that’s not nearly as secure.

So instead, we’ll just use port-forwarding through an SSH tunnel, so your MySQL client thinks it’s connecting to your localhost machine, but it’s really connecting to the other server through the tunnel.

If you are using the command line ssh, the command would look like this. (You can do the same thing graphically in Putty or SecureCRT options if you need to)

ssh -L 3306:localhost:3306 geek@webserver.com

The syntax is ssh -L <localport>hostname<remoteport> <username>@<servername>. We’re using localhost as the hostname because we are directly accessing the remote mysql server through ssh. You could also use this technique to port-forward through one ssh server to another server.

If you already have mysql running on your local machine then you can use a different local port for the port-forwarding, and just set your client tools to access MySQL on a different port.

Once you’ve got the ssh tunnel going, you can open up MySQL Query Browser and enter in the details for your remote server, using localhost as the server host, and adjust the port to whatever you used.

How to check installed software in debian based linux (ubuntu)

Who remembers what has installed in his Ubuntu box?

Here is the simple command:

dpkg –get-selections > installed-software

more installed-software

What can you do in a fresh Ubuntu installation that you do not remember what software you need?

dpkg –set-selections < installed-software

and after that

dselect

All set in your fresh debian based linux.

Debian makes things simple 😀

Shuttleworth: Ubuntu developer Canonical may need 3-5 more years of funding

Founder of unprofitable Linux vendor says he’s willing to continue bankrolling the company Canonical Ltd., the commercial backer of the Ubuntu Linux operating system, is not yet turning a profit, but founder Mark Shuttleworth said during a teleconference today that he is prepared to bankroll the company for three to five more years.

«We continue to require investment, and I continue to be careful with my pennies in making those investments, but I consider this a good proposition,» the billionaire technologist said. «Canonical is not cash-positive, but our offering is very attractive to those who want to pinch their pennies in the Linux space.»

The dismal economic climate could, in fact, drive more business for Ubuntu, Shuttleworth added optimistically.

Canonical, which brings in money by providing fee-based services and support for the Ubuntu software, currently has annual revenue in the range of «several million» dollars, Shuttleworth said. But the company’s low revenue and lack of profits hasn’t shaken his belief in its business model.

«We are entering a time when there’s a real commoditization of the desktop, so I don’t think it would be possible to make a lot of money, or even any money,» by trying to sell Linux itself, Shuttleworth said. «The only way to build businesses around Linux is around services.»

Shuttleworth held the teleconference in conjunction with this week’s upcomingUbuntu 8.10 release, which Canonicalannounced today. Ubuntu 8.10 includes improved 3G wireless support and new features such as a «guest» capability that lets users lend their PCs to other people to check e-mail and perform other tasks without disturbing the existing programs or settings.

Looking forward, Canonical expects Ubuntu’s development to embrace three key trends, Shuttleworth said: touch-based interactivity, 3-D imagery and the integration of Web-like features into the desktop user experience.

In the future, «most devices will have a touch dimension within them,» he said, adding that «the lines between the 2-D desktop and 3-D gaming environment are going to blur.»

 

Thanks to: here