Linux Tips

tree

tree -H localhost > tree.html  #生成html文件,-H 后为服务器地址

制作iso文件

用 mkisofs 命令制作 iso 文件

mkisofs -o file.iso xxxxx/

中文名时会出现乱码,解决方法如下

mkisofs -J -r -o file.iso xxxxx/

服务器序列号

dmidecode -s system-serial-number

序列号转express services code网站
http://www.creativyst.com/Doc/Articles/HT/Dell/DellPop.htm

硬盘使用中不能umount

 
fuser -m /test 
/test:                2241  2331 
kill 进程之后可以umount 

并发数

 
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 

label

 
e2label   /dev/hda3    
#将显示/dev/hda3分区的卷标。    
     
e2lable   /dev/hda3   /DATA    
#把/dev/hda3分区的卷标设为/DATA 

tar

 
tar lcvf - .|(cd /mnt/temp; tar xpvf - )   #全盘备份到/mnt/temp 

dd

创建一个100M的文件

 
dd if=/dev/zero of=file.dat bs=1024k count=100 

SSH

 
解決ssh连接时 unprotected private key file 问题 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
Permissions 0755 for '/home/kakasi/programming/planetlab' are too open. 
It is recommended that your private key files are NOT accessible by others. 
This private key will be ignored. 


原因是我把private key跟public key改过权限 
"chmod 0600 [private key file]" 
就可以了 

iptables

实现:Linux主机做路由,为子网实现共享上网。2个IP,一个公网IP(22.33.44.55),一个内外网关(192.168.0.254)。

  首先打开路由转发,并且为了保证开机重启后生效。如下操作: 
 
    1:修改/etc/sycctl.conf,打开路由转发。 
    [root@kook ~]# vi /etc/sysctl.conf 
    net.ipv4.ip_forward = 1 
    2:立即生效 
    [root@kook ~]# sysctl -p 
    net.ipv4.ip_forward = 1 
    net.ipv4.conf.default.rp_filter = 1 
    net.ipv4.conf.default.accept_source_route = 0 
    kernel.sysrq = 0 
    kernel.core_uses_pid = 1 
    net.ipv4.tcp_syncookies = 1 
    kernel.msgmnb = 65536 
    kernel.msgmax = 65536 
    kernel.shmmax = 4294967295 
    kernel.shmall = 268435456 
    添加iptables的NAT,同样保证开机路由生效。 
    1:添加iptable的NAT 
    [root@kook ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth0 -j MASQUERADE 
    2:修改默认iptables,保障重启开机后仍然生效。 
    [root@kook ~]# iptables-save > /etc/sysconfig/iptables 
    3:让iptables开机启动 
    [root@kook ~]# chkconfig iptables on 
    [root@kook ~]# chkconfig ——list iptables 
    iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off 
    在子网下测试,是否可以上网。完成。 

NFS

yum -y install nfs* 
service portmap start

配置文件/etc/exports内容如下:

          $ cat /etc/exports 
          /home/share 192.168.102.0/24(rw,sync) *(ro) 

配置说明: 对192.168.102.15赋予读写权限,其他机器仅有只读权限。

4 在客户机上查看NFS的资源共享情况

          # showmount -e 192.168.102.47 
          Export list for 192.168.102.47: 
          /home/share (everyone) 

5 使用mount命令即可挂栽共享资源
在客户机 192.168.102.15 上加载共享资源

          # mount 192.168.102.47:/home/share /mnt 

6 一些NFS共享的常用参数:

          ro                      只读访问 
          rw                      读写访问 
          sync                    所有数据在请求时写入共享 
          async                   NFS在写入数据前可以相应请求 
          secure                  NFS通过1024以下的安全TCP/IP端口发送 
          insecure                NFS通过1024以上的端口发送 
          wdelay                  如果多个用户要写入NFS目录,则归组写入(默认) 
          no_wdelay               如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。 
          hide                    在NFS共享目录中不共享其子目录 
          no_hide                 共享NFS目录的子目录 
          subtree_check           如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认) 
          no_subtree_check        和上面相对,不检查父目录权限 
          all_squash              共享文件的UID和GID映射匿名用户anonymous,适合公用目录。 
          no_all_squash           保留共享文件的UID和GID(默认) 
          root_squash             root用户的所有请求映射成如anonymous用户一样的权限(默认) 
          no_root_squash          root用户具有根目录的完全管理访问权限 
          anonuid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的UID 
          anongid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的GID 

