g for linux?linux中for的用法
大家好,如果您还对g for linux不太了解,没有关系,今天就由本站为大家分享g for linux的知识,包括linux中for的用法的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!
linux下scp远程拷贝包含空格的目录或者文件的解决方法
今天遇到个白痴问题,用了linux这么久了,竟然还出现如此低级的错误。
记录下,免得又忘记。
描述:
今天需要远程拷贝一些文件到电脑上,ubuntu的。远程电脑是debian的。
文件和目录包含大量的空格,采用scp拷贝,例:
scp-r root@192.168.0.51:/home/xxj/Documents/files/xx xx jj.tar.gz./
最开始就意识到了是空格问题,就采用了文件空格前加“/”,死活不行,
scp-r root@192.168.0.51:/home/xxj/Documents/files/xx/ xx/ jj.tar.gz./
然后又使用引号,单引号,双引号都用了,还是不行。
scp-r root@192.168.0.51:'/home/xxj/Documents/files/xx xx jj.tar.gz'./
scp-r root@192.168.0.51:"/home/xxj/Documents/files/xx xx jj.tar.gz"./
最后同时在空格前加斜杠,整个文件目录加引号才行
scp-r root@192.168.0.51:"/home/xxj/Documents/files/xx/ xx/ jj.tar.gz"./
linux下带空格文件批量cp和scp补充:
背景:主备机同步文件失败,现在需要人工手动的在备机上补齐文件,但是发现文件名竟然有空格
主机的IP:192.168.0.90主机文件清单名称:src_file.txt,备机文件清单名称:dst_file.txt,
从下面可以知道,主备机的文件路径和文件的名称是不一样的,同步的时候,从src_file.txt copy到dst_file.txt需要一一对应,另备机上的目录并不能保证都存在,所以需要先把备机上的目录结构先创建出来
[root@station90 file]# cat src_file.txt
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# cat dst_file.txt
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
从dst_file.txt(备机文件)可以知道,目录的级别不是一样的,也就是说第一行为6级目录,第二行却为4级目录,如何取出目录结构,先创建出目录结构呢?执行如下命令即可:
[root@station90 file]# awk-F'/''{for(i=1;iNF;i++){printf$i"/"}printf"/n"}' dst_file.txt//注意iNF,即不需要循环到最后一列,printf$i是不打印回车的,一行循环完毕后,printf"/n"会打印回车,这样的结果刚好是我们需要的
/home/dst/1038/g07/k/3/
/home/dst/10327/g04/
/home/dst/1038/g06/lhppww/
/home/dst/101117/06g090/
/home/dst/1052g056/0590/33/
[root@station90 file]# awk'{print$1}' dst_file.txt| awk-F'/''{print substr($0,1,(length($0)-length($NF)))}'/tmp/dst_dir.txt
//根据dst_file.txt的特点,先执行awk'{print$1}' dst_file.txt以空格作为分隔符,取出第一列,这样取出的就没有空格,$0为文本里面包含的所有内容,再通过截取字符串的方式,substr($0(去除空格的文本本身),1(从第一个字符开始),(length($0)-length($NF))所有字符的长度减去最后一列的字符长度,就等于我们需要的字符长度,也就是从1,我们需要的字符长度,结果就是我们需要的了
[root@station90 file]# cat/tmp/dst_dir.txt
/home/dst/1038/g07/k/3/
/home/dst/10327/g04/
/home/dst/1038/g06/lhppww/
/home/dst/101117/06g090/
/home/dst/1052g056/0590/33/
[root@station90 file]# mkdir-p$(cat/tmp/dst_dir.txt)
[root@station90 file]# ls-ld$(cat/tmp/dst_dir.txt)/目录结构已经创建出来了
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/101117/06g090/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/10327/g04/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1038/g06/lhppww/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1038/g07/k/3/
drwxr-xr-x 2 root root 4096 11-10 17:37/home/dst/1052g056/0590/33/
part1:单纯的从本机copy到本机的其他目录,会比较简单,如下所示:
[root@station90 file]# sed-i-e"s/^/cp/"/g"-e"s/$//"/g" src_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat src_file.txt
cp"/home/src/10329/g0197/k/19xiazai h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp"
cp"/home/src/101117/060/090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]#paste-d'' src_file.txt dst_file.txt cp.sh//src_file.txt和dst_file.txt文本以空格作为分隔符合成一个文件
[root@station90 file]#cat cp.sh
cp"/home/src/10329/g0197/k/19xiazai h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
cp"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" cp.sh//在脚本中添加#!/bin/bash
[root@station90 file]# cat cp.sh
#!/bin/bash
cp"/home/src/10329/g0197/k/19xiazai h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
cp"/home/src/10327/g0194/10ng h264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
cp"/home/src/10329/g0196/lh/10xiazai h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
cp"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
cp"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x cp.sh
[root@station90 file]# ls-l"/home/dst/1038/g07/k/3/10290upload h264.3gp"//执行cp.sh脚本前,文件是不存在的
ls:/home/dst/1038/g07/k/3/10290upload h264.3gp:没有那个文件或目录
[root@station90 file]#./cp.sh
[root@station90 file]# ls-l"/home/dst/1038/g07/k/3/10290upload h264.3gp"//ok,copy成功
-rw-r--r-- 1 root root 0 11-10 17:44/home/dst/1038/g07/k/3/10290upload h264.3gp
part2从远成主机scp文件过来,先举例一错误的例子,正确的总是慢慢的总结出来的
执行rm.sh,删除刚才copy过来的文件
[root@station90 file]# cat rm.sh
#!/bin/bash
rm-f"/home/dst/1038/g07/k/3/10290upload h264.3gp"
rm-f"/home/dst/10327/g04/10ng h264 upload.3gp"
rm-f"/home/dst/1038/g06/lhppww/10upload h263.3gp"
rm-f"/home/dst/101117/06g090/1011.3gp"
rm-f"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]#./rm.sh
[root@station90 file]# sed-i-e"s/^/scp/ root/@192/.168/.0/.90/:/"/g"-e"s/$//"/g" src_file.txt
//在src_file.txt文件的开头和结尾添加双引号,同时添加root@192.168.0.90:,最终如下所示
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp"
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt//在文本中的开头和结尾添加双引号
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# paste-d'' src_file.txt dst_file.txt scp.sh//合成shell脚本步骤一
[root@station90 file]# cat scp.sh
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh//合成shell脚本步骤二
[root@station90 file]# cat scp.sh//看过去没有问题是吧,借鉴上面的part1部分cp得到scp嘛,其实是有问题的
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazaih264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ngh264 xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazaih263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_040_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh//发现了吧,从远程主机找不到这些文件,只有没有空格的文件名称才可以复制过来
scp:/home/src/10329/g0197/k/19xiazai: No such file or directory
scp: h264.3gp: No such file or directory
scp:/home/src/10327/g0194/10ng: No such file or directory
scp: h264: No such file or directory
scp: xiazai.3gp: No such file or directory
scp:/home/src/10329/g0196/lh/10xiazai: No such file or directory
scp: h263.3gp: No such file or directory
1011.3gp 100% 0 0.0KB/s 00:00
scp:/home/src/10520/056/0590/99833/10x_04: No such file or directory
scp: 0_0_3.3gp: No such file or directory
继续错误的里程,想想,肯定是空格没有进行转义,遂src_file.txt文件和src_file.txt文件中的空格都进行转义,于是下面错误的过程来了:
[root@station90 file]# cat src_file.txt//原文件内容格式
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" src_file.txt//文件的开头和结尾加上双引号
[root@station90 file]# cat src_file.txt
"/home/src/10329/g0197/k/19xiazai h264.3gp"
"/home/src/10327/g0194/10ng h264 xiazai.3gp"
"/home/src/10329/g0196/lh/10xiazai h263.3gp"
"/home/src/101117/060/090/1011.3gp"
"/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"s//////g" src_file.txt//对空格进行转义
[root@station90 file]# cat src_file.txt
"/home/src/10329/g0197/k/19xiazai/ h264.3gp"
"/home/src/10327/g0194/10ng/ h264/ xiazai.3gp"
"/home/src/10329/g0196/lh/10xiazai/ h263.3gp"
"/home/src/101117/060/090/1011.3gp"
"/home/src/10520/056/0590/99833/10x_04/ 0_0_3.3gp"
[root@station90 file]# cat dst_file.txt///原文件内容格式
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g"-e"s//////g" dst_file.txt//在文件开头和结尾加双引号和空格进行转义
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload/ h264.3gp"
"/home/dst/10327/g04/10ng/ h264/ upload.3gp"
"/home/dst/1038/g06/lhppww/10upload/ h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04/ 0_0_3.3gp"
[root@station90 file]# sed-i"s/^/scp/ root/@192/.168/.0/.90/:/g" src_file.txt//对远程文件(192.168.0.90)src_file.txt加上scp root@192.168.0.90:
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh//对文件加上#!/bin/bash
[root@station90 file]# cat scp.sh
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp""/home/dst/1038/g07/k/3/10290upload/ h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp""/home/dst/10327/g04/10ng/ h264/ upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp""/home/dst/1038/g06/lhppww/10upload/ h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04/ 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh//执行脚本,看过去是都成功了吧,但是……
19xiazai h264.3gp 100% 0 0.0KB/s 00:00
10ng h264 xiazai.3gp 100% 0 0.0KB/s 00:00
10xiazai h263.3gp 100% 0 0.0KB/s 00:00
1011.3gp 100% 0 0.0KB/s 00:00
10x_04 0_0_3.3gp 100% 0 0.0KB/s 00:00
[root@station90 file]#
[root@station90 file]# cd/home/dst/1038/g07/k/3/
[root@station90 3]# ls//看到没有,复制到本地的文件有了/
10290upload/ h264.3gp
[root@station90 dst]# pwd
/home/dst
[root@station90 dst]# find./-type f
./10327/g04/10ng/ h264/ upload.3gp
./1038/g07/k/3/10290upload/ h264.3gp
./1038/g06/lhppww/10upload/ h263.3gp
./1052g056/0590/33/10x_04/ 0_0_3.3gp
[root@station90 dst]# find./-type f-exec rm{}/;//删除复制过来错误的文件
[root@station90 dst]# find./-type f
[root@station90 file]# cat src_file.txt//下面的步骤是正确的,其实只需要对src_file.txt文件的空格加上/进行转义,对本地
dst_file.txt文件只需要在文件的开头结尾加上双引号,就好了
/home/src/10329/g0197/k/19xiazai h264.3gp
/home/src/10327/g0194/10ng h264 xiazai.3gp
/home/src/10329/g0196/lh/10xiazai h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04 0_0_3.3gp
[root@station90 file]# sed-i"s//////g" src_file.txt
[root@station90 file]# cat src_file.txt
/home/src/10329/g0197/k/19xiazai/ h264.3gp
/home/src/10327/g0194/10ng/ h264/ xiazai.3gp
/home/src/10329/g0196/lh/10xiazai/ h263.3gp
/home/src/101117/060/090/1011.3gp
/home/src/10520/056/0590/99833/10x_04/ 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^/scp/ root/@192/.168/.0/.90/:/"/g"-e"s/$//"/g" src_file.txt
[root@station90 file]# cat src_file.txt
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp"
[root@station90 file]# cat dst_file.txt
/home/dst/1038/g07/k/3/10290upload h264.3gp
/home/dst/10327/g04/10ng h264 upload.3gp
/home/dst/1038/g06/lhppww/10upload h263.3gp
/home/dst/101117/06g090/1011.3gp
/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp
[root@station90 file]# sed-i-e"s/^//"/g"-e"s/$//"/g" dst_file.txt
[root@station90 file]# cat dst_file.txt
"/home/dst/1038/g07/k/3/10290upload h264.3gp"
"/home/dst/10327/g04/10ng h264 upload.3gp"
"/home/dst/1038/g06/lhppww/10upload h263.3gp"
"/home/dst/101117/06g090/1011.3gp"
"/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# sed-i"1,1s/^//#/!//bin//bash/n/g" scp.sh
[root@station90 file]# cat scp.sh//最终的脚本样本
#!/bin/bash
scp root@192.168.0.90:"/home/src/10329/g0197/k/19xiazai/h264.3gp""/home/dst/1038/g07/k/3/10290upload h264.3gp"
scp root@192.168.0.90:"/home/src/10327/g0194/10ng/h264/ xiazai.3gp""/home/dst/10327/g04/10ng h264 upload.3gp"
scp root@192.168.0.90:"/home/src/10329/g0196/lh/10xiazai/h263.3gp""/home/dst/1038/g06/lhppww/10upload h263.3gp"
scp root@192.168.0.90:"/home/src/101117/060/090/1011.3gp""/home/dst/101117/06g090/1011.3gp"
scp root@192.168.0.90:"/home/src/10520/056/0590/99833/10x_04/0_0_3.3gp""/home/dst/1052g056/0590/33/10x_04 0_0_3.3gp"
[root@station90 file]# chmod+x scp.sh
[root@station90 file]#./scp.sh
19xiazai h264.3gp 100% 0 0.0KB/s 00:00
10ng h264 xiazai.3gp 100% 0 0.0KB/s 00:00
10xiazai h263.3gp 100% 0 0.0KB/s 00:00
1011.3gp 100% 0 0.0KB/s 00:00
10x_04 0_0_3.3gp 100% 0 0.0KB/s 00:00
[root@station90 file]# cd/home/dst
[root@station90 dst]# find./-type f//现在OK了,linux水平不行,被折磨了2天呐
./10327/g04/10ng h264 upload.3gp
./101117/06g090/1011.3gp
./1038/g07/k/3/10290upload h264.3gp
./1038/g06/lhppww/10upload h263.3gp
./1052g056/0590/33/10x_04 0_0_3.3gp
linux系统下邮件服务器sendmail的配置
安装所需软件
sendmail.8.12.10.tar.gz
cyrus-sasl-2.1.18.tar.gz
安装步骤
1、 先安装cyrus-sasl-2.1.18.tar.gz,sendmail安装时要用到sasl(简单认证和安全层协议)的库文档的头文档。
解压缩:
# tar-zxvf cyrus-sasl-2.1.18.tar.gz
编译:
进入刚解压的源码目录,运行以下命令完成安装。
#./configure--prefix=/usr/local/sasl2--enable-login
一定要加--enable-login,因为SASL2默认不支持login这种验证方式,而OUTLOOK是通过
login来进行SMTP验证的。
#make #编译
#make install #安装
完成以上linux程式安装三步曲之后,就能够开始配置和测试了。
2、配置SASL
为了把SASL应用于sendmail认证,还需进行一些配置工作。sendmail会到/usr/lib目录下去
找SASL2库,而我们是把程式安装在/usr/local/sasl2中,为什么不把软件安装在/usr/lib目录
呢?这主要是为了好管理自已安装的软件啦。所以我们要在/usr/lib目录下做一个链接:
# cd/usr/lib
# ln-s/usr/local/sasl2/lib/*.
ok,接着要在/var/目录下建一个目录给saslauthd进程存在临时数据。
# cd/var
# mkdir state
# cd state
# mkdir saslauthd
注:假如没有这些目录,运行saslauthd时,会提示出错。
ok,接着为确保CYRUS-SASL2函数库知道怎样验证所收来的SASL认证请求,必须创建一
个SASL的配置文档来把MTA程式定义成一个SASL应用。配置文档名为Sendmail.conf(注
意是大写的S),位于/usr/lib/sasl2目录中,也就是/usr/local/sasl2/lib/sasl2这个目录,记得上
面新建的链接了吗?在该文档中您定义您希望使用的认证数据库方法,以下这个例子使用
saslauthd来验证认证请求。
# cd/usr/lib/sasl2
# echo'pwcheck_method: saslauthd'; Sendmail.conf
3、测试
ok,现在能够运行saslauthd了,并进行测试。
# cd/usr/local/sasl2/sbin
#./saslauthd-a shadow
用shadow的用户和密码进行验证
#./testsaslauthd-u userid-p password
0: OK"Success."
假如出现以上信息,就说明saslauthd正常运行了。testsaslauthd程式默认是没有编译的,您
需要在源码目录树的saslauthd子目录中运行 # make testsaslauthd命令生成。
4、sasl2安装完成后,就要开始安装sendmail了。
先解压sendmail源码。
# tar-zxvf sendmail.8.12.10.tar.gz
假如要sendmail支持SASL,需要修改源码的位置配置文档site.config.m4。site.config.m4位
于源码目录树的devtools/Site。文档中应包含以下行:
PREPENDDEF(`confMAPDEF', `-DMAP_REGEX')
APPENDDEF(`confENVDEF', `-DTCPWRAPPERS-DSASL=2')
APPENDDEF(`conf_sendmail_LIBS', `-lwrap-lsasl2')
APPENDDEF(`confLIBDIRS', `-L/usr/local/sasl2/lib')
APPENDDEF(`confINCDIRS', `-I/usr/local/sasl2/include')
第一行配置正则表达式相关内容
第二、三行配置表示在sendmail程式中支持sasl2和tcp_wrapper(可通过hosts.allow和
hosts.deny控制访问)
第四、五行配置指出sasl2的库文档和头文档的位置。
ok,接着在编译前要建立一些用户和目录,并确保有正确的权限。
sendmail必须有一个set-group-id(默认是smmsp组)的程式来在一个组可写的目录中查询
排队邮件。所以我们要建立一个smmsp用户和组。并建立如下目录并配置相应的权限,具
体配置需要可查询源码目录树下sendmail/SECURITY文档。
# groupadd smmsp
# useradd smmsp-d/var/spool/clientmqueue-s/dev/null
# mkdir/var/spool/clientmqueue
# chown-R smmsp:smmsp/var/spool/clientmqueue
# chmod-R 770/var/spool/clientmqueue
# mkdir/etc/mail
# mkdir/var/spool/mqueue
# chmod go-w//etc/etc/mail/usr/var/var/spool/var/spool/mqueue
# chown root//etc/etc/mail/usr/var/var/spool/var/spool/mqueue
ok,接下来就能够进入源码目录树开始编译了。
#./Build-c
-c选项能删除上次编译产生的文档。
#./Build install
编译完成后就能够进行安装。
5、sendmail配置
要sendmail正常运行,还需配置几个文档,首先最重要的就是sendmail.cf文档了。他在源
码目录树下的cf/cf目录下有很多例子可参考。您能够拷贝使用。由于sendmail.cf中的语法
很复杂,所以不建议手工修改。我们能够以sendmail.mc文档配合sendmail-cf目录下的宏通
过m4预处理器自动生成。m4预处理器用来从一组宏文档中创建sendmail配置文档。宏文
件作为输入被读进来。宏被展开,然后写到一个输出文档。sendmail-cf目录一般放在/usr/share
目录下。其实sendmail-cf目录的内容和源码目录树下的cf目录的内容是相同的,所以,为
了确保sendmail-cf目录的内容和安装版本同步,要把源码目录树下的cf目录的内容拷贝到
/usr/share/sendmail-cf目录。
sendmail.mc配置文档内容如下:
divert(-1)
include(`/usr/share/sendmail-cf/m4/cf.m4')
VERSIONID(`linux setup for Red Hat Linux')dnl
OSTYPE(`linux')
dnl Uncomment and edit the following line if your mail needs to be sent out
dnl through an external mail server:
dnl define(`SMART_HOST',`smtp.your.provider')
define(`confDEF_USER_ID',``8:12'')dnl
undefine(`UUCP_RELAY')dnl
undefine(`BITNET_RELAY')dnl
dnl define(`confAUTO_REBUILD')dnl
define(`confTO_CONNECT', `1m')dnl
define(`confTRY_NULL_MX_LIST',true)dnl
define(`confDONT_PROBE_INTERFACES',true)dnl
define(`PROCMAIL_MAILER_PATH',`/usr/bin/procmail')dnl
define(`ALIAS_FILE', `/etc/mail/aliases')dnl
define(`STATUS_FILE', `/etc/mail/statistics')dnl
define(`UUCP_MAILER_MAX', `2000000')dnl
define(`confUSERDB_SPEC', `/etc/mail/userdb.db')dnl
define(`confPRIVACY_FLAGS', `authwarnings,novrfy,noexpn,restrictqrun')dnl
define(`confAUTH_OPTIONS', `A')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5
LOGIN PLAIN')dnl
dnl define(`confCACERT_PATH',`/usr/share/ssl/certs')
dnl define(`confCACERT',`/usr/share/ssl/certs/ca-bundle.crt')
dnl define(`confSERVER_CERT',`/usr/share/ssl/certs/sendmail.pem')
dnl define(`confSERVER_KEY',`/usr/share/ssl/certs/sendmail.pem')
dnl define(`confTO_QUEUEWARN', `4h')dnl
dnl define(`confTO_QUEUERETURN', `5d')dnl
dnl define(`confQUEUE_LA', `12')dnl
dnl define(`confREFUSE_LA', `18')dnl
define(`confTO_IDENT', `0')dnl
dnl FEATURE(delay_checks)dnl
FEATURE(`no_default_msa',`dnl')dnl
FEATURE(`smrsh',`/usr/sbin/smrsh')dnl
FEATURE(`mailertable',`hash-o/etc/mail/mailertable.db')dnl
FEATURE(`virtusertable',`hash-o/etc/mail/virtusertable.db')dnl
FEATURE(redirect)dnl
FEATURE(always_add_domain)dnl
FEATURE(use_cw_file)dnl
FEATURE(use_ct_file)dnl
dnl The'-t' option will retry delivery if e.g. the user runs over his quota.
FEATURE(local_procmail,`',`procmail-t-Y-a$h-d$u')dnl
FEATURE(`access_db',`hash-T;-o/etc/mail/access.db')dnl
FEATURE(`blacklist_recipients')dnl
EXPOSED_USER(`root')dnl
dnl This changes sendmail to only listen on the loopback device 127.0.0.1
dnl and not on any other network devices. Comment this out if you want
dnl to accept email over the network.
DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')
dnl NOTE: binding both IPv4 and IPv6 daemon to the same port requires
dnl a kernel patch
dnl DAEMON_OPTIONS(`port=smtp,Addr=::1, Name=MTA-v6, Family=inet6')
dnl We strongly recommend to comment this one out if you want to protect
dnl yourself from spam. However, the laptop and users on computers that do
dnl not have 24x7 DNS do need this.
FEATURE(`accept_unresolvable_domains')dnl
dnl FEATURE(`relay_based_on_MX')dnl
MAILER(smtp)dnl
MAILER(procmail)dnl
记得要使以下两个条语句有效,他的意思是假如access.db访问控制没有配置,则启用以下
验证方式进行smtp验证。
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5
LOGIN PLAIN')dnl
sendmail宏定义说明
divert(n) 为m4定义一个缓冲动作,当n=-1时缓冲被删除,n=0时开始一个新缓冲
OSTYPE定义宏所使用的操作系统,该宏允许m4程式增加同相关操作系统相关的
文档
Domain定义MTA将使用哪些域来传输邮件
Feature定义配置文档中使用的一个特定的功能集
Define定义配置文档中的一个特定的选项值
MASQUERADE_AS 定义sendmail来应答邮件的其他主机名
MAILER定义sendmail使用的邮件传输方法
dnl注释
完成sendmail.mc文档的编写后就能够用m4程式生成正式的sendmail.cf配置文档。语法如
下:
# m4 sendmail.mc; sendmail.cf
也能够用源码目录树下的cf/cf/Build命令生成。前提是您要在该目录下有一个sendmail.mc
文档。语法如下:
#./Build sendmail.cf
ok,接着就能够把sendmail.cf and submit.cf文档安装到/etc/mail目录中的,语法如下:
#./Build install-cf
其实也能够自已用cp命令拷贝的啦。记得把sendmail.mc文档也拷贝一份到/etc/mail目录,
以便以后修改配置时可重新生成sendmail.cf文档。
ok,接着到/etc/mail目录下配置一些文档。
# cd/etc/mail
# echo'examply.com';; local-host-names 接收邮件的主机名
# echo'localhost RELAY' ;; access 用来拒绝或允许来自某个域的邮件,本例允许
本地转发。
# makemap hash access生成access.db数据库
# touch domaintable 用来把旧域名映射互新域名
# makemap hash domaintable来覆盖向指定域的路由
# makemap hash mailertable用来把用户和域名映射到其他地址
# makemap hash virtusertable别名数据库,文本形式。可参照源码目录树下
sendmail/aliases文档。
# newaliases 从文本文档中创建一个新的别名数据库文档。
# sendmail-v-bi 调试启动。
/etc/mail/aliases: 42 aliases, longest 10 bytes, 432 bytes total
假如出现以上提示信息,则启动成功。可用以下命令正式启动:
# sendmail-bd-q30m
该命令以后台进程方式(-bd)运行,并使其每隔30分钟(-q30m)轮询一次未发送邮件队
列,检查是否有新邮件。
服务器启动后,能够用telnet连接服务器。
# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is'^]'.
220 test.tigerhead ESMTP Sendmail 8.12.10/8.12.10; Tue, 30 Mar 2004 14:50:14+0800
ehlo test 您输入的命令,按回车结束。
250-test.tigerhead Hello LOCALHOST.localdomain [127.0.0.1], pleased to meet you
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-8BITMIME
250-SIZE
250-DSN
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5 LOGIN PLAIN
250-DELIVERBY
250 HELP
以250-开头的为服务器的响应信息。注意倒数第三行,这就是成功配置smtp验证的显示。
输入quit离开。
安装完成后为确保系统安全,还需调整一些目录的权限。可参考sendmail源码目录下
sendmail/SECURITY文档。
# chmod 0640/etc/mail/aliases/etc/mail/aliases.{db,pag,dir}
# chmod 0640/etc/mail/*.{db,pag,dir}
# chmod 0640/etc/mail/statistics/var/log/sendmail.st
# chmod 0600/var/run/sendmail.pid/etc/mail/sendmail.pid
-r-xr-sr-x root smmsp .../PATH/TO/sendmail
drwxrwx--- smmsp smmsp .../var/spool/clientmqueue
drwx------ root wheel .../var/spool/mqueue
-r--r--r-- root wheel .../etc/mail/sendmail.cf
-r--r--r-- root wheel .../etc/mail/submit.cf
6、sendmail功能介绍
aliases别名数据库配置
test: test1,test2,test3 配置test群组别名,test不是个实际的用户,只是个别名。发给
test的信,test1,2,3都可收到。
test: test,testbak 在testbak邮箱中备份test的邮件。
test: test,test@163.com 远程邮件备份,原理同上。
test: :include:/etc/mail/userlist 使用用户列表配置群组。
userlist格式如下:
test1,/
test2,/
test3,/
test4
关于系统预设aliases,由于sendmail预设用mailer-daemon and postmaster作为资料传送者,
或邮件退回的帐号,但系统实际没有这两个帐号,所以要如下配置别名。
mailer-daemon: postmaster
postmaster: root
配置完成后不要忘了用newaliases命令生成数据库。
~/.forward文档配置
其实该文档的作用和aliases数据库的作差不多啦,都是配置别名,做邮件转发的。因为alises
只能由管理员控制,个人用户不能修改,所以就能够在个人的目录下建立一个转寄文档。以
配置个人的邮件转寄列表。文档格式如下:
test
test1
test2
test3
and os on
但由于个人用户安全意识差,假如配置不当会有安全漏洞,不建议使用。
access访问控制列表配置
92.168 RELAY
test.NET OK
test.COM REJECT
test.COM 550 SORRY,WE DON'T ALLOW SPAMMERS HERE
test.ORG DISCARD
Ok--远程主机能够向您的邮件服务器发送邮件;
RELAY--允许中转;
REJECT--不能向您的邮件服务器发邮件和不能中转;
DISCARD--发来的邮件将被丢弃,同时并不向发送者返回错误信息。
nnn text--发来的邮件将被丢弃,但sendmail将会向发送者返回nnn确定的smtp代码和text
变量确定的文本描述。
配置完成后要用makemap hash access.db命令生成数据库。
mailq邮件队列查询命令。
Q-ID邮件id号。
Size邮件容量。
Q-Time邮件进入队列(也就是/var/spool/mqueue目录)的时间和不能邮寄的原因。
Sender/Recipient 发信和收信人的邮箱地址。
mailstats邮寄状态查询命令,可查询sendmail运行作至今邮件收发总计资料。
M :
msgsfr:发送的邮件数量。
bytes_from:邮件容量
megsto:收到邮件的数量。
bytes_to:同上
msgsrej:邮件deny的次数。
msgsdis:邮件discard的次数。
Mailer:esmtp对外邮件,local本地邮件。
mail邮件命令
mail 查看/var/spool/mail/目录下自已邮箱内容。以q退出把看过的邮件保存在
~/mbox中。
mail test@example.com 直接发邮件给人。
mail-s'title text' test@example.com把文档中内容邮寄出去。
mail-f~/mbox查看home目录下邮箱内容。
用mail发附件也是可行,要用到uuencode and uudecode命令进行编码。
编码:uuencode [file] name example:uuencode hello hello;hello.uue default input
is stdin;default output is stdout.
解码:uudecode [-o outfile] name example:uudecode hello.uue能够用-o选项输出另外一个
文档名。
# uuencode~/.bashrc bashrc| mail-s'test uuencode'
test@example.com
五、其他配置。
要想更好的使用sendmail,常用到的一些配置:
1、限制最大邮件。
vi/etc/sendmail.cf
# maximum message size
O MaxMessageSize=5000000 (注:5M)
2、最大的群发数目。
vi/etc/sendmail.cf
# maximum number of recipients per SMTP envelope
O MaxRecipientsPerMessage=20 (注:20个)
3、域名文档----local-host-name
能够用他来实现虚拟域名或多域名支持。
/etc/mail/local-host-name
v1mail.com
v2mail.com
.......
4、mail别名文档--aliases。
vi/etc/aliases
系统内部别名:peng:zpeng,hero,mmmn,yt peng是我的用户名,其他的是别名,用逗号隔开。
转发到其他的邮箱:peng:zpeng@sina.com,yt.sohu.com
#newaliases --写到库中
5、邮件控制文档--access
relay、ok、reject和discard。relay能够实现转发。ok是用来允许用户的任意访问,他会覆盖任何其他已建立的检查(实际配置中,最好别设这项,除非您对该用户是绝对信任的);reject能够实现对来访地址的拒绝,他根本就不容许该地址和您的邮件服务器进行连接通信;discard的作用是在接收到传输的邮件消息后,偷偷地把他丢弃掉(在发送者看来,他的邮件的确是接收了,但他并不知道,发送的目的地址根本不可能接收到他的邮件,服务器巧妙地欺骗了他。
vi/etc/mail/access.
localhost.localdomain RELAY ---允许
localhostRELAY
127.0.0.1 RELAY
peng@sina.com ok
@sexgirl.net reject
211.77.22.45 discard
#makemap hash access.db写入库中
6、虚拟用户文档---virtusertable.
这个文档能够mail重名问题。
例如:您有两个域名,virt1.com and virt2.com,但是都有peng这个用户:
peng@virt1.com and peng@virt2.com,这是两个用户。但对于系统用户来说,他只认系统用户peng,只能通过一下改:
vi/etc/mail/virtusertable
peng@virt2.com r010
这样就能够了。在新开一个用户r010,但是peng@virt2.com在客户端的pop3 and smtp server的用户名和口令要用r010的。
#makemap hask virtusertable.db
#/usr/sbin/sendmail-d0.1-bv root|grep SASL
NAMED_BIND NETINET NETINET6 NETUNIX NEWDB NIS QUEUE SASL SCANF
我的问题已找到了是/usr/lib/sasl/Sendmail.conf的文档名中的S没有大写,问题已解决
Linux中创建新分区提示没有可用自由部门怎么回事
Linux中创建新分区提示没有可用自由部门可以用fdisk命令新建分区,方法如下:
1、首先查看硬盘信息,用fdisk-l命令,如果有硬盘有剩余空间就可以对其进行分区。
[root@bogon桌面]# fdisk-l
Disk/dev/sda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders
Units= cylinders of 16065* 512= 8225280 bytes
Sector size(logical/physical): 512 bytes/ 512 bytes
I/O size(minimum/optimal): 512 bytes/ 512 bytes
Disk identifier: 0x000471ad
Device Boot Start End Blocks Id System
/dev/sda1* 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 3851 30720000 83 Linux
/dev/sda3 3851 4106 2048000 82 Linux swap/ Solaris
/dev/sda4 4106 10444 50912256 5 Extended
/dev/sda5 4106 10444 50911232 83 Linux
Disk/dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units= cylinders of 16065* 512= 8225280 bytes
Sector size(logical/physical): 512 bytes/ 512 bytes
I/O size(minimum/optimal): 512 bytes/ 512 bytes
Disk identifier: 0x00000000
#从上面看出/dev/sda空间已经用完了,/dev/sdb没有使用。
2、下面对/dev/sdb进行分区
[root@bogon桌面]# fdisk/dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xfaa2aa49.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode(command'c') and change display units to
sectors(command'u').
Command(m for help): m//输入m查看帮助文档
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition//添加一个新的分区
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality(experts only)
#fdisk选中/dev/sdb输入m所有基本选项都出现,输入n新建分区
Command(m for help): n
Command action
e extended
p primary partition(1-4)
3、有扩展分区和主分区,逻辑分区在扩展分区中建立。注意到括号中的1-4,最多只能建四个主分区(包括扩展分区)。先建一个主分区:
Command(m for help): n
Command action
e extended
p primary partition(1-4)
p#建主分区
Partition number(1-4): 1#分区号为1
First cylinder(1-2610, default 1):#直接回车默认从第一个柱面开始划分
Using default value 1
Last
cylinder,+cylinders or+size{K,M,G}(1-2610, default 2610):+2G
#加空间大小,这里有很多种选择:+后面单位可以接M,G,K(记得要大写)表示划分你所加的空间,也可以是柱面数。不管怎样都不能超过该磁盘剩余的空间否则无效。
Command(m for help): p#分好后查看分区信息,刚所做的所有一目了然。
Disk/dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units= cylinders of 16065* 512= 8225280 bytes
Sector size(logical/physical): 512 bytes/ 512 bytes
I/O size(minimum/optimal): 512 bytes/ 512 bytes
Disk identifier: 0xfaa2aa49
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
4、同上所述建立扩展分区:
Command(m for help): n
Command action
e extended
p primary partition(1-4)
e#建立扩展分区
Partition number(1-4): 4
First cylinder(263-2610, default 263):
Using default value 263
Last cylinder,+cylinders or+size{K,M,G}(263-2610, default 2610):+4G
Command(m for help): p
Disk/dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units= cylinders of 16065* 512= 8225280 bytes
Sector size(logical/physical): 512 bytes/ 512 bytes
I/O size(minimum/optimal): 512 bytes/ 512 bytes
Disk identifier: 0xfaa2aa49
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
5、扩展分区建好就可以在扩展分区建立逻辑分区了
Command(m for help): n
Command action
l logical(5 or over)
p primary partition(1-4)
l#建逻辑分区
First cylinder(263-785, default 263):
Using default value 263
Last cylinder,+cylinders or+size{K,M,G}(263-785, default 785):+2G
Command(m for help): p
Disk/dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units= cylinders of 16065* 512= 8225280 bytes
Sector size(logical/physical): 512 bytes/ 512 bytes
I/O size(minimum/optimal): 512 bytes/ 512 bytes
Disk identifier: 0xfaa2aa49
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
/dev/sdb4 263 785 4200997+ 5 Extended
/dev/sdb5 263 524 2104483+ 83 Linux
6、上面显示已经建好一个主分区,一个逻辑分区,但是这些现在还没有生效,需要保存退出。
Command(m for help): w#保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.