unix 基本知识

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

原文来自 Learn UNIX in 10 minutes

Directories:
目录操作

File and directory paths in UNIX use the forward slash “/”
to separate directory names in a path.

examples:

/ “root” directory
/usr directory usr (sub-directory of / “root” directory)
/usr/STRIM100 STRIM100 is a subdirectory of /usr

Moving around the file system:

pwd Show the “present working directory”, or current directory.
cd Change current directory to your HOME directory.
cd /usr/STRIM100 Change current directory to /usr/STRIM100.
cd INIT Change current directory to INIT which is a sub-directory of the current
directory.
cd .. Change current directory to the parent directory of the current directory.
cd $STRMWORK Change current directory to the directory defined by the environment
variable ‘STRMWORK’.
cd ~bob Change the current directory to the user bob’s home directory (if you have permission).

Listing directory contents:
显示目录内容

ls list a directory
ls -l list a directory in long ( detailed ) format
显示详细内容

for example:
$ ls -l
drwxr-xr-x 4 cliff user 1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r–r– 1 cliff user 767392 Jun 6 14:28 scanlib.tar.gz
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
| | | | | | | | | | |
| | | | | owner group size date time name
| | | | number of links to file or directory contents
| | | permissions for world
| | permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others…

ls -a List the current directory including hidden files. Hidden files start
with “.”
显示包括隐藏文件(.开头文件)

ls -ld * List all the file and directory names in the current directory using
long format. Without the “d” option, ls would list the contents
of any sub-directory of the current. With the “d” option, ls
just lists them like regular files.
d 参数?

Changing file permissions and attributes
改变文件权限

chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for
the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)
chgrp user file Makes file belong to the group user.
改变组

chown cliff file Makes cliff the owner of file.
改变所有者
chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree.

You must be the owner of the file/directory or be root before you can do any of these things.

Moving, renaming, and copying files:
移动,改名和拷贝

cp file1 file2 copy a file
mv file1 newname move or rename a file
mv file1 ~/AAA/ move file1 into sub-directory AAA in your home directory.
rm file1 [file2 ...] remove or delete a file
rm -r dir1 [dir2...] recursivly remove a directory and its contents BE CAREFUL!
mkdir dir1 [dir2...] create directories
mkdir -p dirpath create the directory dirpath, including all implied directories in the path.
rmdir dir1 [dir2...] remove an empty directory

Viewing and editing files:
查看文件

cat filename Dump a file to the screen in ascii.
more filename Progressively dump a file to the screen: ENTER = one line down
SPACEBAR = page down q=quit
回车-下一行;空格-向下翻页

less filename Like more, but you can use Page-Up too. Not on all systems.
类似于 more,但可向上翻页

vi filename Edit a file using the vi editor. All UNIX systems will have vi in some form.
emacs filename Edit a file using the emacs editor. Not all systems will have emacs.

head filename Show the first few lines of a file.
显示开始部分内容
head -n filename Show the first n lines of a file.
显示开始部分n行内容

tail filename Show the last few lines of a file.
tail -n filename Show the last n lines of a file.

Shells

The behavior of the command line interface will differ slightly depending
on the shell program that is being used.

Depending on the shell used, some extra behaviors can be quite nifty.

You can find out what shell you are using by the command:

echo $SHELL

Of course you can create a file with a list of shell commands and execute it like
a program to perform a task. This is called a shell script. This is in fact the
primary purpose of most shells, not the interactive command line behavior.

Environment variables
环境变量

You can teach your shell to remember things for later using environment variables.
For example under the bash shell:

export CASROOT=/usr/local/CAS3.0 Defines the variable CASROOT with the value
/usr/local/CAS3.0.
export LD_LIBRARY_PATH=$CASROOT/Linux/lib Defines the variable LD_LIBRARY_PATH with
the value of CASROOT with /Linux/lib appended,
or /usr/local/CAS3.0/Linux/lib

By prefixing $ to the variable name, you can evaluate it in any command:

cd $CASROOT Changes your present working directory to the value of CASROOT

echo $CASROOT Prints out the value of CASROOT, or /usr/local/CAS3.0
printenv CASROOT Does the same thing in bash and some other shells.

Interactive History
交互式的历史记录

A feature of bash and tcsh (and sometimes others) you can use
the up-arrow keys to access your previous commands, edit
them, and re-execute them.

Filename Completion
文件名自动补全

A feature of bash and tcsh (and possibly others) you can use the
TAB key to complete a partially typed filename. For example if you
have a file called constantine-monks-and-willy-wonka.txt in your
directory and want to edit it you can type ‘vi const’, hit the TAB key,
and the shell will fill in the rest of the name for you (provided the
completion is unique).

Bash is the way cool shell.

Bash will even complete the name of commands and environment variables.
And if there are multiple completions, if you hit TAB twice bash will show
you all the completions. Bash is the default user shell for most Linux systems.

Redirection:
重定向

grep string filename > newfile Redirects the output of the above grep
command to a file ‘newfile’.
grep string filename >> existfile Appends the output of the grep command
to the end of ‘existfile’.
重定向,追加到 existfile 的结尾

The redirection directives, > and >> can be used on the output of most commands
to direct their output to a file.

Pipes:
管道命令

The pipe symbol “|” is used to direct the output of one command to the input
of another.

For example:

ls -l | more This commands takes the output of the long format directory list command
“ls -l” and pipes it through the more command (also known as a filter).
In this case a very long list of files can be viewed a page at a time.

du -sc * | sort -n | tail
The command “du -sc” lists the sizes of all files and directories in the
current working directory. That is piped through “sort -n” which orders the
output from smallest to largest size. Finally, that output is piped through “tail”
which displays only the last few (which just happen to be the largest) results.
du -sc 显示当前目录所有文件和子目录的大小。
sort -n 从小到大排序
tail 只显示最后的结果

Command Substitution

You can use the output of one command as an input to another command in another way
called command substitution. Command substitution is invoked when by enclosing the
substituted command in backwards single quotes. For example:

cat `find . -name aaa.txt`

which will cat ( dump to the screen ) all the files named aaa.txt that exist in the current
directory or in any subdirectory tree.

Searching for strings in files: The grep command

grep string filename prints all the lines in a file that contain the string

Searching for files : The find command

find search_path -name filename

find . -name aaa.txt Finds all the files named aaa.txt in the current directory or
any subdirectory tree.
find / -name vimrc Find all the files named ‘vimrc’ anywhere on the system.
find /usr/local/games -name “*xpilot*”
Find all files whose names contain the string ‘xpilot’ which
exist within the ‘/usr/local/games’ directory tree.

Reading and writing tapes, backups, and archives: The tar command

The tar command stands for “tape archive”. It is the “standard” way to read
and write archives (collections of files and whole directory trees).

Often you will find archives of stuff with names like stuff.tar, or stuff.tar.gz. This
is stuff in a tar archive, and stuff in a tar archive which has been compressed using the
gzip compression program respectivly.

Chances are that if someone gives you a tape written on a UNIX system, it will be in tar format,
and you will use tar (and your tape drive) to read it.

Likewise, if you want to write a tape to give to someone else, you should probably use
tar as well.

Tar examples:

tar xv Extracts (x) files from the default tape drive while listing (v = verbose)
the file names to the screen.
tar tv Lists the files from the default tape device without extracting them.
tar cv file1 file2
Write files ‘file1′ and ‘file2′ to the default tape device.
tar cvf archive.tar file1 [file2...]
Create a tar archive as a file “archive.tar” containing file1,
file2…etc.
tar xvf archive.tar extract from the archive file
tar cvfz archive.tar.gz dname
Create a gzip compressed tar archive containing everything in the directory
‘dname’. This does not work with all versions of tar.
tar xvfz archive.tar.gz
Extract a gzip compressed tar archive. Does not work with all versions of tar.
tar cvfI archive.tar.bz2 dname
Create a bz2 compressed tar archive. Does not work with all versions of tar

File compression: compress, gzip, and bzip2

The standard UNIX compression commands are compress and uncompress. Compressed files have
a suffix .Z added to their name. For example:

compress part.igs Creates a compressed file part.igs.Z

uncompress part.igs Uncompresseis part.igs from the compressed file part.igs.Z.
Note the .Z is not required.

Another common compression utility is gzip (and gunzip). These are the GNU compress and
uncompress utilities. gzip usually gives better compression than standard compress,
but may not be installed on all systems. The suffix for gzipped files is .gz

gzip part.igs Creates a compressed file part.igs.gz
gunzip part.igs Extracts the original file from part.igs.gz

The bzip2 utility has (in general) even better compression than gzip, but at the cost of longer
times to compress and uncompress the files. It is not as common a utility as gzip, but is
becoming more generally available.

bzip2 part.igs Create a compressed Iges file part.igs.bz2
bunzip2 part.igs.bz2 Uncompress the compressed iges file.

Looking for help: The man and apropos commands

Most of the commands have a manual page which give sometimes useful, often more or less
detailed, sometimes cryptic and unfathomable discriptions of their usage. Some say they
are called man pages because they are only for real men.

Example:

man ls Shows the manual page for the ls command

You can search through the man pages using apropos

Example:

apropos build Shows a list of all the man pages whose discriptions contain the word “build”

Do a man apropos for detailed help on apropos.

Basics of the vi editor

Opening a file
vi filename

Creating text
Edit modes: These keys enter editing modes and type in the text
of your document.

i Insert before current cursor position
I Insert at beginning of current line
a Insert (append) after current cursor position
A Append to end of line
r Replace 1 character
R Replace mode
Terminate insertion or overwrite mode

Deletion of text

x Delete single character
dd Delete current line and put in buffer
ndd Delete n lines (n is a number) and put them in buffer
J Attaches the next line to the end of the current line (deletes carriage return).

Oops

u Undo last command

cut and paste
yy Yank current line into buffer
nyy Yank n lines into buffer
p Put the contents of the buffer after the current line
P Put the contents of the buffer before the current line

cursor positioning
^d Page down
^u Page up
:n Position cursor at line n
:$ Position cursor at end of file
^g Display current line number
h,j,k,l Left,Down,Up, and Right respectivly. Your arrow keys should also work if
if your keyboard mappings are anywhere near sane.

string substitution

:n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines
n1 to n2. If g is included (meaning global),
all instances of string1 on each line
are substituted. If g is not included,
only the first instance per matching line is
substituted.

^ matches start of line
. matches any single character
$ matches end of line

