Linux

Linux server and desktop setup and usage

remove all build in Mac or Linux

In my mac development machine, I placed all of my projects in ~/src directory. For each projects, I already build different targets, debug, release, distribution and adhoc that will take up a lot of space for each project. To free up the space, I ran:

find . -type d -name "build" -print | xargs rm -rf

that will remove all sub-directory named build.

How to let postfix to allow some ip to relay email

To allow postfix accept email relaying from a specific ip adress, we can edit /etc/postfix/main.cf:

mynetworks = 127.0.0.0/8

to

mynetworks = 127.0.0.0/8, a.b.c.d/e

where a.b.c.d/e is the new group of ip address, or you can simply add one ip address in it. Restart postfix and now postfix will consider a.b.c.d/e as trusted ip and allow email from this ip address relaying email using this postfix mail server.

remove files in a directory with large number of files in Linux

When removing files in Linux using:

rm *

Sometimes it give the following error:

bash: /bin/rm: Argument list too long
When the number of files is hugh.

To fix this, we can use xargs:

ls | xargs rm

or

ls *.htm | xargs rm
that will remove all files with extesion .htm

Prevent steal image links using .htaccess

Sometimes prevent overload of both the web server and bandwidth, it is good to prevent unwanted link from other web site. We can add the following lines in .htaccess:

#prevent steal image link
SetEnvIfNoCase Referer "^http://www.waterworld.com.hk/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.waterworld.com.hk$" locally_linked=1
SetEnvIfNoCase Referer "^http://waterworld.com.hk/" locally_linked=1
SetEnvIfNoCase Referer "^http://waterworld.com.hk$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1
<FilesMatch "\.(gif|png|jpe?g)$">
Order Allow,Deny

Restrict users to SFTP only instead of SSH

Sometimes you want to have users, that have access to files on your server, but don't want them to be able to log in and execute commands on your server. This can be done easily. Add user as usually and assign him a password. Then run the following command (replace the 'username' with real user name):
root@host # usermod -s /usr/lib/sftp-server username
This changes user's shell to sftp-server. The last step for this to work is to add '/usr/lib/sftp-server' to /etc/shells to make it a valid shell, eg. like this:

Backup and restore partition in Linux

To backup and restore partition in Linux, we can make use of "dd" command. We can even backup a partition over network.

Firstly, make sure that the partition we want to backup is not using. If we want to backup our Linux partition, we can boot the LiveCD over the cdrom.

To backup a partition "dev/sda4" over network, we can:

dd if=/dev/hda1 bs=1M conv=sync,noerror | gzip -9 -c | ssh -c blowfish user@hostname "dd of=filename.gz bs=1M"

Remap key from ubuntu

For the same reason, I have to remap the right menu key to "Home" and right ctrl key to "End". Linux provide a command "setkeycodes" that can remap the key of the keyboard. To make the system remap keys automatically after each reboot, I wrote the following simple script:

/etc/init.d/mymapkey
#!/bin/sh
case "$1" in
  start)
    setkeycodes e05d 102 e01d 107
    ;;
  *)
        echo "Usage: /etc/init.d/mymapkey {start|stop}"

setup sound card for Lenovo Y410A on ubuntu 8.04

By default, sound card of my Lenovo Y410A laptop does not work on ubuntu 8.04. The OS can detect the sound card but no sound can be produce, or sometimes sound can be produced only after the laptop is waked up from suspension. To fix the problem, I have to add a line in the end of the folllowing file:

/etc/modprobe.d/alsa-base

options snd-hda-intel index=0 model=fujitsu

the line will set the "pin" of the sound card correctly, and I can get the sound correctly after rebooting the system.

zip file corrupted when downloading file using Internet Explorer from apache server

Recently a client compliant that download a zip files from my website and got file corrupted error.  After did some testing, I found that the problem happened on in Internet Explorer only, and the file downloaded is several bytes shorter than the one on the server.  And the zip file on the server is not corrupted.

Then I remembered that I did some changes on the apache server setting recently. and I suspected that it caused by the mod_deflate module of the apache. If I disable this module, the zip file can be download correctly without corruption.

Fix grub

When grub is overwritten by other OS installation, you can resintall it by:

  1. boot ubuntu liveCD
  2. sudo fdisk /dev/sda (assuming your harddisk is /dev/sda)
  3. type p and see the partition number of your linux partition (my case is sda6)
  4. mount partition sudo mount /dev/sda6 /boot (mount Linux partition to /boot)
  5. grub-install --root-directory=/boot /dev/sda (reinstall grub)

All done, and can reboot now.

Syndicate content