SSH密钥

 
$ ssh-keygen -t rsa   (su到想要生成密钥的用户) 
Generating public/private rsa key pair. 
Enter file in which to save the key (/root/.ssh/id_rsa):  <-密钥路径 
Enter passphrase (empty for no passphrase): <-密钥保护密码 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa. <-私钥 
Your public key has been saved in /root/.ssh/id_rsa.pub. <-公钥 
The key fingerprint is: 
17:28:4c:c3:e4:18:d4:c2:31:bd:be:a7:a9:d4:a8:48 root@yepn.net 

公有密钥写入验证文件

 
cat id_rsa.pub >> authorized_keys 

修改sshd_config配件文件如下

 
Port                            22 
Protocol                        2 
HostKey                         /etc/ssh/ssh_host_rsa_key 
HostKey                         /etc/ssh/ssh_host_dsa_key 
KeyRegenerationInterval         1h 
SyslogFacility                  AUTHPRIV 
LoginGraceTime                  5m 
PermitRootLogin                 yes 
PubkeyAuthentication            yes   #使用密钥验证 
AuthorizedKeysFile              .ssh/authorized_keys  #验证文件 
IgnoreRhosts                    yes 
HostbasedAuthentication         no 
PasswordAuthentication          yes   #如果只想使用密钥进行ssh登陆的话,把yes改为no 
PermitEmptyPasswords            no 
ChallengeResponseAuthentication no 
Subsystem        sftp    /usr/libexec/openssh/sftp-server 

修改后重启ssh服务

小技巧

 
查看WEB主页运行的服务 
w3m -dump_head http://www.linuxfans.org 
 
取消mput询问,关闭交互方式 

prompt  

prompt off 

删除含有特殊字符的文件

 
ls -i filename #显示文件节点号 

find ./ -inum 节点号 -print -exec rm {} -rf \;  

安装EPEL

 
RHEL 4: su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/4/i386/epel-release-4-9.noarch.rpm' 
RHEL 5: su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm' 

安装RPMforge

 
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt 
rpm -Uvh http://apt.sw.be/redhat/el5/en/i386/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.i386.rpm 
rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm 

selinux设置

 
#setenforce 0  #修改后不重启生效 

#cacti设置时需要关闭selinux 

制作可引导光盘

 
mkisofs -J -R -V -v -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table \ 
-o /home/admin/Centos-5.2.iso /home/admin/minicd 

忘记root密码解决办法

1. 用RedHat标准安装盘启动系统
当出现提示符时敲入:linux rescue
就会以修复模式启动系统

2. 系统启动完毕后,会将硬盘mount到/mnt/sysimage,届时会有提示

3. 进入提示符后,敲入命令:

 
chroot /mnt/sysimage 

4. 敲入命令:

 
passwd 

更改密码

5. 连续敲入两次exit,即重起系统,将光盘拿出,按正常模式启动系统

除了用光盘引导rescue模式,还有一种方法是用启动装载器来进入单用户模式:

1. grub

进入启动画面之后,敲入“e”,把光标移动到kernel …那一行,再敲入“e”,在kernel 一行的最后加上空格single,回车
敲入“b”,启动系统,即进入单用户模式,
这个时候就可以用passwd命令改密码了。

2. lilo

进入lilo命令行模式,敲入linux single,即进入单用户模式。
passwd …

如果grub/lilo设了密码,一定要记住才行。要是忘了grub/lilo的密码,就只有用rescue模式了。
over

小技巧总结

 
/usr/sbin/lsof -i :110    #查看什么程序运行在110端口下 
 
dmidecode -t memory  #查看内存 
 
cat /proc/cpuinfo    #查看CPU 
 
hdparm -cdtT /dev/sda  #硬盘读写速度 
hdparm -i /dev/hda   #硬盘序列号,SCSI无效 
 
sdparm --quiet --page=sn /dev/sda  #SCSI硬盘序列号,sdparm需要安装 
                                   #可通过rpmforge源,yum方式安装 
 
unalias cp 
-f 强制复制生效 
cp -f 
 
dos2unix filename.ldif 
iconv -f gb2312 -t UTF-8 -o filename.ldif.utf8 filename.ldif 
#Linux转码功能,可以让openldap支持中文导入 
 
grep -n "^$" filename #显示空行行号 
 
find . -name "whatever.*" -print0 | rsync -av --files-from=- --from0 ./ remotehost:/destination/ 
#查找指定文件,rsync到远程服务器,并创建递归目录 
# -e "/usr/bin/ssh -oNoneSwitch=yes" 禁用数据加密验证,需要重新编译ssh支持 

find lb06* -name "*DATE*_*" -print0 |rsync -av -e "/usr/bin/ssh -oNoneSwitch=yes" ¥ 
--files-from=- --from0 ./ remotehost:/destination/ 

 find lb* -name adlantis_sp -print0 -exec cp -aRv {} --parents --target-directory=/mnt/usb060b/ \; 

新建用户

