2008-07-03

FreeBSD 从零开始

FreeBSD这个东西属于配置一次一年不用管的东西,所以时间长了难免忘东忘西,这里记录下FreeBSD从零开始变成强悍Server的过程。 如果要用GNOME 之类的发行包的话还是Burn a DVD 最省心。 零七八碎的文件散落在4张CD上,如果你烧录一张DVD的话,生活就会简单很多

启动IPF防火墙

一个典型的IPF配置

#== default block
block in quick on rl0 from 192.168.0.0/16 to any
block in quick on rl0 from 172.16.0.0/12 to any
block in quick on rl0 from 10.0.0.0/8 to any
block in quick on rl0 from 127.0.0.0/8 to any
block in quick on rl0 from 0.0.0.0/8 to any
block in quick on rl0 from 169.254.0.0/16 to any
#== TCP
block in on rl0 proto tcp from any to any
block out on rl0 proto tcp from any to any
#service
pass in quick on rl0 proto tcp from any to any port=80 keep state
pass in quick on rl0 proto tcp from any to any port=443 keep state
#client
pass out quick on rl0 proto tcp from any to any port=20 keep state
pass out quick on rl0 proto tcp from any to any port=21 keep state
pass out quick on rl0 proto tcp from any to any port=80 keep state
pass out quick on rl0 proto tcp from any to any port=443 keep state
pass out quick on rl0 proto tcp from any to any port=5999 keep state #for cvsup/csup

###UDP
block in on rl0 proto udp all
block out on rl0 proto udp all
pass out quick on rl0 proto udp from any to any port=53 keep state

配置网络
如果是DHCP的话直接写ifconfig_xxx = "DHCP" 就可以了。


安装mysql

cd /usr/ports/databases/mysql51-server/
make
记得要在rc.conf中增加mysql_enable="YES",否则手工启动脚本也会没反应。


安装PHP

如果你打算用apache联php的话,还是不要使用sysinstall安装了,那样安装的结果是没有libphp5.so。老老实实去/usr/ports/lang/php5 下去make,记得带上apache22的选项。然后把下面的语句添加到合适的地方。
AddType application/x-httpd-php .php

AddType application/x-httpd-source .phps

书写PHP
奇怪的是,PHP似乎并不能自动使用UTF-8方式连接数据库,所以要在PHP中加入以下几句:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT=utf8");
mysql_query("SET CHARACTER_SET_RESULTS=utf8");


安装Apache

Apache可以使用sysinstall从DVD直接安装。
在/boot/loader.conf中可以增加accf_http_load="YES",这个accf的功能:function is to buffer incoming connections until a certain complete HTTP requests arrive.
具体见Http://www.mydigitallife.info/2006/04/23/freebsd-apache-http-accept-filter-error/

启用ftp & ssh
ftpd & ssh是inetd的一部分,所以需要配置和启用inetd

ftp stream tcp nowait root /usr/libexec/ftpd ftpd -l一行的#号去掉。


正确处理DHCP和resov.conf的关系

在/etc/dhclient.conf中加入
static dns:
interface "vr0"{
prepend domain-name-services xx.xx.xx.xx, x.x.x.x;
}


Color

$ export TERM=xterm

$ alias ls=’ls –G’



万一

万一你的网络在某个内网里,只能用proxy的话:
不要相信 export HTTP_PROXY=ip:port 可以确保你的make时引发的fetch能正确执行。那个是骗小孩的~~

躲在防火墙后
csh下设定Proxy的环境变量:
setenv http_proxy 192.168.14.1:8080
setenv ftp_proxy 192.168.14.1:8080
bash下:
export http_proxy=proxyserver:port
export ftp_proxy=proxyserver:port

使用WGET获取ports
上面设定好之后,让我们替换掉FreeBSD中最白痴的一个工具fetch,这个东西在配置了ftp_proxy甚至不能够穿越HTTP Proxy进行ftp获取操作,为此,我们必须启用wget - 或者更强大的多线程工具。如果你跟我一样在Firewall后面无发下手make wget的话,你可以从其他机器上直接copy一个 wget文件到本地的/usr/local/bin下。

然后在/etc/make.conf中添加如下文字,如果没有这个文件就手工建立:
FETCH_BINARY=/usr/local/bin/wget
FETCH_ARGS=-nc
DISABLE_SIZE=yes
DISABLE_SIZE必须设上,否则一个只有fetch才能解释的arg就跑到wget头上去了。
-nc是强制覆盖,必须设上,否则wget会不停的在后面增加.1 .2 .3 .4。


前面之所以要强调在环境里写是因为在网络上充斥这这样的白痴赋值写法:
FETCH_ENV=http_proxy=http://proxy2.zsu.edu.cn:3128
FETCH_ENV=ftp_proxy=http://proxy2.zsu.edu.cn:3128

