Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
53 total  XML / RSS feed 

activate root in ubuntu after install

After this you can run su.

sudo passwd root

running scheduled programs in linux

use crontab

Edit the current configuration for crontab with:
crontab -e

Normally we'd created bash script files located in /usr/local/bin and have them specified for execution within our crontab configuration.

Rename files by reordering existing data (regular expression capturing submatches)

Takes all files in a directory that end with .jpg and that have a filename with format DD_MM_YY-HHMM.jpg and renames them as YYMMDDHHMM.jpg

#alter the regex to suit your needs, each () will return a submatch
my $regex = qr!(\d\d)[-](\d\d)[-](\d\d)[_](\d\d\d\d)!;

while(<*.jpg>) {
        $oldname = $_;
        if ($oldname=~m/$regex/) {

                #$1,$2,etc are submatchs
                rename $oldname,$3.$2.$1.$4.".jpg";

Protect .svn directories using htaccess

// block access to .svn dirs
// should be done server-wide if you can (another snippet)

<IfModule mod_rewrite.c>
  RewriteRule ^(.*/)?\.svn/ - [F,L]
  ErrorDocument 403 "Access Forbidden"

Ubuntu 7.10: bootstrap a Ruby on Rails stack

// get started with ubuntu 7.1
// e.g. on a new slice from !

# 1) make yrself a user and stop being root

# 2) main web stack + ruby
sudo aptitude install -y make screen
sudo aptitude install -y apache2 php5 mysql-server
sudo aptitude install -y subversion postfix
sudo aptitude install -y ruby1.8 ruby1.8-dev rubygems irb
sudo aptitude install -y imagemagick librmagick-ruby1.8 librmagick-ruby-doc libfreetype6-dev xml-core

# 3) gemz
# rubygems appears to be broken as hell. 
# gem update --system does squat
# had to manually d/l rubygems 1.0.1, ruby setup.rb
tar xzvf rubygems-1.0.1.tgz
cd rubygems-1.0.1
sudo ruby setup.rb
sudo gem install rails mongrel merb map_by_method

[SAMBA] exclude file/directory from shared folder

// i have shared a directory and in this directory is one
// which i dont want to share! how can i exclude this?

  path = /var/test
  veto files = /var/test/private

// where /var/test/private can be a file or directory.

Compressing a directory with rar on Linux

I've been struggling to get this to work for so long that when I finally got it going I had to throw it up here so I wouldn't lose it.
rar a -m5 -R output.rar /etc/

This will create a max compression (not taking into account dictionary sizes and the like) archive of the entire etc directory.

Attach a screen before chrooting into your linux from scratch minimal system

This snippet is not so much a snippet as a tip. It's goal is to make your Linux from Scratch experience easier by allowing you the freedom of being able to attach and detach to a screen session from your host computer. Of course you would go through the book and change the chroot command to suit your needs, but just bear in mind that the chroot command you will use may be different from the one I use.

By using screen, you can start a lengthy compile and then detach, go about your business and re-attach another time. I use it primarily when I have to start a compilation from one computer, then later from another machine located somewhere else. Combined with SSH, this can be real handy.

desktop ~ # screen -S clfs
desktop ~ # chroot "${CLFS}" /tools/bin/env -i \
>     HOME=/root TERM="${TERM}" PS1='\u:\w\$ ' \
>     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
>     /tools/bin/bash --login +h

At any time, you can hit ctrl+a+d to detach. Then if you want to re-attach just type screen -r clfs and whamo, your right back where you left off.

Thanks again to ChrisS67 for helping me with my GCC problem (or me forgetting to install findutils) ;)

Mounting CIFS shares on Ubuntu with utf8 support

Just a simple reminder how to use the WD MyBook WE with Linux properly.

sudo aptitude install smbfs
sudo mkdir /media/sharename
sudo vim /etc/nsswitch.conf

hosts: files dns

to read
hosts: files wins dns

sudo aptitude install winbind

I mount it manually with
sudo mount -t cifs //MyBookWorld/PUBLIC /media/shares/MyBook/PUBLIC -o username=

grep - search for a string within files within a directory (and sub directories)

// description of your code here

-r gives you recursion
"redeem reward" is the string being searched for
/home/tom is the directory to start the search from

grep -r "redeem reward" /home/tom

Human Readable ls and df commands

Simple but useful to know...

To get a ls (file list) or df (disk free) to show in K,M or G instead of *huge* numbers of bytes without commas, use the -h flag.

ls -lh
df -h

postfix - flush queues / logs

Using the program: postsuper

There are three queues (active, deferred, incoming) and four log directories (bounce, defer, trace, flush).

To remove all mails in the deferred queue:

postsuper -d ALL deferred

Note that all of the queues and log dirs are located in:

wireless linux scan Access point and connect

// description of your code here

$ iwlist scan
$ iwconfig eth1 ESSID <MY_ESSID> mode Managed channel <MY_CHANNEL>

Channels and ESSID can be listed by issuing the command "iwlist scan".

reboot or shutdown linux server

how to shutdown a linux box with -f skip fsck file system check, -r reboot.

You must always specify a time or the word: now. Give time in minutes by using: +m

shutdown -rf +1 "Scheduled test of system reboot"

Will reboot the server in one minute, skipping fsck on startup.

SSH dynamic forward (Linux)

This command will create a dynamic forward from an SSH client to an SSH server. Basically what this does is allow you to use any local port (8080 in this example) as a proxy for any TCP application.

Feedback, suggestions and comments are all welcome!

# In the following example, we create a dynamic
# forward from port 8080 (client) to port 22 (

ssh -D 8080

# Now, we'll check out netstat to see what we
# have done.


# Active Internet connections (w/o servers)
# ...
# ...
# Awesome! Now we've got the connection. I'll add
# another post telling how to use this port as a
# socks proxy for any TCP application :)

send email with attachment from command line

useful for sending db backups

echo command is for the body
-s specifies the subject
-a gives the attachment
finally the recipient
email will be sent as root@serveraxis
echo "database backup for 070614" | mutt -s "backup: myerasmus_db_070614" -a erasmus_db_070614.sql.bz2.gpg [email address]

encrypt files using gpg

encrypt for recipient [Bob] [file]

gpg -e -r Bob file

decrypt, passphrase will be asked for
gpg --output <filename> -d [file] 

linux ubuntu ssh login

To replace the normal text password login with ssh public/private key pair....

Generate a pair at home (creates RSA based key pair)
ssh-keygen -t rsa

Enter in a passphrase when it asks.

upload the .pub key to the server
sftp root@<server address>
lcd /home/<your login>/.ssh

Login to the server normally as root.
Goto the .ssh directory
Append the public key to the authorized_keys
ssh root@<server address>
cd .ssh
cat >> authorized_keys

Then check that /etc/ssh/sshd_config has...
RSAAuthentication yes
PubkeyAuthentication yes

... in it

Restart the ssh server
/etc/init.d/ssh reload

Try logging in again as root and you should be prompted for passphrase to your ssh key.

ssh -v root@<server address>

to diagnose problems

linux server runlevel adminstration

To change system services from booting, or to set them to start at boot time use

First step is to create a symbolic link within /etc/init.d to the proggie that you want to service-ize

Here's an example with Apache 2.2
sudo ln -s /usr/local/sbin/apachectl /etc/init.d/apache2
sudo chmod +x /etc/init.d/apache2

Now we have to set which runlevels will use this program


This is a GUI interface into the various runlevels, what's currently running and not. Use space bar to
toggle whether a service is on or not.

NB: most servers run at runlevel:2

check which runlevel is default for the server by looking at:

postfix reload

To restart postfix and read and files from /etc/postfix

postfix reload
« Newer Snippets
Older Snippets »
53 total  XML / RSS feed