These and other “special characters” (like the forward slash) can be “escaped” with \
i.e to match the string “/usr/STRIM100/SOFT” say “\/usr\/STRIM100\/SOFT”

Examples:

:1,$:s/dog/cat/g Substitute ‘cat’ for ‘dog’, every instance
for the entire file - lines 1 to $ (end of file)

:23,25:/frog/bird/ Substitute ‘bird’ for ‘frog’ on lines
23 through 25. Only the first instance
on each line is substituted.

Saving and quitting and other “ex” commands

These commands are all prefixed by pressing colon (:) and then entered in the lower
left corner of the window. They are called “ex” commands because they are commands
of the ex text editor - the precursor line editor to the screen editor
vi. You cannot enter an “ex” command when you are in an edit mode (typing text onto the screen)
Press to exit from an editing mode.

:w Write the current file.
:w new.file Write the file to the name ‘new.file’.
:w! existing.file Overwrite an existing file with the file currently being edited.
:wq Write the file and quit.
:q Quit.
:q! Quit with no changes.

:e filename Open the file ‘filename’ for editing.

:set number Turns on line numbering
:set nonumber Turns off line numbering

Popularity: 9% [?]

Tags:

ssh,vnc 反向连接

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

反向连接不需要网关映射,因此不需要惊动网管大人,这样我就可以从家里(ADSL)连接控制单位局域网中的我的机器…
嘿嘿…坏处就是,两头都要有人…
感谢 pal(小时)蹲在我家里帮忙测试。

ssh 反向连接

如果没有安装 ssh的话,安装之

sudo apt-get install ssh

被控端,执行

ssh -f -N -R 10000:localhost:22 guoshuang@xxx.xxx.xxx.xxx

控制端执行
ssh guoshuang@localhost -p 10000

参考此文

vnc 反向连接

其实只要网关(防火墙)没有关掉 5500 就可以。注意:是5500能出去即可,不是进来,所以一般都是开放的。

sudo apt-get install x11vnc

控制端执行下面命令,等待邀请

vncviewer -listen

被控端,主动发起邀请

x11vnc -connect xxx.xxx.xxx.xxx:5500

参考此文

windows 和 mac 下的软件可参考下文

Free and easy remote access (through firewalls and NAT routers) with VNC reverse connections
内网远程控制全攻略

注意:vncviewer -fullscreen 模式全屏连接 vnc,用 f8 或者 ctrl+esc esc 退出。即便如此,也会经常出现不能退出或者控制失灵的情况,这篇 patch: fix WM bindings for vncviewer -fullscreen 应该有些帮助

或者使用 xvnc4viewer(sudo apt-get install xvnc4viewer)而不是tightvnc viewer。

f8 和窗口控制都没问题。

Popularity: 37% [?]

Tags: , , , , , , , ,

Linux操作系统的8个经典技巧(转)

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

1、处理特殊的文件名

  假设Linux系统中有一个文件名叫“-ee”,如果我们想对它进行操作,例如要删除它,按照一般的删除方法在命令行中输入rm -ee命令,界面会提示我们是“无效选项”(invalid option),原来由于文件名的第一个字符为“-”,Linux把文件名当作选项了,我们可以使用“–”符号来解决这个问题,输入“rm — -ee”命令便可顺利删除名为“-ee”的文件。如果是其他特殊字符的话可以在特殊字符前加一个“”符号,或者用双引号把整个文件名括起来。

  2、直接进行Linux的安装工作

  在安装Linux操作系统时,可以利用该系统光盘中的一个名为“loadlin.exe”的软件,将Linux核心直接调入内存,由Linux核心代替当前操作系统来接管计算机,并进入Linux的安装界面。在安装Linux时,我们只要在运行对话框中输入“loadlin E:imagesvmlinuz root=/dev/ram initrd=E:imagesinitrd.img”这个命令就可以直接安装Linux了;其中“E:imagesvmlinuz”为Linux的核心名。

  3、消除Xwindows下的死机现象

  我们可以用两个常用的方法来消除这种现象:第一,用键盘上的复合键“Ctrl+Alt+Backspace”来关闭当前正在运行的任务;第二,首先按住键盘上的“Ctrl+Alt+F2”复合键,让系统切换到另一个操作台,然后登录到系统,再执行“#ps -ax/grep startx”命令,这将会列出你的Xserver的进程标识,接着在命令行中输入如下命令就能消除Xwindows下的死机现象:#kill -9 PID_Number,最后通过“Alt+F1”复合键返回到原来的平台。

  4、快速关闭Linux系统

  最新版本的Linux/UNIX系统借鉴了大型机的技术,采用了抗掉电的日志式文件系统,可以自动跟踪保存用户数据,自动同步刷新文件系统,用户完全可以随手关闭电源,从而达到快速关闭系统的目的。

  5、巧妙使用“rm”命令

  我们可以使用带“-r”参数的“rm”命令来删除一个非空目录,例如我们在命令行中输入“rm -r bbb”这样的命令,表示系统将把bbb目录中包含的所有文件和子目录全部删除掉。

  6、巧妙使用“Tab”键

  大家知道在Linux字符界面中输入命令时,有时需要输入很多字符,如果经常这样逐个地输入字符,比较麻烦。假设键入的字符足以确定该目录下一个惟一的文件时,我们只需按键盘上的“Tab”键就可以自动补齐该文件名的剩下部分,例如要把目录/ccc下的文件“ddddddd-1.2.3.tar.gz”解包时,当我们在命令行中键入到“tar xvfz /ccc/d”时,如果该文件是该目录下惟一以“d”打头的文件的话就可以直接按下“Tab”键,这时命令会被自动补齐为:tar xvfz /ccc/ddddddd-1.2.3.tar.gz ,从而提高了输入效率。

  7、多用鼠标拷贝与粘贴来提高操作速度

  Linux系统安装后,每次启动到字符界面时都会自动运行一个叫“gpm”的程序,该程序运行后就可以用鼠标来拷贝与粘贴了。具体做法是按住鼠标左键拖动使要拷贝的地方突出显示,这时突出显示的区域已经被拷贝,再按鼠标右键拷贝的内容就会被粘贴在光标所在位置了。如果我们在Xwindow下运行Linux 系统,拷贝与粘贴的操作与在Windows 9x系统下一样。

  8、快速启动Linux系统

  在DOS下,有一种简单快速启动Linux的方法,那就是load Linux。loadlin.exe是DOS下的可执行程序,它可以在纯DOS环境下迅速启动Linux,而且无需重启计算机,通常我们可以在光盘的 “/kernels”目录下找到这个程序。如果不知这个程序被放置于安装盘的何处,可以使用“find -name loadlin*”命令来寻找。找到之后将其复制到DOS分区中,同时还需要复制一份你所使用的Linux内核文件。可以通过Windows直接从光盘复制,也可在Linux环境下使用“mcopy”命令将文件复制到DOS分区;接着再编写一个名为“Linux.bat”的批处理文件,文件内容如下:c: loadlin c:vmlinuz root=/dev/hda1 ro(我们假设loadlin.exe和vmlinuz这两个内核文件都在c盘根目录下,“root”为Linux的根设备,而且Linux处于硬盘第一分区,所以设备名为“/dev/hda1”,“ro”意为readonly)。以后在DOS下要启动Linux时,运行“Linux.bat”就可以了。

via lupa

Popularity: 17% [?]

Tags: , , , , ,

sed-非交互式文本编辑器

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

sed 是在 UNIX ® 操作系统上运行的一个非交互式上下文编辑器。sed 被设计在下列三种情况下发挥作用:

* 1) 编辑那些对舒适的交互式编辑而言太大的文件。
* 2) 在编辑命令太复杂而难于在交互模式下键入的时候编辑任何大小的文件。
* 3) 要在对输入的一趟扫描中有效的进行多个‘全局’编辑函数。

完整文章在 sed - 非交互式文本编辑器

Popularity: 12% [?]

Tags: , ,

linux 服务器配置资料二

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

as4+postfix+cyrus-sasl+mysql+postfixadmin+courier-imap+courier-maildrop+squirrelmail+clamav+spamassassin+amavisd-new

转载请注明出处
最后更新日期:2006年8月3日
2004年10左右成稿

1.mysql

2.apache

3.php

4.cyrus-sasl

5.postfix

6.postfixadmin

7.courier-authlib

8.courier-imap

9.courier-maildrop

10.测试

11.webmail
11.1.squirrelmail

12.clamav

13.amavisd-new

14.spamassassin

15.附:启动脚本

本文用到的软件

MySQL 5.0.15
网站:http://www.mysql.com
下载
APACHE 2.0.55
网站:http://www.apache.org
下载
PHP 4.4.1
网站:http://www.php.net
下载
Cyrus-SASL 2.1.21
网站:http://asg.web.cmu.edu/sasl
下载
Postfix 2.2.5
网站:http://www.postfix.org
下载
PostfixAdmin 2.1.0
网站:http://www.postfixadmin.com
下载
Courier-authlib 0.57
网站:http://www.courier-mta.org/authlib
下载
Courier-IMAP 4.0.6
网站:http://www.courier-mta.org/imap
下载
Courier-maildrop 2.0.1
网站:http://www.courier-mta.org/maildrop
下载
SquirrelMail 1.4.5
网站:http://www.squirrelmail.org
下载
Extmail 0.20
网站:http://www.extmail.org
下载
clamav 0.87
网站:http://www.clamav.net
下载
amavisd-new 2.3.3
网站:http://www.ijs.si/software/amavisd
下载
Spamassassin 3.1.0
网站:http://spamassassin.apache.org
下载
我以前那篇文章中密码加密有问题(密码一加密,smtp认证就通不过),所以下定决心更新一下。这次sasl密码验证机制改为authdaemond(感谢网友606),并把测试部分单独列出来。很多人问起我的安装环境,我安装linux的时候,只选了开发工具,其他的都没选,还有,这些软件包安装的时候都没有依赖性问题,有的话,我也提出来了。

1.安装 mysql 5.0.15
# wget http://dev.mysql.com/get/Downloads/MySQL-5…ysql.new21.com/
# tar zxvf mysql-5.0.15.tar.gz
# cd cd mysql-5.0.15
# groupadd mysql
# useradd -g mysql mysql
# CFLAGS=”-O3″ CXX=gcc CXXFLAGS=”-O3 -felide-constructors -fno-exceptions -fno-rtti” \
./configure –prefix=/usr/local/mysql \
–enable-assembler –with-mysqld-ldflags=-all-static –with-charset=gbk
# make
# make install
# cp support-files/my-medium.cnf /etc/my.cnf