这是错误的,只要有一点赋值的概念就知道为什么错了。。。


JDK
从安装复杂度到使用程度来说,装Diablo-jdk15之类的东西要远比jdk16要省心,要安全。

SUN-WTK
very few people come to this far,however I need this.

First of all you should enable linux binary compatible : kldload linux and you should also put linux_enable="YES" in

rc.conf.

And then you should build /usr/ports/emulators/linux_base-fc4, yes perverify required some DLLs.
then you should build /usr/ports/x11/linux-xorg-libs, yes preverify1.1 link to X11.


Freebsd下ZIP不可乱用

如果你使用ZIP去压缩/usr/ports,哪怕是设定了零压缩可需要几个小时。
如果用tar cf 的话,只需要几十秒。

天大的差距啊~~~


用rsync对网站进行镜像备份

by inburst
http://xfocus.org

对系统管理员来说,平时的工作重心应该集中在维护系统正常运转,能够正常提供服务上,这里往往牵涉到一个数据备份的问题,在我所了解

的情况中,有80%的系统管理员不是太关心自己服务器的安全性,但往往对备分镜像的技术相当感兴趣,但由于商业产品的软硬件价格都相当高

昂,因此往往会选择自由软件。这里准备介绍的rsync就是这样的软件,它可以满足绝大多数要求不是特别高的备份需求。

一、特性简介

rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync。它的特性如下:

1、可以镜像保存整个目录树和文件系统。
2、可以很容易做到保持原来文件的权限、时间、软硬链接等等。
3、无须特殊权限即可安装。
4、优化的流程,文件传输效率高。
5、可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
6、支持匿名传输。

二、使用方法

rsync的使用方法很简单,我就举自己使用的例子来说明吧。

1、系统环境

rsync支持大多数的类unix系统,无论是Linux、Solaris还是BSD上都经过了良好的测试。我的系统环境为:

