Saturday, December 27, 2008

List the installed packages on your Ubuntu box

I found out how to do this recently and thought it might be helpful to some people. To output this information to a file in your home directory you would use,


CODE:
dpkg --get-selections > installed-software


And if you wanted to use the list to reinstall this software on a fresh ubuntu setup,

Code:

dpkg --set-selections < installed-software

followed by

Code:

dselect

Saturday, December 6, 2008

How to get rid of spaces in file names

for file in *\ * ; do mv "${file}" "${file// /_}" ; done

Monday, November 17, 2008

Rotate / Delete log files using logrotate.d

http://www.watchingthenet.com/how-to-manage-log-file-size-archive-deletion.html

Sunday, November 9, 2008

Use ISO images via script

http://www.ubuntugeek.com/mount-and-unmout-iso-images-without-burning-them.html

Saturday, November 8, 2008

How to update your system via Terminal

Setting up a cron job to do updates on a regular basis is simple to do using the command,


sudo aptitude full-upgrade

How Do I Enable Remote Access To MySQL Database Server?

How Do I Enable Remote Access To MySQL Database Server?

By default, MySQL database server remote access disabled for security reasons. However, some time you need to provide the remote access to database server from home or from web server.
MySQL Remote Access

You need type the following commands which will allow remote connections:
Step # 1: Login over ssh if server is outside your IDC

First, login over ssh to remote MySQL database server
Step # 2: Enable networking

Once connected you need edit the mysql configuration file my.cfg using text editor such as vi.

* If you are using Debian Linux file is located at /etc/mysql/my.cnf location
* If you are using Red Hat Linux/Fedora/Centos Linux file is located at /etc/my.cnf location
* If you are using FreeBSD you need to create a file /var/db/mysql/my.cnf

# vi /etc/my.cnf
Step # 3: Once file opened, locate line that read as follows

[mysqld]

Make sure line skip-networking is commented (or remove line) and add following line

bind-address=YOUR-SERVER-IP

For example, if your MySQL server IP is 65.55.55.2 then entire block should be look like as follows:
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/English
bind-address = 65.55.55.2
# skip-networking
....
..
....Where,

* bind-address : IP address to bind to.
* skip-networking : Don’t listen for TCP/IP connections at all. All interaction with mysqld must be made via Unix sockets. This option is highly recommended for systems where only local requests are allowed. Since you need to allow remote connection this line should removed from file or put it in comment state.

Step# 4 Save and Close the file

Restart your mysql service to take change in effect:# /etc/init.d/mysql restart
Step # 5 Grant access to remote IP address

# mysql -u root -p mysqlGrant access to new database
If you want to add new database called foo for user bar and remote IP 202.54.10.20 then you need to type following commands at mysql> prompt:mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar@'202.54.10.20' IDENTIFIED BY 'PASSWORD';
How Do I Grant access to existing database?

Let us assume that you are always making connection from remote IP called 202.54.10.20 for database called webdb for user webadmin, To grant access to this IP address type the following command At mysql> prompt for existing database:mysql> update db set Host='202.54.10.20' where Db='webdb';
mysql> update user set Host='202.54.10.20' where user='webadmin';
Step # 5: Logout of MySQL

Type exit command to logout mysql:mysql> exit
Step # 6: Open port 3306

You need to open port 3306 using iptables or BSD pf firewall.
A sample iptables rule to open Linux iptables firewall

/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

OR only allow remote connection from your web server located at 10.5.1.3:

/sbin/iptables -A INPUT -i eth0 -s 10.5.1.3 -p tcp --destination-port 3306 -j ACCEPT

OR only allow remote connection from your lan subnet 192.168.1.0/24:

/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT

A sample FreeBSD / OpenBSD pf rule ( /etc/pf.conf)

pass in on $ext_if proto tcp from any to any port 3306

OR allow only access from your web server located at 10.5.1.3:

pass in on $ext_if proto tcp from 10.5.1.3 to any port 3306 flags S/SA synproxy state

Step # 7: Test it

From remote system or your desktop type the command:
$ mysql -u webadmin –h 65.55.55.2 –p
Where,

* -u webadmin: webadmin is MySQL username
* -h IP or hostname: 65.55.55.2 is MySQL server IP address or hostname (FQDN)
* -p : Prompt for password

You can also use telnet to connect to port 3306 for testing purpose:$ telnet 65.55.55.2 3306

Finding your Ubuntu version

Sometimes you ask others to help you resolve a problem, it is good to know which ubuntu version you have... here is the very useful command


cappetta@Linux-Box:/var/log/mysql$ lsb_release -dc
Description: Ubuntu 7.10
Codename: gutsy