#!/bin/bash 
 
useradd $1 
echo mypassword | passwd --stdin $1 
 
# Invocation ./myscript.sh frank

crontab

 
第一道第五个字段的整数取值范围及意义是: 

0~59 表示分 

1~23 表示小时 

1~31 表示日 

1~12 表示月份 

0~6 表示星期(其中0表示星期日)  

下记为使用例 

#每天早上6点  
106* * * date  

#每两个小时  
0*/2* * * date  

#晚上11点到早上8点之间每两个小时,早上部点  
0 23-7/2,8* * * date  

#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点  
0 11 4* mon-wed date  

#1月份日早上4点  
0 4 1 jan* date   

ping

 
ping -c 10 127.0.0.1 

发送10个数据包到127.0.0.1等结果 

rsync

 
/usr/bin/rsync -vzrtopg --progress --delete --exclude-from=/etc/rsyncd.exclude  
 --password-file=/etc/rsyncd.pwd admin@172.30.2.222::home /home 

#此命令为一行 

rsync参数说明

选项说明

-v, –verbose 详细模式输出
-q, –quiet 精简输出模式
-c, –checksum 打开校验开关,强制对文件传输进行校验
-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, –recursive 对子目录以递归模式处理
-R, –relative 使用相对路径信息

rsync foo/bar/foo.c remote:/tmp/

则在/tmp目录下创建foo.c文件,而如果使用-R参数:

rsync -R foo/bar/foo.c remote:/tmp/

则会创建文件/tmp/foo/bar/foo.c,也就是会保持完全路径信息。

-b, –backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用–suffix选项来指定不同的备份文件前缀。
–backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, –update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, –links 保留软链结
-L, –copy-links 想对待常规文件一样处理软链结
–copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
–safe-links 忽略指向SRC路径目录树以外的链结
-H, –hard-links 保留硬链结
-p, –perms 保持文件权限
-o, –owner 保持文件属主信息
-g, –group 保持文件属组信息
-D, –devices 保持设备文件信息
-t, –times 保持文件时间信息
-S, –sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, –dry-run现实哪些文件将被传输
-W, –whole-file 拷贝文件,不进行增量检测
-x, –one-file-system 不要跨越文件系统边界
-B, –block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, –rsh=COMMAND 指定替代rsh的shell程序
–rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, –cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
–existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
–delete 删除那些DST中SRC没有的文件
–delete-excluded 同样删除接收端那些被该选项指定排除的文件
–delete-after 传输结束以后再删除
–ignore-errors 及时出现IO错误也进行删除
–max-delete=NUM 最多删除NUM个文件
–partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
–force 强制删除目录,即使不为空
–numeric-ids 不将数字的用户和组ID匹配为用户名和组名
–timeout=TIME IP超时时间,单位为秒
-I, –ignore-times 不跳过那些有同样的时间和长度的文件
–size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
–modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T –temp-dir=DIR 在DIR中创建临时文件
–compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 –partial
–progress 显示备份过程
-z, –compress 对备份的文件在传输时进行压缩处理
–exclude=PATTERN 指定排除不需要传输的文件模式
–include=PATTERN 指定不排除而需要传输的文件模式
–exclude-from=FILE 排除FILE中指定模式的文件
–include-from=FILE 不排除FILE指定模式匹配的文件
–version 打印版本信息
–address 绑定到特定的地址
–config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
–port=PORT 指定其他的rsync服务端口
–blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
–progress 在传输时现实传输过程
–log-format=FORMAT 指定日志文件格式
–password-file=FILE 从FILE中得到密码
–bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, –help 显示帮助信息

制作RPM安装包

rpm安装包的spec文件例子:rpmtest.spec

 
Summary: example 
Name: rpmtest 
Version: 0.1 
Release: 1 
Group: Application/User 
License: Share 
Packager: rpmtest 
%description 
#rpmtest example 
%changelog 
* Sun Jun 6 2004 CSP 
- build for the first time. 
%prep 
%build 
%install 
%files 
/usr/lib/rpmtest.out 

生成spec文件后,在root的用户下使用rpmbuild -bb rpmtest.spec生成rpm安装包rpmtest-0.1-1.i386.rpm。

通过rpm -i –test rpmtest-0.1-1.i386.rpm测试程序是否可以安装。

通过rpm -ivh rpmtest-0.1-1.i386.rpm安装程序

通过rpm -e rpmtest-0.1-1卸载程序

grep

 
grep 'devpts \| sysfs' /etc/fstab #显示两个关键字 
/home/yepnnet/public_html/wiki/data/pages/linux.txt · 最后更改: 2010/02/24 18:27 由 admin
到顶部
chimeric.de = chi`s home Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0