设置自启动
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig –add mysqld

安装完以后要初始化数据库
# cd /usr/local/mysql
# /usr/local/mysql/bin/mysql_install_db –user=mysql
# chown -R root .
# chown -R mysql var
# chgrp -R mysql .

好了,至此mysql安装完毕,你可以这样起动你的mysql服务
# service mysqld start

为了能让系统找到mysql,请运行如下命令
# PATH=$PATH:/usr/local/mysql/bin
# export PATH
# echo “/usr/local/mysql/lib/mysql” >> /etc/ld.so.conf
# ldconfig

Go to top.

2.安装 apache 2.0.55
# wget http://apache.freelamp.com/httpd/httpd-2.0.55.tar.bz2
# tar jxvf httpd-2.0.55.tar.bz2
# cd httpd-2.0.55
# ./configure –prefix=/usr/local/apache
# make
# make install

设置自启动
# cp support/apachectl /etc/init.d/httpd

修改/etc/init.d/httpd
# vi /etc/init.d/httpd(前面几行改成如下样子)
#!/bin/sh
#
# Startup script for the Apache Web Server
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/log/httpd.pid
# config: /usr/local/apache/conf/httpd.conf

# chkconfig –add httpd
# chmod 755 /etc/init.d/httpd
# chkconfig httpd on

创建网页根目录
# mkdir /var/www

修改apache配置文件
# vi /usr/local/apache/conf/httpd.conf
//存放网页的目录,原来为DocumentRoot “”,改成:
DocumentRoot “/var/www”
//这句应该和DocumentRoot 的目录保持一致,原来为,改成:

//Indexes:当在目录中找不到DirectoryIndex列表中指定的文件就生成当前目录的文件列表
//FollowSymlinks:允许符号链接跟随,访问不在本目录下的文件
Options Indexes FollowSymLinks
//禁止读取.htaccess配置文件的内容
AllowOverride None
//指定先执行Allow(允许)访问规则,再执行Deny(拒绝)访问规则
Order allow,deny
//设置Allow(允许)访问规则,允许所有连接
Allow from all

启动服务
# service httpd start

Go to top.

3.安装php 4.4.1
# wget http://cn.php.net/get/php-4.4.1.tar.bz2/fr…rom/this/mirror
# tar jxvf php-4.4.1.tar.bz2
# cd php-4.4.1
# ./configure \
–prefix=/usr/local/php \
–with-mysql=/usr/local/mysql \
–with-apxs2=/usr/local/apache/bin/apxs
# make
# make install
# cp php.ini-dist /usr/local/php/lib/php.ini

# vi /usr/local/php/lib/php.ini
;default_charset = “iso-8859-1″
在这行下面加一行
default_charset = “gbk”

# vi /usr/local/apache/conf/httpd.conf
找到#AddType application/x-tar .tgz 这行,在下面加两行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到下面一行在后面加上index.php,这表示网站的默认页也能够为index.php
DirectoryIndex index.html index.html.var index.php
注意:改变了http.conf后,要重启apache服务

Go to top.

4.安装 cyrus-sasl 2.1.21
先关闭as4默认安装的sasl
# mv /usr/lib/sasl /usr/lib/sasl.OFF
# mv /usr/lib/sasl2 /usr/lib/sasl2.OFF

编译安装cyrus-sasl2.1.21
# wget http://ftp.andrew.cmu.edu/pub/cyrus-mail/c…l-2.1.21.tar.gz
# tar zxvf cyrus-sasl-2.1.21.tar.gz
# cd cyrus-sasl-2.1.21
# ./configure \
–disable-anon -enable-plain –enable-login \
–enable-sql –with-mysql=/usr/local/mysql \
–with-mysql-includes=/usr/local/mysql/include/mysql \
–with-mysql-libs=/usr/local/mysql/lib/mysql \
–with-authdaemond
# make
# make install

更新lib库
# echo “/usr/local/lib” >> /etc/ld.so.conf
# ldconfig

重要
# ln -s /usr/local/lib/sasl2 /usr/lib/sasl2

Go to top.

5.安装postfix 2.2.5
如果你的系统上原来有sendmail,先将其停止并将其文件改名
# /etc/init.d/sendmail stop
# chkconfig –level 0123456 sendmail off
# mv /usr/bin/newaliases /usr/bin/newaliases.orig
# mv /usr/bin/mailq /usr/bin/mailq.orig
# mv /usr/sbin/sendmail /usr/sbin/sendmail.orig

开始安装
# groupadd -g 12345 postfix
# useradd -u 12345 -g 12345 -c postfix -d/dev/null -s/sbin/nologin postfix
# groupadd -g 54321 postdrop
# wget ftp://postfix.cn99.com/postfix/official/p…ix-2.2.5.tar.gz
# tar zxvf postfix-2.2.5.tar.gz
# cd postfix-2.2.5
(Building Postfix with SASL authentication and mysql support)
# make -f Makefile.init makefiles \
‘CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include/mysql -DUSE_SASL_AUTH -I/usr/local/include/sasl’ \
‘AUXLIBS=-L/usr/local/mysql/lib/mysql -lmysqlclient -lz -lm -L/usr/local/lib -lsasl2′
# make install

注意:
本例中Mysql安装在/usr/lcoal/mysql,sasl2安装在/usr/lib/sasl2。如果安装路径不同,请自行修改编译时CCARGS和AUXLIBS选项。
在执行make install的时候可能会得到如下的提示:
/usr/libexec/ld-elf.so.1: Shared object “libmysqlclient.so.12″ not found
这是因为mysql不是安装在默认目录中的,所以需要告诉postfix应该到哪里去找libmysqlclient.so.12,使用ldconfig就可以达到这个目的
# echo /usr/local/mysql/lib/mysql >> /etc/ld.so.conf
# ldconfig

Go to top.

6.安装postfixadmin 2.1.0
建立apache和maildrop的用户和组
# groupadd vmail -g 1001
# useradd vmail -u 1001 -g 1001 -s/sbin/nologin -d/dev/null

# vi /usr/local/apache/conf/httpd.conf

User nobody
Group #-1
改为
User vmail
Group vmail

# wget http://high5.net/postfixadmin/download.php…admin-2.1.0.tgz
# tar -zxvf postfixadmin-2.1.0.tgz
# mv postfixadmin-2.1.0 /var/www/postfixadmin
更改权限,假定运行apache的用户和组为vmail
# chown -R vmail:vmail /var/www/postfixadmin
# cd /var/www/postfixadmin
# chmod 640 *.php *.css
# cd /var/www/postfixadmin/admin/
# chmod 640 *.php .ht*
# cd /var/www/postfixadmin/images/
# chmod *.png
# cd /var/www/postfixadmin/languages/
# chmod 640 *.lang
# cd /var/www/postfixadmin/templates/
# chmod 640 *.tpl
# cd /var/www/postfixadmin/users/
# chmod 640 *.php

建立mysql表
# cd /var/www/postfixadmin
# mysql -u root < DATABASE_MYSQL.TXT
# cp config.inc.php.sample config.inc.php
# vi config.inc.php
本例中的配置如下:
$CONF['default_language'] = 'cn';
$CONF['database_type'] = 'mysql';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfixadmin';
$CONF['database_password'] = 'postfixadmin';
$CONF['database_name'] = 'postfix';
$CONF['encrypt'] = 'md5crypt';
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['quota'] = 'YES';
$CONF['quota_multiplier'] = '1024000';

邮箱的存储格式使用domain.ltd/username的形式,所以设置:
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';

然后打开浏览器,进入postfixadmin的欢迎界面,点击网页上的setup,看看检查是否通过,记得要删除setup.php文件。然后进入http://www.yourdomain.com/postfixadmin/admin/index.php,就可以新建域名、管理员以及邮箱了。

Go to top.

# vi /etc/postfix/main.cf
#=====================BASE=========================
myhostname = www.test.com
mydomain = test.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 127.0.0.0/8
inet_interfaces = all

#=====================Vritual Mailbox settings=========================
virtual_mailbox_base = /var/mailbox
virtual_mailbox_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql/mysql_virtual_domains_maps.cf
virtual_alias_domains =
virtual_alias_maps = mysql:/etc/postfix/mysql/mysql_virtual_alias_maps.cf
virtual_uid_maps = static:1001
virtual_gid_maps = static:1001
virtual_transport = maildrop
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1

#====================QUOTA========================
message_size_limit = 14336000
virtual_mailbox_limit = 20971520
virtual_create_maildirsize = yes
virtual_mailbox_extended = yes
virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql/mysql_virtual_mailbox_limit_maps.cf
virtual_mailbox_limit_override = yes
virtual_maildir_limit_message = Sorry, the user's maildir has overdrawn his diskspace quota, please try again later.
virtual_overquota_bounce = yes

#====================SASL========================
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_invalid_hostname,
reject_non_fqdn_hostname,
reject_unknown_sender_domain,
reject_non_fqdn_sender,
reject_non_fqdn_recipient,
reject_unknown_recipient_domain,
reject_unauth_pipelining,
reject_unauth_destination,
permit
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP "Version not Available"

Go to top.

建立/var/mailbox并设置权限
# mkdir /var/mailbox
# chown -R vmail:vmail /var/mailbox
# chmod -R ug+rwx,o-rwx /var/mailbox

建立/etc/postfix/mysql文件夹和MySQL查询配置文件
# mkdir /etc/postfix/mysql
1、vi /etc/postfix/mysql/mysql_virtual_alias_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address

2、vi /etc/postfix/mysql/mysql_virtual_domains_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = domain
select_field = description
where_field = domain
#additional_conditions = and backupmx = '0' and active = '1'

3、vi /etc/postfix/mysql/mysql_virtual_mailbox_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
#additional_conditions = and active = '1'

4、vi /etc/postfix/mysql/mysql_virtual_mailbox_limit_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username
#additional_conditions = and active = '1'

Go to top.

配置 /usr/local/lib/sasl2/smtpd.conf
sasl密码验证机制为authdaemond
# vi /usr/local/lib/sasl2/smtpd.conf
pwcheck_method:authdaemond
log_level:3
srp_mda:md5
password_format:crypt
mech_list:PLAIN LOGIN
authdaemond_path: /usr/local/var/spool/authdaemon/socket

Go to top.