cappetta@Linux-Box:/var/log/mysql$ cat /etc/issue
Ubuntu 7.10 \n \l

cappetta@Linux-Box:/var/log/mysql$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=7.10
DISTRIB_CODENAME=gutsy
DISTRIB_DESCRIPTION="Ubuntu 7.10"


HOWTO: Log Boot Messages

HOWTO: Log Boot Messages

At times I find it very difficult to see what has successfully loaded or not during boot up. There is a way to initiate logging during startup (boot).

Here's how to set it up:

You need to enable boot logging by opening a terminal and typing the following:
Code:
$ sudo gedit /etc/default/bootlogd
The text editor will open, and the following will be show:

Code:
# Run bootlogd at startup ?
BOOTLOGD_ENABLE=No
Change BOOTLOGD_ENABLE to yes

Now, everytime you restart, /var/log/boot will be created, and it will contain a log of all your boot messages. You need to be root to open it, so you can easily open it up again with sudo.


Quote:
NOTE: On my machine, I get a message on my screen during boot saying that the boot log has failed. But it was lying, the boot log worked just fine.

ALSO NOTE: The boot log will be full of lines like this '^[[A^[[74G[ ok ]'. All that 'garbage' is just the color code used to display the text as white or red during boot. It seems to get captured along with all the rest of the text.
So there you have it! This is a really useful feature, and I was slightly surprised that this was disabled by default.

Wednesday, October 29, 2008

Howto: Backup and restore your system!

1: Backing-up

To do this, become root with
Code:
sudo su
and go to the root of your filesystem (we use this in our example, but you can go anywhere you want your backup to end up, including remote or removable drives.)
Code:
cd /
Now, below is the full command I would use to make a backup of my system:

Code:

tar cvpzf backup.tgz --exclude=/proc 
--exclude=/lost+found --exclude=/backup.tgz
--exclude=/mnt --exclude=/sys /

Now, lets explain this a little bit.
The 'tar' part is, obviously, the program we're going to use.

'cvpfz' are the options we give to tar, like 'create archive' (obviously),
'preserve permissions'(to keep the same permissions on everything the same), and 'gzip' to keep the size down.

Next, the name the archive is going to get. backup.tgz in our example.

Next comes the root of the directory we want to backup. Since we want to backup everything; /

Now come the directories we want to exclude. We don't want to backup everything since some dirs aren't very useful to include. Also make sure you don't include the file itself, or else you'll get weird results.
You might also not want to include the /mnt folder if you have other partitions mounted there or you'll end up backing those up too. Also make sure you don't have anything mounted in /media (i.e. don't have any cd's or removable media mounted). Either that or exclude /media.

EDIT : kvidell suggests below we also exclude the /dev directory. I have other evidence that says it is very unwise to do so though.

Well, if the command agrees with you, hit enter (or return, whatever) and sit back&relax. This might take a while.

Afterwards you'll have a file called backup.tgz in the root of your filessytem, which is probably pretty large. Now you can burn it to DVD or move it to another machine, whatever you like!

EDIT2:
At the end of the process you might get a message along the lines of 'tar: Error exit delayed from previous errors' or something, but in most cases you can just ignore that.

Alternatively, you can use Bzip2 to compress your backup. This means higher compression but lower speed. If compression is important to you, just substitute
the 'z' in the command with 'j', and give the backup the right extension.
That would make the command look like this:

Code:
tar cvpjf backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /
2: Restoring

Warning: Please, for goodness sake, be careful here. If you don't understand what you are doing here you might end up overwriting stuff that is important to you, so please take care!

Well, we'll just continue with our example from the previous chapter; the file backup.tgz in the root of the partition.

Once again, make sure you are root and that you and the backup file are in the root of the filesystem.

One of the beautiful things of Linux is that This'll work even on a running system; no need to screw around with boot-cd's or anything. Of course, if you've rendered your system unbootable you might have no choice but to use a live-cd, but the results are the same. You can even remove every single file of a Linux system while it is running with one command. I'm not giving you that command though!

Well, back on-topic.
This is the command that I would use:

Code:
 tar xvpfz backup.tgz -C /
Or if you used bz2;

Code:
 tar xvpfj backup.tar.bz2 -C /
WARNING: this will overwrite every single file on your partition with the one in the archive!

Just hit enter/return/your brother/whatever and watch the fireworks. Again, this might take a while. When it is done, you have a fully restored Ubuntu system! Just make sure that, before you do anything else, you re-create the directories you excluded:
Code:
mkdir proc
mkdir lost+found
mkdir mnt
mkdir sys
etc...
And when you reboot, everything should be the way it was when you made the backup!