server: FreeBSD 4.3 ip: 192.168.168.52
client: Solaris 8 ip: 192.168.168.137
rsync 版本 2.4.6(可以从http://rsync.samba.org/rsync/获得最新版本)

2、配置server端的/etc/rsyncd.conf文件

bash-2.03# cat /etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = no # 不使用chroot
max connections = 4 # 最大连接数为4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log # 日志记录文件

[inburst] # 这里是认证的模块名,在client端需要指定
path = /home/inburst/python/ # 需要做镜像的目录
comment = BACKUP CLIENT IS SOLARIS 8 E250
ignore errors # 可以忽略一些无关的IO错误
read only = yes # 只读
list = no # 不允许列文件
auth users = inburst # 认证的用户名,如果没有这行,则表明是匿名
secrets file = /etc/inburst.pas # 认证文件名

[web]
path = /usr/local/apache/htdocs/
comment = inburst.org web server

3、在server端生成一个密码文件/etc/inburst.pas

bash-2.03# cat /etc/inburst.pas
inburst:hack

出于安全目的,文件的属性必需是只有属主可读。

4、在server端将rsync以守护进程形式启动

bash-2.03# rsync --daemon

如果要在启动时把服务起来,有几种不同的方法,比如:

a、加入inetd.conf

编辑/etc/services,加入rsync 873/tcp,指定rsync的服务端口是873
编加/etc/inetd.conf,加入rsync stream tcp nowait root /bin/rsync rsync --daemon

b、加入rc.local

在各种操作系统中,rc文件存放位置不尽相同,可以修改使系统启动时rsync --daemon加载进去。

5、从client端进行测试

下面这个命令行中-vzrtopg里的v是verbose,z是压缩,r是recursive,topg都是保持文件原有属性如属主、时间的参数。--progress是指显示

出详细的进度情况,--delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致。后面的inburst@ip中,

inburst是指定密码文件中的用户名,之后的::inburst这一inburst是模块名,也就是在/etc/rsyncd.conf中自定义的名称。最后的/tmp是备份

到本地的目录名。

在这里面,还可以用-e ssh的参数建立起加密的连接。可以用--password-file=/password/path/file来指定密码文件,这样就可以在脚本中使

用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。

bash-2.03# rsync -vzrtopg --progress --delete inburst@192.168.168.52::inburst /tmp/
Password:
receiving file list ... done
./
1
785 (100%)
1.py
4086 (100%)
2.py
10680 (100%)
a
0 (100%)
ip
3956 (100%)
./
wrote 190 bytes read 5499 bytes 758.53 bytes/sec
total size is 19507 speedup is 3.43

6、创建更新脚本

如果有比较复杂的工作,利用一些常见的脚本语言可以有帮助。比如:

bash-2.03# cat /usr/local/bin/rsync.sh

#!/bin/sh
DATE=`date +%w`

rsync -vzrtopg --progress --delete inburst@192.168.168.52::inburst /home/quack/backup/$DATE --password-file=/etc/rsync.pass >

/var/log/rsync.$DATE

7、修改/etc/crontab做好定时

比如:

bash-2.03# echo "15 4 * * 6 root rsync.sh">>/etc/crontab

三、FAQ

Q:如何通过ssh进行rsync,而且无须输入密码?
A:可以通过以下几个步骤

1. 通过ssh-keygen在server A上建立SSH keys,不要指定密码,你会在~/.ssh下看到identity和identity.pub文件
2. 在server B上的home目录建立子目录.ssh
3. 将A的identity.pub拷贝到server B上
4. 将identity.pub加到~[user b]/.ssh/authorized_keys
5. 于是server A上的A用户,可通过下面命令以用户B ssh到server B上了
e.g. ssh -l userB serverB
这样就使server A上的用户A就可以ssh以用户B的身份无需密码登陆到server B上了。

Q:如何通过在不危害安全的情况下通过防火墙使用rsync?
A:解答如下:

这通常有两种情况,一种是服务器在防火墙内,一种是服务器在防火墙外。
无论哪种情况,通常还是使用ssh,这时最好新建一个备份用户,并且配置sshd仅允许这个用户通过RSA认证方式进入。
如果服务器在防火墙内,则最好限定客户端的IP地址,拒绝其它所有连接。
如果客户机在防火墙内,则可以简单允许防火墙打开TCP端口22的ssh外发连接就ok了。

Q:我能将更改过或者删除的文件也备份上来吗?
A:当然可以:

你可以使用如:rsync -other -options -backupdir = ./backup-2000-2-13 ...这样的命令来实现。
这样如果源文件:/path/to/some/file.c改变了,那么旧的文件就会被移到./backup-2000-2-13/path/to/some/file.c,这里这个目录需要自己

手工建立起来

Q:我需要在防火墙上开放哪些端口以适应rsync?
A:视情况而定

rsync可以直接通过873端口的tcp连接传文件,也可以通过22端口的ssh来进行文件传递,但你也可以通过下列命令改变它的端口:

rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:

Q:我如何通过rsync只复制目录结构,忽略掉文件呢?
A:rsync -av --include '*/' --exclude '*' source-dir dest-dir

Q:为什么我总会出现"Read-only file system"的错误呢?
A:看看是否忘了设"read only = no"了

Q:为什么我会出现'@ERROR: invalid gid'的错误呢?
A:rsync使用时默认是用uid=nobody;gid=nobody来运行的,如果你的系统不存在nobody组的话,就会出现这样的错误,可以试试gid =

nogroup或者其它

Q:绑定端口873失败是怎么回事?
A:如果你不是以root权限运行这一守护进程的话,因为1024端口以下是特权端口,会出现这样的错误。你可以用--port参数来改变。

Q:为什么我认证失败?
A:从你的命令行看来:

你用的是:
> bash$ rsync -a 144.16.251.213::test test
> Password:
> @ERROR: auth failed on module test
>
> I dont understand this. Can somebody explain as to how to acomplish this.
> All suggestions are welcome.

应该是没有以你的用户名登陆导致的问题,试试rsync -a max@144.16.251.213::test test

四、一些可借鉴的脚本

这里这些脚本都是rsync网站上的例子:

1、每隔七天将数据往中心服务器做增量备份

#!/bin/sh

# This script does personal backups to a rsync backup server. You will end up
# with a 7 day rotating incremental backup. The incrementals will go
# into subdirectories named after the day of the week, and the current
# full backup goes into a directory called "current"
# tridge@linuxcare.com

# directory to backup
BDIR=/home/$USER

# excludes file - this contains a wildcard pattern per line of files to exclude
EXCLUDES=$HOME/cron/excludes

# the name of the backup machine
BSERVER=owl

# your password on the backup server
export RSYNC_PASSWORD=XXXXXX


########################################################################

BACKUPDIR=`date +%A`
OPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=/$BACKUPDIR -a"

export PATH=$PATH:/bin:/usr/bin:/usr/local/bin

# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BSERVER::$USER/$BACKUPDIR/
rmdir $HOME/emptydir

# now the actual transfer
rsync $OPTS $BDIR $BSERVER::$USER/current

2、备份至一个空闲的硬盘

#!/bin/sh

export PATH=/usr/local/bin:/usr/bin:/bin

LIST="rootfs usr data data2"

for d in $LIST; do
mount /backup/$d
rsync -ax --exclude fstab --delete /$d/ /backup/$d/
umount /backup/$d
done

DAY=`date "+%A"`

rsync -a --delete /usr/local/apache /data2/backups/$DAY
rsync -a --delete /data/solid /data2/backups/$DAY

3、对vger.rutgers.edu的cvs树进行镜像

#!/bin/bash

cd /var/www/cvs/vger/
PATH=/usr/local/bin:/usr/freeware/bin:/usr/bin:/bin

RUN=`lps x | grep rsync | grep -v grep | wc -l`
if [ "$RUN" -gt 0 ]; then
echo already running
exit 1
fi

rsync -az vger.rutgers.edu::cvs/CVSROOT/ChangeLog $HOME/ChangeLog

sum1=`sum $HOME/ChangeLog`
sum2=`sum /var/www/cvs/vger/CVSROOT/ChangeLog`

if [ "$sum1" = "$sum2" ]; then
echo nothing to do
exit 0
fi

rsync -az --delete --force vger.rutgers.edu::cvs/ /var/www/cvs/vger/
exit 0

4、利用find的一种巧妙方式

rsync -avR remote:'`find /home -name "*.[ch]"`' /tmp/

可以用这种方法列出需要备份的文件列表——这种方法似乎比较少人用到。

五、参考资料:

1、http://rsync.samba.org/
2、rsync examples
3、rsync FAQ

2008-07-02

Creating Your Own FreeBSD 7.0 DVD

[http://it.toolbox.com/blogs/bsd-guru/creating-your-own-freebsd-70-dvd-22791]

Dru (SysAdmin, Technical Writer, Technical Trainer) posted 2/28/2008 | Comments (32)
If you're in the FreeBSD world, you've probably already downloaded or csup'd FreeBSD 7.0 or are in the process of doing so now. As yes, after what seemed like waiting forever, the announcement of 7.0 went out last night.

I spent this morning making a DVD ISO of FreeBSD 7.0 to be included in the upcoming BSD magazine. Creating a DVD is easy and my instructions follow.

Start by downloading the following four files from your closest FreeBSD mirror; you'll find these files in the ISO-IMAGES-i386/7.0/ directory:

7.0-RELEASE-i386-disc1.iso
7.0-RELEASE-i386-disc2.iso
7.0-RELEASE-i386-disc3.iso
7.0-RELEASE-i386-docs.iso

Note: all of the mirrors are extremely busy today so you may have to try a few before you find one close to you that has some connections left. I saved mine to the download subdirectory of my home directory. I created a staging area, became the superuser and ran the following commands to mount and untar the contents of the 4 files:

cd ~dru/downloads/dvd-freebsd7/
mdconfig -a -t vnode -f ../7.0-RELEASE-i386-disc1.iso -u 0
mount -t cd9660 /dev/md0 /mnt
tar -C /mnt -cf - . | tar -xf -
umount /mnt

mdconfig -a -t vnode -f ../7.0-RELEASE-i386-disc2.iso -u 1
mount -t cd9660 /dev/md1 /mnt
tar -C /mnt -cf - . | tar -xf -
umount /mnt

mdconfig -a -t vnode -f ../7.0-RELEASE-i386-disc3.iso -u 2
mount -t cd9660 /dev/md2 /mnt
tar -C /mnt -cf - . | tar -xf -
umount /mnt

mdconfig -a -t vnode -f ../7.0-RELEASE-i386-docs.iso -u 3
mount -t cd9660 /dev/md3 /mnt
tar -C /mnt -cf - . | tar -xf -
umount /mnt


Next, use sed or the replace function of your favourite text editor to remove all incidences of ||1 and ||2 and ||3 from the file packages/INDEX.

Add these lines to cdrom.inf:

CD_VOLUME = 0
CD_VOLUME = 1
CD_VOLUME = 2
CD_VOLUME = 3

Then, remove this directory:

rm -Rf rr_moved

or you will get this error when you try to create your DVD:

mkisofs: Error: './rr_moved' and '(NULL POINTER)' have the same Rock Ridge name 'rr_moved'.
mki/usr/home/dlavigne6/sofs: Unable to sort directory
:-( write failed: Input/output error

This command (type all on one line) will burn your DVD on the fly; note that it will NOT save a copy of the ISO to your hard disc:

growisofs -Z /dev/cd0 -J -R -no-emul-boot -b boot/cdboot -iso-level 3 .

To instead create an ISO which you can then burn at your leisure, use this command (typed all on one line):

mkisofs -V FreeBSD7 -J -R -b boot/cdboot -no-emul-boot -o freebsd7.iso .

and to burn the ISO:

growisofs -dvd-compat -Z /dev/cd0=freebsd7.iso

That's it. Have fun!