7.安装Courier-authlib 0.57
新版本的imap不再包含authentication library,必须先安装 Courier authentication library
# wget http://www.courier-mta.org/beta/courier-au...0051004.tar.bz2
# tar jxvf courier-authlib-0.57.20051004.tar.bz2
# cd courier-authlib-0.57.20051004
# ./configure \
--with-redhat \
--with-authmysql=yes \
--with-mailuser=vmail --with-mailgroup=vmail \
--with-mysql-libs=/usr/local/mysql/lib/mysql --with-mysql-includes=/usr/local/mysql/include/mysql/
# make
# make install
# make install-configure

重要
# chmod +x /usr/local/var/spool/authdaemon/

# vi /usr/local/etc/authlib/authdaemonrc
authmodulelist="authmysql"

# vi /usr/local/etc/authlib/authmysqlrc
MYSQL_SERVER localhost
MYSQL_SOCKET /tmp/mysql.sock
MYSQL_DATABASE postfix
MYSQL_USERNAME postfix
MYSQL_PASSWORD postfix
MYSQL_USER_TABLE mailbox
MYSQL_LOGIN_FIELD username
MYSQL_CRYPT_PWFIELD password
MYSQL_UID_FIELD '1001'
MYSQL_GID_FIELD '1001'
MYSQL_HOME_FIELD '/var/mailbox/'
MYSQL_MAILDIR_FIELD maildir
MYSQL_NAME_FIELD name
MYSQL_QUOTA_FIELD concat(quota,'S')
MYSQL_WHERE_CLAUSE active='1'
DEFAULT_DOMAIN test.com
注意:确认在这个文件中不能用空格键(包括行尾),只能用tab键。
确认只使用单引号,比如:'/var/mailbox/','UID','GID'(本文为'1001')
localhost不能用单引号
确认你的/etc/hosts文件中有localhost
编译时如果支持Ipv6可能导致错误
MYSQL_GID_FIELD 和MYSQL_UID_FIELD是maildrop的UID和GID,而不是MySQL的

启动服务
自启动:
# cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
# chmod 755 /etc/rc.d/init.d/courier-authlib
# chkconfig --level 0123456 courier-authlib on
手动启动服务:
# authdaemond start

Go to top.

8.安装Courier-imap 4.0.6
# wget http://www.courier-mta.org/beta/imap/couri...0051004.tar.bz2
# tar jxvf courier-imap-4.0.6.20051004.tar.bz2
# cd courier-imap-4.0.6.20051004
# ./configure \
--prefix=/usr/local/imap \
--with-redhat \
--disable-root-check \
--enable-unicode=utf-8,iso-8859-1,gb2312,gbk,gb18030 \
--with-trashquota \
--with-dirsync
# make
# make install-strip (先install-strip,如果失败,再make install)
# make install-configure

# vi /usr/local/imap/etc/pop3d
POP3DSTART=YES

# vi /usr/local/imap/etc/imapd
IMAPDSTART=YES

让imap自启动:
# cp courier-imap.sysvinit /etc/rc.d/init.d/courier-imap
# chmod 755 /etc/rc.d/init.d/courier-imap
# chkconfig --level 0123456 courier-imap on

Go to top.

9.安装Courier-maildrop 2.0.1
先装pcre
# wget http://optusnet.dl.sourceforge.net/sourcef...cre-6.3.tar.bz2
# tar jxvf pcre-6.3.tar.bz2
# cd pcre-6.3
# ./configure
# make
# make install

# wget http://optusnet.dl.sourceforge.net/sourcef...p-2.0.1.tar.bz2
# tar jxvf maildrop-2.0.1.tar.bz2
# cd maildrop-2.0.1
# ./configure \
--prefix=/usr/local/maildrop \
--enable-sendmail=/usr/sbin/sendmail \
--enable-trusted-users='root vmail' \
--enable-syslog=1 \
--enable-maildirquota \
--enable-maildrop-uid=1001 \
--enable-maildrop-gid=1001 \
--with-trashquota \
--with-dirsync
# make
# make install
# cp /usr/local/maildrop/bin/maildrop /usr/bin
# chmod a+rx /usr/bin/maildrop

运行maildrop -v应该有如下提示信息:
maildrop 2.0.0 Copyright 1998-2005 Double Precision, Inc.
GDBM extensions enabled.
Courier Authentication Library extension enabled.
Maildir quota extension enabled.
This program is distributed under the terms of the GNU General Public
License. See COPYING for additional information.

新建/etc/maildroprc文件
# vi /etc/maildroprc
logfile "/var/mailbox/maildrop.log"
to "$HOME/$DEFAULT"

# chmod a+r /etc/maildroprc

配置Postfix
# vi /etc/postfix/master.cf
maildrop unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/local/maildrop/bin/maildrop -w 90 -d ${recipient}

由于maildrop没有建立及删除maildir的功能,因此由脚本实现(是否有什么安全隐患?)
# vi /etc/sudoers
vmail ALL = NOPASSWD: /usr/sbin/maildirmake.sh , /usr/sbin/maildirdel.sh

新建/usr/sbin/maildirmake.sh文件
# vi /usr/sbin/maildirmake.sh
#!/bin/bash
set -e
if [ ! -d /var/mailbox/$1 ]
then
mkdir /var/mailbox/$1
fi
chown -R vmail:vmail /var/mailbox/$1
cd "/var/mailbox/$1"
/usr/local/imap/bin/maildirmake $2
chown -R vmail:vmail /var/mailbox/$1/$2

新建/usr/sbin/maildirdel.sh文件
# vi /usr/sbin/maildirmake.sh
#!/bin/bash
rm -rf /var/mailbox/$1/$2

# chmod 755 /usr/sbin/maildirmake.sh
# chmod 755 /usr/sbin/maildirdel.sh

更改postfixadmin目录及postfixadmin/admin目录下的create-mailbox.php文件
在这两个文件的$tQuota = $CONF['maxquota'];行后加一行(postfixadmin/admin目录下的大概在200行,postfixadmin目录下的大概在?行):
# vi /var/www/postfixadmin/admin/create-mailbox.php (vi /var/www/postfixadmin/create-mailbox.php)
system("sudo /usr/sbin/maildirmake.sh $fDomain ".$_POST['fUsername']);

更改postfixadmin目录及postfixadmin/admin目录下的delete.php文件
在这两个文件的$result = db_query ("SELECT * FROM mailbox WHERE username='$fDelete' AND domain='$fDomain'");行后加几行:
# vi /var/www/postfixadmin/admin/delete.php (vi /var/www/postfixadmin/delete.php)
$userarray=explode("@",$fDelete);
$user=$userarray[0];
$domain=$userarray[1];
system("sudo /usr/sbin/maildirdel.sh $domain $user");

10.测试
启动所有服务
# service httpd start
# service mysqld start
# postfix start
# service courier-authlib start
# service courier-imap start

# netstat -ant | grep "LISTEN"
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN
tcp 0 0 :::110 :::* LISTEN
tcp 0 0 :::143 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN

在postfixadmin中建立一测试帐户test@test.com

测试smtp
# perl -MMIME::Base64 -e 'print encode_base64("test\@test.com");'
dGVzdEB0ZXN0LmNvbQ==
# perl -MMIME::Base64 -e 'print encode_base64("test");'
dGVzdA==

# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
220 www.test.com ESMTP "Version not Available"
ehlo www.test.com
250-www.test.com
250-PIPELINING
250-SIZE 14336000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250 8BITMIME
auth login
334 VXNlcm5hbWU6
dGVzdEB0ZXN0LmNvbQ==
334 UGFzc3dvcmQ6
dGVzdA==
235 Authentication successful

测试POP3和IMAP
# telnet localhost 110
+OK Hello there
user test@test.com
+OK Password required.
pass test
+OK Logged in.
quit
+OK bye-bye

测试maildrop
# maildrop -V 9 -d test@test.com
maildrop: authlib: groupid=1001
maildrop: authlib: userid=1001
maildrop: authlib: logname=test@test.com, home=/var/mailbox/, mail=test.com/test/
maildrop: Changing to /var/mailbox/

ctrl+c退出

Go to top.

11.webmail

11.1.安装squirrelmail 1.4.5
下载squirrelmail及中文包
# wget http://keihanna.dl.sourceforge.net/sourcef...l-1.4.5.tar.bz2
# wget http://optusnet.dl.sourceforge.net/sourcef...0050904.tar.bz2
把squirreelmail解压到/var/www目录下。
# tar jxvf squirrelmail-1.4.5.tar.bz2 -C /var/www/
# mv /var/www/squirrelmail-1.4.5/ /var/www/squirrelmail
解压中文包
# tar jxvf zh_CN-1.4.5-20050904.tar.bz2 -C /var/www/squirrelmail/

在配置squirrelmail之前先下载三个插件:
Quota Usage Version 1.3
# wget http://www.squirrelmail.org/countdl.php?fi....3-1.2.7.tar.gz
Compatibility Version 2.0.2
# wget http://www.squirrelmail.org/countdl.php?fi...ty-2.0.2.tar.gz
Change MySQL Password Version 3.2
# wget http://www.squirrelmail.org/countdl.php?fi....2-1.2.8.tar.gz

把这三个插件解压到squirrelmail的plugin目录下
# tar zxvf quota_usage-1.3-1.2.7.tar.gz -C /var/www/squirrelmail/plugins/
# tar zxvf compatibility-2.0.2.tar.gz -C /var/www/squirrelmail/plugins/
# tar zxvf change_mysqlpass-3.2-1.2.8.tar.gz -C /var/www/squirrelmail/plugins/
第一个插件是用来显示邮箱的使用情况的;第二个和第三个插件是用来修改密码的。
如果不想装这些插件,请跳过。

配置Quota Usage
# cd /var/www/squirrelmail/plugins/quota_usage
# cp config.php.sample config.php

配置Change MySQL Password
# cd /var/www/squirrelmail/plugins/change_mysqlpass
# cp config.php.sample config.php
# vi config.php
更改如下几个变量:
$mysql_database = 'postfix';
$mysql_table = 'mailbox';
$mysql_userid_field = 'username';
$mysql_password_field ='password';
$mysql_manager_id = 'postfix';
$mysql_manager_pw = 'postfix';
$mysql_unixcrypt = 0;
$mysql_MD5crypt = 1;
$use_ssl_for_password_change = 0;

配置squirrelmail
# cd /var/www/squirrelmail
# ./configure
进入10. Languages
把1. Default Language : 的en_US改成zh_CN。
进入8. Plugins,添加这三个插件