2.1: GRUB restore
Now, if you want to move your system to a new harddisk or if you did something nasty to your GRUB (like, say, install Windows), You'll also need to reinstall GRUB.
There are several very good howto's on how to do that here on this forum, so i'm not going to reinvent the wheel. Instead, take a look here:

http://www.ubuntuforums.org/showthre...t=grub+restore

Personally I have used SuperGrubDisk numerous times http://www.supergrubdisk.org/

FAQ Scripting Languages

Languages:
* Perl
* PHP
* Python
* Ruby
* Tcl
* ECMAScript
* Shell
* C

Common to all:

There are two ways to run a script, one by invoking the interpreter on the command line, and the other is to run it as a normal executable.

To mark a file as executable, use this command:

Code:
chmod +x 
To let the system know which interpreter to use, use the shebang ( #! ). It must be the first line of the script. It should point to the interpreter with the correct options.

To run a script, run it like a normal program, if the script isn't in your $PATH variable, specify the path.

The packages to be install, an example program, and the running of it are given here.

Code:
./
Will run a script or program in your current working directory.

Perl:

You don't need to install anything.

PHP Code:
#!/usr/bin/perl
#Save as "hw.pl"
print "Hello world!";
Mark it executable, and run.

PHP Code:
chmod +x hw.pl
./hw.pl
PHP:

You will need to install the php-cli package.

Code:
sudo aptitude install php5-cli
Example program:

PHP Code:
#!/usr/bin/php
// Save as hw.php
echo "Hello world\n";
?>
Mark it as executable and run.

Python:

Python is already installed.

Example program:

PHP Code:
#!/usr/bin/python
#Save as "hw.py"
print "Hello world!"
Mark it as executable and run.

Ruby:

Install with:
Code:
sudo aptitude install ruby
Example program:

PHP Code:
#!/usr/bin/ruby
# Save as "hw.rb"
puts "Hello world!";
Mark as executable and run.

Tcl:

Tcl is installed.

Example program:

PHP Code:
#! /usr/bin/tclsh
# Save as "hw.tcl"
puts "Hello World!"
Mark it as executable and run.

ECMAScript

Note: ECMAScript isn't normally used this way.

You will need to install spidermonkey, which is the ECMAScript interpreter used by Firefox.

Code:
sudo aptitude install spidermonkey-bin
Example program:

PHP Code:
#!/usr/bin/smjs
//Save as "hw.js"
print("Hello world");
Mark as executable and run.

Shell Scripts

Obviously, you have a shell installed.

Example program:

Code:
#!/bin/sh
# Save as "hw.sh"
echo "Hello world"
Mark as executable and run.

C:

Surprise! C is normally compiled, see FAQ: Compiling your first C or C++ programs if you want to learn how to use C. There is a very good C interpreter which you can use. It is also a compiler, and may be better than gcc in some cases.

You will need to install Tiny C Compiler (tcc), use:

Code:
sudo aptitude install tcc
Example program:

PHP Code:
#!/usr/bin/tcc -run
#include

int main(int argv, char ** argv)
{
printf("Hello World\n");
return
0;
}
Mark it as executable and run it.



Read more:

http://ubuntuforums.org/showthread.php?t=692720

SSH - Modify Welcome Message

Modify the SSH Welcome Message

Very simple...

edit this file: /etc/motd

List Largest directories by Diskspace

List Largest directories by Diskspace


Directory Size Lister/Sorter

Have you ever wondered which directories and trees take up all the diskspace? Questions like this come during pruning/archiving, backup, and numerous other activities. Make the following oneliner script (I call it dirsizes) to find out:
Code:
du -sm $(find $1 -type d -maxdepth 1 -xdev) | sort -g

Create a Tar File of current Directory and Sub Directories


Create a tar file of current directory and all sub directories:


Just give the name of the directory to tar. For example, suppose you want to create a tar file for all the files (including all subdirectories) in the directory called myfiles. Give this command from the directory that contains myfiles:
Code:
tar cvf myfiles.tar myfiles

The find command is used to create a list of all the file names that end in .tex. The --files-from argument to tar is then used; this tells tar to only archive those files listed in /tmp/texfilenames.


To check the list of files in the tar file, you can use the t option. For example,

Code:
tar tf myfiles.tar | less 


Search for files and archive only select ones from a list in a file:


Code:
find myfiles -name "*.tex" > /tmp/texfilenames
tar cvf myfiles.tar --files-from /tmp/texfilenames

Sunday, October 12, 2008

Useful Ubuntu commands

Here are some of useful commands that I have come across:

List installed packages / Easy Restore



Create a list of installed packages:

Code:
dpkg --get-selections > installed-software
And if you wanted to use the list to reinstall this software on a fresh ubuntu setup,

Code:
dpkg --set-selections <> 
followed by

Code:
dselect