Replacing same text in multiple files

If you have text you want to replace in multiple locations, there are several ways to do this. To replace the text Windows with Linux in all files in current directory called test[something] you can run this:

perl -i -pe 's/Windows/Linux/;' test*

To replace the text Windows with Linux in all text files in current directory and down you can run this:

find . -name '*.txt' -print | xargs perl -pi -e's/Windows/Linux/ig' *.txt

Or if you prefer this will also work, but only on regular files:

find -type f -name '*.txt' -print0 | xargs --null perl -pi -e 's/Windows/Linux/'

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

How to mount ext2fs from FreeBSD

To mount ext2fs filesystems under FreeBSD, you first have to build a new kernel with ext2fs support. Put the line

options “EXT2FS”

in your kernel configuration file for the new kernel and compile.
Read the FreeBSD handbook to learn how to do that.

or

Do the following steps to enable ext2fs support in the kernel:

# cd /usr/src/sys/modules/ext2fs
# make
# make install

You can use ‘kldload‘ to load the ext2fs module in to the kernel.

# kldload ext2fs
Then you will be able to mount your linux partitions by giving a command like:

# mount -t ext2fs /dev/ad1s1 /mnt

to unload module use

# kldunload ext2fs

To load the module automatically on system startup

add the following line in to /boot/loader.conf

ext2fs_load=”YES”

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.

A Short Introduction To Cron Jobs

crontab

The command to create/edit, list, and remove cron jobs is crontab. If you call it with the -u option, it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines «your» crontab, i.e., the crontab of the person executing the command. If you are looged in as root and run crontab without -u, then root’s crontab is listed/modified/removed. If you are logged in as exampleuser and run crontab without -u, then exampleuser‘s crontab is listed/modified/removed.

Examples:

crontab -l

lists the cron jobs of the user as that you are currently logged in:

server1:~# crontab -l
* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log
30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log
server1:~#

crontab -u exampleuser -l

lists all cron jobs of exampleuser.

crontab -e

let’s you create/modify the cron jobs of the user as that you are currently logged in (I’ll come to the syntax in the next chapter).

crontab -u exampleuser -e

let’s you create/modify the cron jobs of exampleuser.

crontab -r

deletes all cron jobs of the user as that you’re currently logged in.

crontab -u exampleuser -r

deletes all cron jobs of exampleuser.

If you have written your cron jobs to a text file, you can use the text file to create the cron jobs. For example, let’s assume you have created the text file /tmp/my_cron_jobs.txt

vi /tmp/my_cron_jobs.txt

… with the following contents:

30 00 * * * /path/to/script

You can create a cron job from that file as follows:

crontab /tmp/my_cron_jobs.txt

(Or for exampleuser:

crontab -u exampleuser /tmp/my_cron_jobs.txt

)

Please note that this will overwrite all previously created cron jobs – if you’ve already created some cron jobs, you better use crontab -e and add the new cron job manually.

See

man crontab

to learn more about the crontab command.

Cron Job Syntax

A cron job consists out of six fields:

<minute> <hour> <day of month> <month> <day of week> <command>

field          allowed values
—–          ————–
minute         0-59
hour           0-23
day of month   1-31
month          1-12 (or names, see below)
day of week    0-7 (0 or 7 is Sun, or use names)

When specifying day of week, both day 0 and day 7 will be considered Sunday.

A field may be an asterisk (*), which always stands for first-last.

Names can also be used for the «month» and «day of week» fields. Use the first three letters of the particular day or month (case doesn’t matter), e.g. sun or SUN for Sunday or marMAR for March..

Let’s take a look at the two cron jobs from the first chapter:

* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log

This means: execute /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per minute.

30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log

This means: execute /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per day at 00:30h.

The day of a command’s execution can be specified by two fields: day of month, and day of week. If both fields are restricted (i.e., aren’t *), the command will be run when either field matches the current time. For example, 30 4 1,15 * 5 would cause a command to be run at 4:30h on the 1st and 15th of each month, plus every Friday.

You can use ranges to define cron jobs:

Examples:

1,2,5,9 – means every first, second, fifth, and ninth (minute, hour, month, …).

0-4,8-12 – means all (minutes, hours, months,…) from 0 to 4 and from 8 to 12.

*/5 – means every fifth (minute, hour, month, …).

1-9/2 is the same as 1,3,5,7,9.

Ranges or lists of names are not allowed (if you are using names instead of numbers for months and days – e.g., Mon-Wed is not valid).

1,7,25,47 */2 * * * command

means: run command every second hour in the first, seventh, 25th, and 47th minute.

Instead of the first five fields, one of eight special strings may appear:

string         meaning
——         ——-
@reboot        Run once, at startup.
@yearly        Run once a year, «0 0 1 1 *».
@annually      (same as @yearly)
@monthly       Run once a month, «0 0 1 * *».
@weekly        Run once a week, «0 0 * * 0».
@daily         Run once a day, «0 0 * * *».
@midnight      (same as @daily)
@hourly        Run once an hour, «0 * * * *».

You can also use name=value pairs in a crontab to define variables for the cron jobs:

# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
# mail any output to exampleuser, no matter whose crontab this is
MAILTO=exampleuser
# set the PATH variable to make sure all commands in the crontab are found
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * my_command

Please note: unless you set a PATH variable in a crontab, always use full paths in the crontab to make sure commands are found and can be executed. For example, instead of writing rsync, you should write /usr/bin/rsync. Use which to find out the full path of a program:

which rsync

server1:~# which rsync
/usr/bin/rsync
server1:~#

See man 5 crontab to learn more about the cron job syntax.

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 😀

Use Postfix Instead of Sendmail in Trixbox

Another alternative to email notification is to use Postfix instead of Sendmail because Sendmail is not as secure besides, using Postfix is a lot easier and it is more secure than Sendmail.

If you want to proceed with it, you have to remove Sendmail and replace it with Postfix, using these 2 commands:

rpm -e –nodeps sendmail
yum -y install postfix

Following this, edit your /etc/postfix/main.cf, (use your preffered editor and add your preffered SMTP) and add:

relayhost = smtp.your-smtp.com

Replace smtp.your-smtp.com with your own smtp server.

You should also edit /etc/asterisk/vm_general.inc and set the «serveremail» parameter to your real email address.  This is to avoid messages with invalid from-addresses floating aimlessly in the Internet.

Then load Postfix.

service postfix reload

Postfix will now send all outgoing email to your ISP’s mail server.