# chown -R vmail:vmail /var/www/squirrelmail/data/
# chmod -R 730 /var/www/squirrelmail/data/

打开浏览器输入http://192.168.0.5/squirrelmail/,用test@test.com登陆,你将会在屏幕的左上角看到邮箱的使用情况,你还会看到一条警告信息:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /var/www/squirrelmail/plugins/change_mysqlpass/functions.php on line 129
这是由于数据库结构不一致造成的,你只要把functions.php中的129行注释调即可(在行首加//)

Go to top.

12.安装clamav 0.87
# wget http://optusnet.dl.sourceforge.net/sourcef...mav-0.87.tar.gz
# tar zxvf clamav-0.87.tar.gz
# cd clamav-0.87
# groupadd clamav
# useradd -g clamav -s/sbin/nologin -d/dev/null clamav
# ./configure --prefix=/usr/local/clamav --with-dbdir=/usr/local/share/clamav --disable-zlib-vcheck
# make
# make check
# make install

编辑/usr/local/clamav/etc/clamd.conf
# vi /usr/local/clamav/etc/clamd.conf
#Verbose logging with syslog
LogSyslog
LogVerbose
LogFacility LOG_MAIL
LogFile /var/log/clamav/clamd.log
#Change pid file location
PidFile /var/run/clamav/clamd.pid
DatabaseDirectory /usr/local/share/clamav
#Set the clamav socket
LocalSocket /var/run/clamav/clamd
#Close the connection when this limit is exceeded
StreamMaxLength 10M
#Don't run clamd as root
User amavis
#Newer versions require you to uncomment this
ScanMail
ScanArchive

编辑/usr/local/clamav/etc/freshclam.conf
# vi /usr/local/clamav/etc/freshclam.conf
DatabaseDirectory /usr/local/share/clamav
UpdateLogFile /var/log/clamav/freshclam.log
LogSyslog
LogVerbose
DatabaseOwner amavis
#Check for updates every two hours. That is the official recommendation
Checks 12
DatabaseMirror db.CN.clamav.net
DatabaseMirror database.clamav.net
NotifyClamd

注意:一定要注释掉上面两个文件中Example那行

添加amavis用户和组
# groupadd amavis
# useradd -g amavis -s /sbin/nologin -d /dev/null amavis

创建日志文件夹并设置权限
# mkdir /var/log/clamav
# chmod -R 744 /var/log/clamav
# chown -R amavis:amavis /var/log/clamav

# chown -R amavis.amavis /usr/local/share/clamav
# mkdir /var/run/clamav
# chmod 700 /var/run/clamav
# chown amavis.amavis /var/run/clamav

手动更新病毒库
# /usr/local/clamav/bin/freshclam

启动
# /usr/local/clamav/sbin/clamd

Go to top.

13.安装amavisd-new 2.3.3

# wget http://www.ijs.si/software/amavisd/amavisd...ew-2.3.3.tar.gz
# tar zxvf amavisd-new-2.3.3.tar.gz
# cd amavisd-new-2.3.3

# mkdir -p /var/amavis /var/amavis/tmp /var/amavis/var /var/amavis/db
# chown -R amavis:amavis /var/amavis
# chmod -R 750 /var/amavis

# cp amavisd /usr/local/sbin/
# chown root /usr/local/sbin/amavisd
# chmod 755 /usr/local/sbin/amavisd

# cp amavisd.conf /etc/
# chown root /etc/amavisd.conf
# chmod 644 /etc/amavisd.conf

# cp amavisd_init.sh /etc/rc.d/init.d/amavisd
# chmod 744 /etc/rc.d/init.d/amavisd
# chkconfig --add amavisd
# chkconfig amavisd on
# vi /etc/rc.d/init.d/amavisd
prog="/usr/local/sbin/amavisd"

病毒邮件存放目录
# mkdir /var/virusmails
# chown amavis:amavis /var/virusmails
# chmod 750 /var/virusmails

Go to top.

编辑/etc/amavisd.conf,修改下面这几行
# vi /etc/amavisd.conf
$max_servers=8;
$daemon_user = 'amavis';
$daemon_group = 'amavis';
$mydomain = 'test.com';
$db_home = "$MYHOME/db";
$inet_socket_port = 10024;
$sa_tag_level_deflt = -100;
$sa_tag2_level_deflt = 6.3;
$sa_kill_level_deflt = $sa_tag2_level_deflt;
$virus_admin = "virusalert\@$mydomain";
$sa_spam_subject_tag = '***SPAM*** ';
$notify_method = $forward_method;
$forward_method = 'smtp:127.0.0.1:10025';
$final_virus_destiny = D_DISCARD;
$final_banned_destiny = D_DISCARD;
$final_spam_destiny = D_DISCARD;
['ClamAV-clamd',
\&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd"],
qr/\bOK$/, qr/\bFOUND$/,
qr/^.*?: (?!Infected Archive)(.*) FOUND$/ ],

测试amavis
# /usr/local/sbin/amavisd debug
ERROR: MISSING REQUIRED BASIC MODULES:
Time::HiRes
IO::Wrap
Unix::Syslog
Mail::Field
MIME::Words
Net::Server
BEGIN failed--compilation aborted at /usr/local/sbin/amavisd line 141.
根据出错提示,缺什么装什么,我的LANG变量为en_US.UTF-8
# perl -MCPAN -e shell
cpan> install Time::HiRes
cpan> install IO::Wrap
cpan> install Unix::Syslog
cpan> install Mail::Field
cpan> install Compress::Zlib
cpan> install MIME::Words
cpan> install Net::Server
cpan> install BerkeleyDB
cpan> install Convert::TNEF
cpan> install Convert::UUlib
cpan> install Archive::Tar
cpan> install Archive::Zip
cpan> install HTML::Parser
cpan> install DB_File
cpan> install Net::DNS (提示是否test, 选择no)
cpan> install Digest::SHA1
cpan> install Mail::SpamAssassin (# export LANG=en_US)

期间MIME安装会失败,只好手动安装,跳过测试
# cd /root/.cpan/build/MIME-tools-5.418/
# perl Makefile.PL
# make install

启动
# /usr/local/sbin/amavisd start 或
# service amavisd start

Go to top.

设置postfix
修改/etc/postfix/main.cf
# vi /etc/postfix/main.cf (加入一行)
content_filter = amavis:127.0.0.1:10024
修改/etc/postfix/master.cf
# vi /etc/postfix/master.cf (在最后加上)
# amavisd-new
amavis unix - - n - 2 smtp
-o smtp_data_done_timeout=1200
-o smtp_send_xforward_command=yes
-o disable_dns_lookups=yes

localhost:10025 inet n - n - - smtpd
-o content_filter=
-o local_recipient_maps=
-o relay_recipient_maps=
-o mynetworks=127.0.0.0/8
-o smtpd_helo_restrictions=
-o smtpd_client_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
-o strict_rfc821_envelopes=yes
-o smtpd_error_sleep_time=0
-o smtpd_soft_error_limit=1001
-o smtpd_hard_error_limit=1000

重新服务
# service postfix restart
# service clamd restart
# service amavisd restart

# netstat -ant | grep LISTEN (应该可以看到这两个端口在监听)
127.0.0.1.10024 *.* 0 0 49152 0 LISTEN
127.0.0.1.10025 *.* 0 0 49152 0 LISTEN

Go to top.

14.安装Spamassassin
前面装amavis的时候已经装好了,如果没装,请按如下方式安装

启动spamd
# /usr/bin/spamd –daemonize –pidfile /var/run/spamd.pid

下载中文垃圾垃圾邮件过滤规则Chinese_rules.cf
# wget -N -P /usr/share/spamassassin www.ccert.edu.cn/spam/sa/Chinese_rules.cf

每次更新Chinese_rules.cf需要重启spamd方法如下
# kill -HUP `cat /var/run/spamd.pid`

自动更新中文垃圾垃圾邮件过滤规则
# vi /etc/crontab (加一行)
0 0 1 * * root wget -N -P /usr/share/spamassassin www.ccert.edu.cn/spam/sa/Chinese_rules.cf;kill -HUP `cat /var/run/spamd.pid`

测试病毒扫描
给该test@test.com用户发送邮件,包含以下内容:
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
查看日志,如果出现类似如下提示,则表明成功
Nov 6 22:06:20 localhost postfix/smtp[18276]: 9CEB657E22: to=, relay=127.0.0.1[127.0.0.1], delay=2, status=sent (250 2.7.1 Ok, discarded, id=18262-01 - VIRUS: Eicar-Test-Signature)
邮件病毒扫描日志将被记录在/var/log/clamav/clamav.log中!

测试垃圾邮件扫描
给test@test.com用户发送邮件,包含以下内容:
XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
查看日志,如果出现类似如下提示,表明成功
Nov 6 22:10:51 localhost amavis[18263]: (18263-01) Blocked SPAM, LOCAL [127.0.0.1] [127.0.0.1] -> , quarantine: spam-JH2LSCT1MLYg.gz, Message-ID: <1451.192.168.1.10.1131286248.squirrel@192.168.1.21>, mail_id: JH2LSCT1MLYg, Hits: 1005.902, 3591 ms

Go to top.

15.启动脚本
postfix的启动脚本:
# cat /etc/rc.d/init.d/postfix
=================================================================
#!/bin/bash
#
# mailsys This shell script takes care of starting and stopping Postfix
# author : xingyu.wang 2004/1/28
#
# chkconfig: 2345 80 30
# description: Postfix is a Mail Transport Agent, which is the program
# that moves mail from one machine to another.
#
# processname: mailsys
# pidfile: /var/run/postfix.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -f /usr/sbin/postfix ] || exit 0
RETVAL=0
prog=”Postfix”

start() {
# Start daemons.
echo -n $”Starting $prog: ”
/usr/sbin/postfix start > /dev/null 2>&1 &

RETVAL=$?

if [ $RETVAL -eq 0 ]; then
touch /var/lock/subsys/postfix
success $”$prog start”
else
failure $”$prog start failure”
fi

echo
return $RETVAL
}

stop() {
# Stop daemons.
echo -n $”Shutting down $prog: ”
/usr/sbin/postfix stop > /dev/null 2>&1 &
RETVAL=$?

if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/postfix
success $”$prog stop”
else
failure $”$prog stop failure”
fi

echo
return $RETVAL
}

# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart}”
exit 1
esac
exit $RETVA

# chmod 755 /etc/rc.d/init.d/postfix
# chkconfig –level 2345 postfix on

clamav的启动脚本
# vi /etc/rc.d/init.d/clamd
=================================================================
#! /bin/bash
#
# crond Start/Stop the clam antivirus daemon.
#
# chkconfig: 2345 90 60
# description: clamdis a standard UNIX program that scans for Viruses.
# processname: clamd
# config: /usr/local/clamav/etc/clamd.conf
# pidfile: /var/run/clamav/clamd.pid

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

# See how we were called.

prog=”clamd”
progdir=”/usr/local/clamav/sbin”

# Source configuration
if [ -f /etc/sysconfig/$prog ] ; then
. /etc/sysconfig/$prog
fi

start() {
echo -n $”Starting $prog: ”
daemon $progdir/$prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/run/clamav/clamd.pid
return $RETVAL
}

stop() {
echo -n $”Stopping $prog: ”
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/run/clamav/clamd.pid
return $RETVAL
}

rhstatus() {
status clamd
}

restart() {
stop
start
}

reload() {
echo -n $”Reloading clam daemon configuration: ”
killproc clamd -HUP
retval=$?
echo
return $RETVAL
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
rhstatus
;;
condrestart)
[ -f /var/lock/subsys/clamd ] && restart || :
;;
*)
echo $”Usage: $0 {start|stop|status|reload|restart|condrestart}”
exit 1
esac

exit 0

# chmod 755 /etc/rc.d/init.d/clamd
# chkconfig –add clamd
# chkconfig clamd on

Go to top.

Popularity: 24% [?]

Tags: , , , , , , , , ,

Apache 安装 mod_rewrite 模块(ubuntu)

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

编辑

/etc/apache2/httpd.conf

加入

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

如果你的服务器apache还没有安装,那很简单,在编译apache时将mod_rewrite模块编译进去就可以,相关文档可以在www.gbunix.com中找到。如果你的apache已经安装好了,现在只想编译出mod_rewrite.so模块,在apache中进行加载,下面我们就介绍这个方法。

以Solaris操作系统进行举例:

# PATH=/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin:$PATH
# export PATH
# which gcc
# which make

# find ./ -name mod_rewrite.c //在apache的安装目录中寻找mod_rewrite.c文件
# cd PATH/to/mod_rewrite.c //进入包含mod_rewrite.c文件的目录
# apxs -c mod_rewrite.c //apxs请指定绝对路径,在你当前正在使用apache的bin目录里
# apxs -i -a -n mod_rewrite mod_rewrite.la

如果没有什么错误的话,应该在你的apache的modules目录中编译出一个mod_rewrite.so文件。

编辑httpd.conf文件,确认httpd.conf中已经包含mod_rewrite.so的加载语句,如下:

LoadModule rewrite_module modules/mod_rewrite.so

这时,你的apache应该已经支持rewrite了。

Popularity: 11% [?]

Tags: ,

about grep

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

Global Regular ExPression 文本搜索工具。

相关资料:
介绍一款强大的文档搜索工具——grep

windows 下的 grep 工具有 wingrep 和 powergrep


The name comes from a command in the Unix text editor ed that takes the form:

g/re/p

This means “search globally for matches to the regular expression re, and print lines where they are found”.

http://www.wingrep.com/

PowerGREP is a powerful Windows grep tool. Quickly search through large numbers of files on your PC or network, including text and binary files, compressed archives, MS Word documents, Excel spreadsheets and PDF files, etc. Find the information you want with powerful text patterns (regular expressions) specifying the form of what you want, instead of literal text. Search and replace with one or many regular expressions to comprehensively maintain web sites, source code, reports, etc. Extract statistics and knowledge from logs files and large data sets.

http://www.powergrep.com

Popularity: 14% [?]

Tags: , ,

10 个 unix 好习惯

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

这篇 Learn 10 good UNIX usage habits挺有趣,全摘如下:

Adopt 10 good habits

Ten good habits to adopt are:

1. Make directory trees in a single swipe.
2. Change the path; do not move the archive.
3. Combine your commands with control operators.
4. Quote variables with caution.
5. Use escape sequences to manage long input.
6. Group your commands together in a list.
7. Use xargs outside of find.
8. Know when grep should do the counting — and when it should step aside.
9. Match certain fields in output, not just lines.
10. Stop piping cats.

Make directory trees in a single swipe

Listing 1 illustrates one of the most common bad UNIX habits around: defining directory trees one at a time.
Listing 1. Example of bad habit #1: Defining directory trees individually

~ $ mkdir tmp
~ $ cd tmp
~/tmp $ mkdir a
~/tmp $ cd a
~/tmp/a $ mkdir b
~/tmp/a $ cd b
~/tmp/a/b/ $ mkdir c
~/tmp/a/b/ $ cd c
~/tmp/a/b/c $

It is so much quicker to use the -p option to mkdir and make all parent directories along with their children in a single command. But even administrators who know about this option are still caught stepping through the subdirectories as they make them on the command line. It is worth your time to conscientiously pick up the good habit:

Listing 2. Example of good habit #1: Defining directory trees with one command
一次建立目录树

~ $ mkdir -p tmp/a/b/c

You can use this option to make entire complex directory trees, which are great to use inside scripts; not just simple hierarchies. For example:
Listing 3. Another example of good habit #1: Defining complex directory trees with one command

~ $ mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}

In the past, the only excuse to define directories individually was that your mkdir implementation did not support this option, but this is no longer true on most systems. IBM, AIX??, mkdir, GNU mkdir, and others that conform to the Single UNIX Specification now have this option.
For the few systems that still lack the capability, use the mkdirhier script (see Resources), which is a wrapper for mkdir that does the same function:

~ $ mkdirhier project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat/a}

Change the path; do not move the archive

Another bad usage pattern is moving a .tar archive file to a certain directory because it happens to be the directory you want to extract it in. You never need to do this. You can unpack any .tar archive file into any directory you like — that is what the -C option is for. Use the -C option when unpacking an archive file to specify the directory to unpack it in:
Listing 4. Example of good habit #2: Using option -C to unpack a .tar archive file
使用 -C 参数直接解压文件到需要的目录

~ $ tar xvf -C tmp/a/b/c newarc.tar.gz

Making a habit of using -C is preferable to moving the archive file to where you want to unpack it, changing to that directory, and only then extracting its contents — especially if the archive file belongs somewhere else.

Combine your commands with control operators

You probably already know that in most shells, you can combine commands on a single command line by placing a semicolon (;) between them. The semicolon is a shell control operator, and while it is useful for stringing together multiple discrete commands on a single command line, it does not work for everything. For example, suppose you use a semicolon to combine two commands in which the proper execution of the second command depends entirely upon the successful completion of the first. If the first command does not exit as you expected, the second command still runs — and fails. Instead, use more appropriate control operators (some are described in this article). As long as your shell supports them, they are worth getting into the habit of using them.
Run a command only if another command returns a zero exit status
Use the && control operator to combine two commands so that the second is run only if the first command returns a zero exit status. In other words, if the first command runs successfully, the second command runs. If the first command fails, the second command does not run at all. For example:

Listing 5. Example of good habit #3: Combining commands with control operators
使用 && 控制符控制:第一个命令成功后执行下一个

~ $ cd tmp/a/b/c && tar xvf ~/archive.tar

In this example, the contents of the archive are extracted into the ~/tmp/a/b/c directory unless that directory does not exist. If the directory does not exist, the tar command does not run, so nothing is extracted.
Run a command only if another command returns a non-zero exit status
Similarly, the || control operator separates two commands and runs the second command only if the first command returns a non-zero exit status. In other words, if the first command is successful, the second command does not run. If the first command fails, the second command does run. This operator is often used when testing for whether a given directory exists and, if not, it creates one:

Listing 6. Another example of good habit #3: Combining commands with control operators
使用 || 控制符:第一个失败执行下一个

~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c

You can also combine the control operators described in this section. Each works on the last command run:

Listing 7. A combined example of good habit #3: Combining commands with control operators
也可以组合使用

~ $ cd tmp/a/b/c || mkdir -p tmp/a/b/c && tar xvf -C tmp/a/b/c ~/archive.tar

Quote variables with caution

Always be careful with shell expansion and variable names. It is generally a good idea to enclose variable calls in double quotation marks, unless you have a good reason not to. Similarly, if you are directly following a variable name with alphanumeric text, be sure also to enclose the variable name in curly braces ({}) to distinguish it from the surrounding text. Otherwise, the shell interprets the trailing text as part of your variable name — and most likely returns a null value. Listing 8 provides examples of various quotation and non-quotation of variables and their effects.
Listing 8. Example of good habit #4: Quoting (and not quoting) a variable
~ $ ls tmp/
a b
~ $ VAR=”tmp/*”
~ $ echo $VAR
tmp/a tmp/b
~ $ echo “$VAR”
tmp/*
~ $ echo $VARa
~ $ echo “$VARa”
~ $ echo “${VAR}a”
tmp/*a
~ $ echo ${VAR}a
tmp/a
~ $

Use escape sequences to manage long input

You have probably seen code examples in which a backslash (\) continues a long line over to the next line, and you know that most shells treat what you type over successive lines joined by a backslash as one long line. However, you might not take advantage of this function on the command line as often as you can. The backslash is especially handy if your terminal does not handle multi-line wrapping properly or when your command line is smaller than usual (such as when you have a long path on the prompt). The backslash is also useful for making sense of long input lines as you type them, as in the following example:
Listing 9. Example of good habit #5: Using a backslash for long input
用 \ 断行很长的命令行

~ $ cd tmp/a/b/c || \
> mkdir -p tmp/a/b/c && \
> tar xvf -C tmp/a/b/c ~/archive.tar

Alternatively, the following configuration also works:

Listing 10. Alternative example of good habit #5: Using a backslash for long input

~ $ cd tmp/a/b/c \
> || \
> mkdir -p tmp/a/b/c \
> && \
> tar xvf -C tmp/a/b/c ~/archive.tar

However you divide an input line over multiple lines, the shell always treats it as one continuous line, because it always strips out all the backslashes and extra spaces.
Note: In most shells, when you press the up arrow key, the entire multi-line entry is redrawn on a single, long input line.

Group your commands together in a list

Most shells have ways to group a set of commands together in a list so that you can pass their sum-total output down a pipeline or otherwise redirect any or all of its streams to the same place. You can generally do this by running a list of commands in a subshell or by running a list of commands in the current shell.
Run a list of commands in a subshell
Use parentheses to enclose a list of commands in a single group. Doing so runs the commands in a new subshell and allows you to redirect or otherwise collect the output of the whole, as in the following example:

Listing 11. Example of good habit #6: Running a list of commands in a subshell

~ $ ( cd tmp/a/b/c/ || mkdir -p tmp/a/b/c && \
> VAR=$PWD; cd ~; tar xvf -C $VAR archive.tar ) \
> | mailx admin -S “Archive contents”

In this example, the content of the archive is extracted in the tmp/a/b/c/ directory while the output of the grouped commands, including a list of extracted files, is mailed to the admin address.
The use of a subshell is preferable in cases when you are redefining environment variables in your list of commands and you do not want those definitions to apply to your current shell.
Run a list of commands in the current shell
Use curly braces ({}) to enclose a list of commands to run in the current shell. Make sure you include spaces between the braces and the actual commands, or the shell might not interpret the braces correctly. Also, make sure that the final command in your list ends with a semicolon, as in the following example:
Listing 12. Another example of good habit #6: Running a list of commands in the current shell
~ $ { cp ${VAR}a . && chown -R guest.guest a && \
> tar cvf newarchive.tar a; } | mailx admin -S “New archive”

Use xargs outside of find
使用 xargs 而不是 find

Use the xargs tool as a filter for making good use of output culled from the find command. The general precept is that a find run provides a list of files that match some criteria. This list is passed on to xargs, which then runs some other useful command with that list of files as arguments, as in the following example:
Listing 13. Example of the classic use of the xargs tool

~ $ find some-file-criteria some-file-path | \
> xargs some-great-command-that-needs-filename-arguments

However, do not think of xargs as just a helper for find; it is one of those underutilized tools that, when you get into the habit of using it, you want to try on everything, including the following uses.
Passing a space-delimited list
In its simplest invocation, xargs is like a filter that takes as input a list (with each member on a single line). The tool puts those members on a single space-delimited line:

Listing 14. Example of output from the xargs tool

~ $ xargs
a
b
c
Control-D
a b c
~ $

You can send the output of any tool that outputs file names through xargs to get a list of arguments for some other tool that takes file names as an argument, as in the following example:

Listing 15. Example of using of the xargs tool

~/tmp $ ls -1 | xargs
December_Report.pdf README a archive.tar mkdirhier.sh
~/tmp $ ls -1 | xargs file
December_Report.pdf: PDF document, version 1.3
README: ASCII text
a: directory
archive.tar: POSIX tar archive
mkdirhier.sh: Bourne shell script text executable
~/tmp $

The xargs command is useful for more than passing file names. Use it any time you need to filter text into a single line:
Listing 16. Example of good habit #7: Using the xargs tool to filter text into a single line
~/tmp $ ls -l | xargs
-rw-r–r– 7 joe joe 12043 Jan 27 20:36 December_Report.pdf -rw-r–r– 1 \
root root 238 Dec 03 08:19 README drwxr-xr-x 38 joe joe 354082 Nov 02 \
16:07 a -rw-r–r– 3 joe joe 5096 Dec 14 14:26 archive.tar -rwxr-xr-x 1 \
joe joe 3239 Sep 30 12:40 mkdirhier.sh
~/tmp $

Be cautious using xargs
Technically, a rare situation occurs in which you could get into trouble using xargs. By default, the end-of-file string is an underscore (_); if that character is sent as a single input argument, everything after it is ignored. As a precaution against this, use the -e flag, which, without arguments, turns off the end-of-file string completely.

Know when grep should do the counting — and when it should step aside

Avoid piping a grep to wc -l in order to count the number of lines of output. The -c option to grep gives a count of lines that match the specified pattern and is generally faster than a pipe to wc, as in the following example:
Listing 17. Example of good habit #8: Counting lines with and without grep
~ $ time grep and tmp/a/longfile.txt | wc -l
2811
real 0m0.097s
user 0m0.006s
sys 0m0.032s
~ $ time grep -c and tmp/a/longfile.txt
2811
real 0m0.013s
user 0m0.006s
sys 0m0.005s
~ $

An addition to the speed factor, the -c option is also a better way to do the counting. With multiple files, grep with the -c option returns a separate count for each file, one on each line, whereas a pipe to wc gives a total count for all files combined.
However, regardless of speed considerations, this example showcases another common error to avoid. These counting methods only give counts of the number of lines containing matched patterns — and if that is what you are looking for, that is great. But in cases where lines can have multiple instances of a particular pattern, these methods do not give you a true count of the actual number of instances matched. To count the number of instances, use wc to count, after all. First, run a grep command with the -o option, if your version supports it. This option outputs only the matched pattern, one on each line, and not the line itself. But you cannot use it in conjunction with the -c option, so use wc -l to count the lines, as in the following example:
Listing 18. Example of good habit #8: Counting pattern instances with grep
~ $ grep -o and tmp/a/longfile.txt | wc -l
3402
~ $
In this case, a call to wc is slightly faster than a second call to grep with a dummy pattern put in to match and count each line (such as grep -c).

Match certain fields in output, not just lines

A tool like awk is preferable to grep when you want to match the pattern in only a specific field in the lines of output and not just anywhere in the lines.
The following simplified example shows how to list only those files modified in December:
Listing 19. Example of bad habit #9: Using grep to find patterns in specific fields
~/tmp $ ls -l /tmp/a/b/c | grep Dec
-rw-r–r– 7 joe joe 12043 Jan 27 20:36 December_Report.pdf
-rw-r–r– 1 root root 238 Dec 03 08:19 README
-rw-r–r– 3 joe joe 5096 Dec 14 14:26 archive.tar
~/tmp $

In this example, grep filters the lines, outputting all files with Dec in their modification dates as well as in their names. Therefore, a file such as December_Report.pdf is matched, even if it has not been modified since January. This probably is not what you want. To match a pattern in a particular field, it is better to use awk, where a relational operator matches the exact field, as in the following example:
Listing 20. Example of good habit #9: Using awk to find patterns in specific fields
~/tmp $ ls -l | awk ‘$6 == “Dec”‘
-rw-r–r– 3 joe joe 5096 Dec 14 14:26 archive.tar
-rw-r–r– 1 root root 238 Dec 03 08:19 README
~/tmp $

See Resources for more details about how to use awk.

Stop piping cats

A basic-but-common grep usage error involves piping the output of cat to grep to search the contents of a single file. This is absolutely unnecessary and a waste of time, because tools such as grep take file names as arguments. You simply do not need to use cat in this situation at all, as in the following example:
Listing 21. Example of good and bad habit #10: Using grep with and without cat

~ $ time cat tmp/a/longfile.txt | grep and
2811
real 0m0.015s
user 0m0.003s
sys 0m0.013s
~ $ time grep and tmp/a/longfile.txt
2811
real 0m0.010s
user 0m0.006s
sys 0m0.004s
~ $

This mistake applies to many tools. Because most tools take standard input as an argument using a hyphen (-), even the argument for using cat to intersperse multiple files with stdin is often not valid. It is really only necessary to concatenate first before a pipe when you use cat with one of its several filtering options.

Conclusion: Embrace good habits

It is good to examine your command-line habits for any bad usage patterns. Bad habits slow you down and often lead to unexpected errors. This article presents 10 new habits that can help you break away from many of the most common usage errors. Picking up these good habits is a positive step toward sharpening your UNIX command-line skills.

Popularity: 16% [?]

Tags: , , , ,

Linux与windows 互访总结(转)

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

Smb是实现linux和windows互访的一座桥梁,所以就先让我们来了解一下什么是smb,它的主要功能,通过smb实现共享的方法及配置。在最后我们还介绍了怎样使用mount命令来挂载windows共享目录。

一. Samba 简介

Samba(SMB是其缩写) 是一个网络服务器,用于Linux和Windows共享文件之用;Samba 即可以用于Windows和Linux之间的共享文件,也一样用于Linux和Linux之间的共享文件;不过对于Linux和Linux之间共享文件有更好的网络文件系统NFS,NFS也是需要架设服务器的;
大家知道在Windows 网络中的每台机器即可以是文件共享的服务器,也可以同是客户机;Samba 也一样能行,比如一台Linux的机器,如果架了Samba Server 后,它能充当共享服务器,同时也能做为客户机来访问其它网络中的Windows共享文件系统,或其它Linux的Sabmba 服务器;
我们在Windows网络中,看到共享文件功能知道,我们直接就可以把共享文件夹当做本地硬盘来使用。在Linux的中,就是通过Samba的向网络中的机器提供共享文件系统,也可以把网络中其它机器的共享挂载在本地机上使用;这在一定意义上说和FTP是不一样的。

二. samba服务器的启动及服务器配置文件

<一>Samba 有两个服务器,一个是smb,另一个是nmb;
smb 是Samba 的主要启动服务器,让其它机器能知道此机器共享了什么;
而nmb是解析用的,它把这台Linux机器所共享的工作组及在此工作组下的netbios name解析出来。如果不打开nmb服务器的话,只能通过IP来访问,也就是说在windows的IE浏览器中输入linux计算机的ip才能访问,例如:\\192.168.0.1\test,而输入linux的计算机名却不能进行访问,比如:\\wangyh\test。
所以在安装完samba服务器后必须启动smb的服务。下面是启动、查看、关闭smb服务器的命令

1.一般的情况下,在RPM包的系统,如果是用RPM包安装的Samba ,一般可以通过如下的方式来启动Samba服务器;
[root@localhost ~]# /etc/init.d/smb start
2.对所有linux系统来说,通用的方法就是直接运行smb 和nmb;进入终端在root权限下把samba所在的目录敲到命令行里即可,但是您要知道smb和nmb所在的目录;如果是自己编译的Samba ,其存放的目录您应该知道;
例如:samba所在的目录为 /usr/sbin/smbd(nmbd),启动smb输入如下:
[root◎wangyh:/home/wangyh]#/usr/sbin/smbd
[root◎wangyh:/home/wangyh]#/usr/sbin/nmbd
3.启动完后还要检查一下samba服务器是否运行起来,查看命令如下:
[root◎wangyh:/home/wangyh]#/pgrep smbd
[root◎wangyh:/home/wangyh]#/pgrep nmbd
4.关闭smb服务器,命令如下:
[root◎wangyh:/home/wangyh]#/pkill smbd
[root◎wangyh:/home/wangyh]#/pkill nmbd

<二> smb的配置文件及服务器端和客户端的工具

1.如果我们是用Linux发行版自带的Samba软件包,一般情况下Samba服务器的配置文件都位于/etc/samba目录中,服务器的主配置文件是smb.conf;也有用户配置文件 smbpasswd、smbusers和lmhosts等;还有一个文件是secrets.tdb,这个文件是Samba 服务器启动时自动生成的;
2.在smb服务器中几个主要常用的服务器端工具为smbd、nmbd、smbpasswd;客户端为smbmount(在ubuntu l 6.06的内核中没有smbmount,所以它不支持这个命令,但可以用mount 加参数的形势来实现挂载),smbclient等。

<三>实现 ubuntu linux 6.06 与windows系统的互访

1.Windows 2000/xp/2003访问ubuntu linux 系统(已测试)

第一步:安装samba,smbclient,smbfs
命令: [root◎wangyh:/home/wangyh]# apt-get install samba
[root◎wangyh:/home/wangyh]# apt-get install smbclient
[root◎wangyh:/home/wangyh]# apt-get install smbfs
第二步:更改smb.conf 文件
对于新手来说在改之前最好将smb.conf文件进行备份。
修改smb.conf文件用vi 或gedit命令都可以,如下所示:
[root◎wangyh:/home/wangyh]# vi /etc/samba/smb.conf
[root◎wangyh:/home/wangyh]# gedit /etc/samba/smb.conf
在[global]这段中修改的内容如下:
在[global]下面加入 两行
doc charset=UTF-8
unix charset=UTF-8
workgroup=WORKGROUP
在下面添加一行 netbios name=wangyh(这里填本机的计算机名)

找到passwd program行将其改为
passwd program=/etc/samba/smbpasswd
找到 security行将其改为
security=user
找到[homes]段修改内容如下
browseable=yes
writable=yes
如果我们想在文件中创建共享文件夹,我们只需要在后面写入段
[wangyh](共享文件)
path=/home/wangyh(共享文件夹路径)
browseable=yes
public=yes(在网上邻居中显示)
writable=yes
当然在ubuntu linux系统中点击系统->系统管理->共享文件夹,在这里选择共享的文件夹也是可以,设置完后,系统自动将你所设置的共享信息写入smb.conf文件中。

注解:

workgroup 就是Windows中显示的工作组;
netbios name 就是在Windows中显示出来的计算机名;
security 这是验证和登录方式,这里我们用了user ;验证方式有好多种,这是其中常用的一种;一种是share的验证方式,这种方式就是不用设置用户和密码了,但这种方式的安全级别很低,一般我们不采用这种方式;还有一种是server,server模式要求用户的认证由Samba服务器或NT服务器来完成。
Browseable为是否可以浏览,browseable=yes,为可浏览,我们在工作组下就能看到共享文件夹。如果您browseable=no ,那末文件夹在工作组中就不显示。
writeable 为是否可写,这里我设置为可写;

第三步:添加smb用户,并设置linux用户密码

这里要注意的是在添加smb用户之前,必须将其添加成ubuntu linux 用户,命令如下:
1)添加成linux用户
adduser –a heqing(heqing为用户名)
enter new UNIX password:*****
retype new UNIX password:*****
剩下的项按提示输入即可
2)添加成smb用户,并设置smb密码
smbpasswd -a heqing(已存在的linux用户)
NEW SMB passwd:******
Retype NEW SMB passwd:******
添加成功
最后要说明的是heqing用户的linux用户密码可以与smb密码不相同

第四步:设置windows客户端

基本完成上面的配置后windows就可以访问ubuntu linux系统中的共享文件夹了,如果windows 在网上邻居或通过IE看不到的话,要查看两点
在windows终端ping ubuntu linux系统的主机ip,如果可以ping通,看下一条。
1) 查看windows防火墙中是否禁止其他计算机访问本机或是否允许ping入。如果可以,再看下一条。
查看windows 用户设置中的guest用户是否禁用,如果禁用,将其解除即可。
通过以上设置在windows计算机的网上邻居或IE都可以看到ubuntu linux系统的共享文件,并可以对其修改,粘贴新文件,复制原有文件等。
2.Ubuntu linux 系统访问windows xp/2003/2000系统

在我的测试过程中,实现ubuntu linux系统访问windows系统的方式有3种,我将这几种方式说明如下:

第一种:用smb访问
如果在ubuntu linux系统终端pingwindows主机的ip地址可以ping通,且windows计算机中有共享的文件夹,则打开位置下的网络服务器,点击界面中windows网络->workgroup,在workgroup组中就可以看到windows计算机名,双击会弹出输入用户名和密码的对话框,将我们上面设置smb的用户名和密码输入就可以进行访问。
第二种:用ubuntu 系统自带的连接到服务器功能访问
在位置中启动连接到服务器,在服务类型中选择“windows共享”,在服务器中输入windows计算机的ip地址或计算机名,点击连接,这样在系统桌面中就会显示一个连接到windows 计算机的文件卷。
我们可以像访问ubuntu系统中的磁盘一样来访问它,同样在访问的时候要输入smb的用户名和密码。
第三种:用mount’挂载windows的共享目录到本地磁盘
首先要在ubuntu系统中建立一个挂载点,在这里我们建立的挂在点为 /mnt/wind
同样在保证网络连接正常,且windows中有共享目录的前提下,进行一下操作
以windows的ip为192.168.0.1,共享文件夹为share为例
命令如下:
mount -t smbfs –o username=wangyh,password=123456 //192.168.0.1/share /mnt/wind
有是这样在访问的时候会出现中文乱码的问题,这样我们可以按下面命令来执行,可以解决这个问题
mount -o smbfs -o iocharset=uft8,codepage=cp936,clmask=777,fmask=777,userneme=wangyh,password=123456 //192.168.0.1/share /mnt/wind
注解:usename和password都为smb的用户和密码,如果要详细了解mount命令,使用man mount在终端查看详细用法
第四种:使用smbclient 命令
命令如下:
smbclient –L //192.168.0.1/share -U wangyh
password:输入smb用户wangyh的密码
回车就可以用,用命令get下载文件,用put上传文件。
注解:
smbclient介绍:
命令 说明
?或help [command] 提供关于帮助或某个命令的帮助
![shell command] 执行所用的SHELL命令,或让用户进入 SHELL提示符
cd [目录] 切换到服务器端的指定目录,如未指定,则 smbclient 返回当前本地目录
lcd [目录] 切换到客户端指定的目录;
dir 或ls 列出当前目录下的文件;
exit 或quit 退出smbclient
get file1 file2 从服务器上下载file1,并以文件名file2存在本地机上;
如果不想改名,可以把file2省略
mget file1 file2 file3 filen 从服务器上下载多个文件;
md或mkdir 目录 在服务器上创建目录
rd或rmdir 目录 删除服务器上的目录
put file1 [file2] 向服务器上传一个文件file1,传到服务器上改名为file2;
mput file1 file2 filen 向服务器上传多个文件

原文来自 ubuntu cn forum

Popularity: 13% [?]

Tags: , , , , , ,

写在multiget1.0发表之后(转)

Posted 9 months, 2 weeks ago at 11:32 am. 0 comments

写在multiget1.0发表之后[感谢及期望]

终于能够发布1.0版了,我很欣慰,感谢上帝,让我在误删除0.1.1版之前一分钟做了个备份,否则就没有今天的话。

从发布0.1.1版到现在,刚好3个月,也是我能预期支付的时间。期间小病了两次,母亲大病一次,孩子发烧病了3次,真是一个不堪的时间段。所幸终于坚持了下来。

按时间先后:

感谢CU网友langue校正英语翻译,
感谢CU网友abel校正繁体中文翻译,
感谢everest老大cjacker提供unicode编译补丁,
感谢linuxsir网友MatthewGong打包rpm,

你们的名字在下一个版本中将被加入。

同时,特别感谢同济大学建筑工程系的老师们!虽然我知道你们也许不会看见,但这是我应该做的。

下面来谈谈MultiGet本身及对未来的期望。

一直以来我没有给程序做一个详细的文档,一是因为时间的原因,二是不擅长写东西。我想整个程序的结构脉络还是比较清晰的,有一定C++基础的人应该能读懂。因为第一次写Linux程序,可能很多地方处理的不那么符合Linux标准,比如翻译的处理(受MS影响太深?)。界面和底层的结合也比较紧密(考虑到图形界面的反馈效率),似乎不那么科学。底层的代码都是我自己写的,可能隐含了比现有的库更多的错误,也没有经过太长时间的检验,都需要加以改进或者必要时直接替换成其他完善的库。

对于MultiGet的未来,我不敢期望太多,只希望后续开发有兴趣的人能参与进来。1.0版之前,是我包办了,但之后肯定是不可能的,一个人的精力毕竟有限,能做的贡献微不足道。当前MultiGet在sourceforge上的排名还算比较靠前,最近几天都在300-400之间,应该值得继续做下去。

我个人想在MultiGet 2.0实现如下主要功能:

1:改写系统结构,实现插件接口,这样有一些边缘的东西就可以用插件来挂接,比如镜像查询可以利用迅雷或其他服务商的,可以选择安装。
2:利用插件集成BT接口,甚至emule等其他流行P2P,当然从其他项目剥离代码。
3:更强大的文件管理功能。
4:rtsp/mms,https/sftp,rsync协议。
5:或许转向基于GTK+以避免wxGTK的包装缺陷。
6:跨平台到windows,其他平台如BSD,MAC有余力则做。

如果能够实现这些功能,则无疑是目前最强大的下载器,实际上当前的多地址下载的智能优化已经是非常领先的。
但要实现这些功能有一定的困难,主要在于投入比较大,三五个人肯定支持不起来,估计10个人都要8个月才可以得到一个稳定版本。

希望有兴趣,有能力的人能够参与进来,基本上2.0会从头做起,也许改名也说不定,一切都可以商量,我不一定做teamleader,我在Linux下的经验有限,应由大家推选,我做个组织者和coder就可以。

最少要10个人才启动这个2.0版计划,少于这个数字真的干不了,我期望是15-20人,因为很多人都是业余时间来做的。有兴趣的请和我联系 multiget@gmail.com 或跟贴,我好统计人数,如果人多,还可以适当增加目标功能。希望能展开这个计划,让世界知道中国人不是总拿别人的代码!!

via http://bbs.chinaunix.net/archiver/?tid-854742.html

Popularity: 14% [?]

Tags: , , ,

ad 468x60