Saturday, November 7, 2015

利用arch配置一个pxe server,网络安装各种版本linux系统,各种版本windows系统!

想了半天也没有找到合适的板块发这个,最后还是发这里把!

最近折腾了两个小主机,华硕EeeBox B202,atom n270的u,1g的内存,80g的硬盘,机器挺小巧,就是单核u,功能实在鸡肋,索性一个做了个播放机,这个就做了一个pxe server!

利用wol的功能,网络装系统,至于pxe的原理,如下:


以下是摘自syslinux官网关于pxelinux的介绍,懒得翻译了,大家自己看,应该不难理解.
The basic configuration is the same for all Syslinux variants. This document explains only some of the differences specifically applicable to PXELINUX.
On the TFTP server, create the directory "/tftpboot", and copy "pxelinux.0" (from the Syslinux distribution) and any kernel or initrd images that you want to boot.
[5.00+] Also copy "ldlinux.c32" from the Syslinux distribution to the "/tftpboot" directory on the TFTP server.
Finally, create the directory "/tftpboot/pxelinux.cfg". The configuration file (equivalent of syslinux.cfg -- see the SYSLINUX FAQ for the options here) will live in this

directory.
Because more than one system may be booted from the same server, the configuration file name depends on the IP address of the booting machine.
Before a generic explanation, let's see first an example. When:
the bootloader file name is "/mybootdir/pxelinux.0"; and,
the client UUID is "b8945908-d6a6-41a9-611d-74a6ab80b83d"; and,
the Ethernet MAC address is "88:99:AA:BB:CC:DD"; and,
the IP address is "192.168.2.91", or in uppercase hexadecimal, "C0A8025B";
then PXELINUX will try the following configuration files (in this order):
/mybootdir/pxelinux.cfg/b8945908-d6a6-41a9-611d-74a6ab80b83d
/mybootdir/pxelinux.cfg/01-88-99-aa-bb-cc-dd
/mybootdir/pxelinux.cfg/C0A8025B
/mybootdir/pxelinux.cfg/C0A8025
/mybootdir/pxelinux.cfg/C0A802
/mybootdir/pxelinux.cfg/C0A80
/mybootdir/pxelinux.cfg/C0A8
/mybootdir/pxelinux.cfg/C0A
/mybootdir/pxelinux.cfg/C0
/mybootdir/pxelinux.cfg/C
/mybootdir/pxelinux.cfg/default
Let's see now what exactly the above example represents.
After attempting the file as specified in the DHCP or hardcoded options, PXELINUX will probe the following paths, prefixed with "pxelinux.cfg/", under the initial Working

Directory.
The client UUID, if provided by the PXE stack.
Note that some BIOSes do not have a valid UUID, and it might end up reporting something like all 1's.
This value is represented in the standard UUID format using lowercase hexadecimal digits, e.g. "b8945908-d6a6-41a9-611d-74a6ab80b83d".
The hardware type (using its ARP type code) and address, all in lowercase hexadecimal with dash separators.
For example, for an Ethernet (ARP type "1") with address "88:99:AA:BB:CC:DD", it would search for the filename "01-88-99-aa-bb-cc-dd".
The client's own IPv4 address in uppercase hexadecimal, followed by removing hex characters, one at a time, from the end. For example, "192.168.2.91" → "C0A8025B".
The included program, "gethostip", can be used to compute the hexadecimal IP address for any host.
Lowercase "default".


1:首先,需要的工具包有 dnsmasq,samba, nfs-utils,openssh,syslinux

dnsmasq   =    充当dhcp和tftpserver
samba      =    安装windows使用,另外也为了方便windows机器管理
nfs-utils    =    加载linux镜像用
openssh    =    管理用,ssh登录很方便,毕竟这台机器是个小的服务器吧
syslinux     =   启动pxelinux以及各种启动程序

arch下很简单,一条命令

pacman -S dnsmasq samba nfs-utils openssh syslinux

2:然后就是配置dhcp和tftp server,以及nfs 和 ssh server了


直接贴配置文件比较省事
dnsmasq为了配置方便,我在/etc/dnsmasq.d/重建了一个pxe.conf的配置文件,配置如下

记住要把/etc/dnsmasq.conf里面下面这句话取消注释


# Include another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
conf-dir=/etc/dnsmasq.d


#     function as a tftp server 启动tftpserver
enable-tftp
#     root directory for files available via ftp. ftp的root环境
tftp-root=/tftpboot
#     the boot filename
#pxe-service=X86PC,"PXE Boot Menu",pxelinux 启动文件
dhcp-boot=/tftpboot/pxelinux.0
#     kill multicast
#dhcp-option=vendor:PXEClient,6,2b
#     disable reuse of the dhcp servername and filename fields as extra.
#     option space. to avoid confusing some old or broken dhcp clients.
dhcp-no-override
#     This range(s) is for the public interface, where dnsmasq functions
#     as a proxy DHCP server providing boot information but no IP leases.
#     Any ip in the subnet will do, so you may just put your server NIC ip here.
#     I used my router ip address
#dhcp-range=192.168.2.1,proxy,255.255.255.0

#dhcp-range=192.168.2.100,proxy
# do not give IPs that are in pool of DSL routers DHCP 我避开了家里路由器的dhcp缺省池
dhcp-range=192.168.2.151,192.168.2.200,12h
#     router/gateway ip address
dhcp-option=3,192.168.2.1
#     dns server ip address
dhcp-option=6,192.168.2.1
#     enable logging
log-dhcp
log-queries


samba的配置文件smb.conf
在windows下直接在文件浏览器中打入\\192.168.2.19,就可以打开tftpboot文件夹,直接管理里面的文件


# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options (perhaps too
# many!) most of which are not shown in this example
#
# Any line which starts with a ; (semi-colon) or a # (hash)
# is a comment and is ignored. In this example we will use a #
# for commentry and a ; for parts of the config file that you
# may wish to enable
#
# NOTE: Whenever you modify this file you should run the command "testparm"
# to check that you have not made any basic syntactic errors.
#
#======================= Global Settings =====================================
[global]

# workgroup = NT-Domain-Name or Workgroup-Name
   workgroup = Workgroup

# server string is the equivalent of the NT Description field
   server string = Samba Server

# This option is important for security. It allows you to restrict
# connections to machines which are on your local network. The
# following example restricts access to two C class networks and
# the "loopback" interface. For more examples of the syntax see
# the smb.conf man page
;   hosts allow = 192.168.1. 192.168.2. 127.

# if you want to automatically load your printer list rather
# than setting them up individually then you'll need this
   printcap name = /etc/printcap
   load printers = yes

# It should not be necessary to spell out the print system type unless
# yours is non-standard. Currently supported print systems include:
# bsd, sysv, plp, lprng, aix, hpux, qnx
;   printing = bsd

# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
;  guest account = pcguest

# this tells Samba to use a separate log file for each machine
# that connects
   log file = /var/log/samba/%m.log

# Put a capping on the size of the log files (in Kb).
   max log size = 50

# Security mode. Most people will want user level security. See
# security_level.txt for details.
   security = user
#   security = share
# Use password server option only with security = server
;   password server =

# Password Level allows matching of _n_ characters of the password for
# all combinations of upper and lower case.
;  username level = 8

# You may wish to use password encryption. Please read
# ENCRYPTION.txt, Win95.txt and WinNT.txt in the Samba documentation.
# Do not enable this option unless you have read those documents
;  encrypt passwords = yes
;  smb passwd file = /etc/samba/smbpasswd

# The following are needed to allow password changing from Windows to
# update the Linux sytsem password also.
# NOTE: Use these with 'encrypt passwords' and 'smb passwd file' above.
# NOTE2: You do NOT need these to allow workstations to change only
#        the encrypted SMB passwords. They allow the Unix password
#        to be kept in sync with the SMB password.
;  unix password sync = Yes
;  passwd program = /usr/bin/passwd %u
;  passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n *passwd:*all*authentication*tokens*updated*successfully*

# Unix users can map to different SMB User names
;  username map = /etc/samba/smbusers
# 开启 windows guest登录
   map to guest = Bad User

# Using the following line enables you to customise your configuration
# on a per machine basis. The %m gets replaced with the netbios name
# of the machine that is connecting
;   include = /etc/samba/smb.conf.%m

# Configure Samba to use multiple interfaces
# If you have multiple network interfaces then you must list them
# here. See the man page for details.
;   interfaces = 192.168.12.2/24 192.168.13.2/24

# Configure remote browse list synchronisation here
#  request announcement to, or browse list sync from:
# a specific host or from / to a whole subnet (see below)
;   remote browse sync = 192.168.3.25 192.168.5.255
# Cause this host to announce itself to local subnets here
;   remote announce = 192.168.1.255 192.168.2.44

# Browser Control Options:
# set local master to no if you don't want Samba to become a master
# browser on your network. Otherwise the normal election rules apply
;   local master = no

# OS Level determines the precedence of this server in master browser
# elections. The default value should be reasonable
;   os level = 33

# Domain Master specifies Samba to be the Domain Master Browser. This
# allows Samba to collate browse lists between subnets. Don't use this
# if you already have a Windows NT domain controller doing this job
;   domain master = yes
    domain master = auto

# Preferred Master causes Samba to force a local browser election on startup
# and gives it a slightly higher chance of winning the election
;   preferred master = yes

# Use only if you have an NT server on your network that has been
# configured at install time to be a primary domain controller.
;   domain controller =

# Enable this if you want Samba to be a domain logon server for
# Windows95 workstations.
;   domain logons = yes

# if you enable domain logons then you may want a per-machine or
# per user logon script
# run a specific logon batch file per workstation (machine)
;   logon script = %m.bat
# run a specific logon batch file per username
;   logon script = %U.bat

# Where to store roving profiles (only for Win95 and WinNT)
#        %L substitutes for this servers netbios name, %U is username
#        You must uncomment the [Profiles] share below
;   logon path = \\%L\Profiles\%U

# All NetBIOS names must be resolved to IP Addresses
# 'Name Resolve Order' allows the named resolution mechanism to be specified
# the default order is "host lmhosts wins bcast". "host" means use the unix
# system gethostbyname() function call that will use either /etc/hosts OR
# DNS or NIS depending on the settings of /etc/host.config, /etc/nsswitch.conf
# and the /etc/resolv.conf file. "host" therefore is system configuration
# dependant. This parameter is most often of use to prevent DNS lookups
# in order to resolve NetBIOS names to IP Addresses. Use with care!
# The example below excludes use of name resolution for machines that are NOT
# on the local network segment
# - OR - are not deliberately to be known via lmhosts or via WINS.
; name resolve order = wins lmhosts bcast

# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
;   wins support = yes

# WINS Server - Tells the NMBD components of Samba to be a WINS Client
# Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
;   wins server = w.x.y.z

# WINS Proxy - Tells Samba to answer name resolution queries on
# behalf of a non WINS capable client, for this to work there must be
# at least one WINS Server on the network. The default is NO.
;   wins proxy = yes

# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# via DNS nslookups. The built-in default for versions 1.9.17 is yes,
# this has been changed in version 1.9.18 to no.
   dns proxy = no

# Case Preservation can be handy - system default is _no_
# NOTE: These can be set on a per share basis
;  preserve case = no
;  short preserve case = no
# Default case is normally upper case for all DOS files
;  default case = lower
# Be very careful with case sensitivity - it can break things!
;  case sensitive = no

#============================ Share Definitions ==============================
[homes]
   comment = Home Directories
   browseable = no
   writable = yes

# Un-comment the following and create the netlogon directory for Domain Logons
; [netlogon]
;   comment = Network Logon Service
;   path = /home/netlogon
;   guest ok = yes
;   writable = no
;   share modes = no


# Un-comment the following to provide a specific roving profile share
# the default is to use the user's home directory
;[Profiles]
;    path = /home/profiles
;    browseable = no
;    guest ok = yes


# NOTE: If you have a BSD-style print system there is no need to
# specifically define each individual printer
[printers]
   comment = All Printers
   path = /var/spool/samba
   browseable = no
# Set public = yes to allow user 'guest account' to print
   guest ok = no
   writable = no
   printable = yes

# This one is useful for people to share files
;[tmp]
;   comment = Temporary file space
;   path = /tmp
;   read only = no
;   public = yes

# A publicly accessible directory, but read only, except for people in
# the "staff" group
;[public]
;   comment = Public Stuff
;   path = /home/samba
;   public = yes
;   read only = yes
;   write list = @staff

# Other examples.
#
# A private printer, usable only by fred. Spool data will be placed in fred's
# home directory. Note that fred must have write access to the spool directory,
# wherever it is.
;[fredsprn]
;   comment = Fred's Printer
;   valid users = fred
;   path = /homes/fred
;   printer = freds_printer
;   public = no
;   writable = no
;   printable = yes

# A private directory, usable only by fred. Note that fred requires write
# access to the directory.
;[fredsdir]
;   comment = Fred's Service
;   path = /usr/somewhere/private
;   valid users = fred
;   public = no
;   writable = yes
;   printable = no

# a service which has a different directory for each machine that connects
# this allows you to tailor configurations to incoming machines. You could
# also use the %u option to tailor it by user name.
# The %m gets replaced with the machine name that is connecting.
;[pchome]
;  comment = PC Directories
;  path = /usr/pc/%m
;  public = no
;  writable = yes

# A publicly accessible directory, read/write to all users. Note that all files
# created in the directory by users will be owned by the default user, so
# any user with access can delete any other user's files. Obviously this
# directory must be writable by the default user. Another user could of course
# be specified, in which case all files would be owned by that user instead.
;[public]
;   path = /usr/somewhere/else/public
;   public = yes
;   only guest = yes
;   writable = yes
;   printable = no

# The following two entries demonstrate how to share a directory so that two
# users can place files there that will be owned by the specific users. In this
# setup, the directory should be writable by both users and should have the
# sticky bit set on it to prevent abuse. Obviously this could be extended to
# as many users as required.
;[myshare]
;   comment = Mary's and Fred's stuff
;   path = /usr/somewhere/shared
;   valid users = mary fred
;   public = no
;   writable = yes
;   printable = no
;   create mask = 0765

[tftpboot]
    comment = TFTP + PXE
    path = /tftpboot/
    public = yes
    only guest = yes
    browseable = yes
    read only = no
    writable = yes
    create mask = 0644
    directory mask = 0755
#    force create mask = 0644
#    force directory mask = 0755
    force user = root
    force group = root





nfs的配置文件 exports


# /etc/exports - exports(5) - directories exported to NFS clients
#
# Example for NFSv2 and NFSv3:
#  /srv/home        hostname1(rw,sync) hostname2(ro,sync)
# Example for NFSv4:
#  /srv/nfs4    hostname1(rw,sync,fsid=0)
#  /srv/nfs4/home   hostname1(rw,sync,nohide)
# Using Kerberos and integrity checking:
#  /srv/nfs4        *(rw,sync,sec=krb5i,fsid=0)
#  /srv/nfs4/home   *(rw,sync,sec=krb5i,nohide)
#
# Use `exportfs -arv` to reload.
#/tftpboot/images/ARCH_201510/ 192.168.2.0/24(ro,no_subtree_check)
/tftpboot/images/ARCH_201510/  *(ro,no_subtree_check,async,no_root_squash)
/tftpboot/images/kali2/         *(ro,no_subtree_check,async,no_root_squash)


3:接着配置tftpboot主目录

makdir /tftpboot/{images,iso,syslinux,pxelinux.cfg}

其中images文件夹是挂载的linux安装镜像和windows安装镜像

iso文件夹里是我各种安装iso文件的收集地点

syslinux文件夹里是syslinux各种模块的收藏地点

pxelinux.cfg目录里存放着pxelinux的启动配置文件,也就是目录文件

然后把各种pxelinux启动相关的文件,拷贝入tftpboot目录,和syslinux目录


cp /usr/lib/syslinux/bios/pxelinux.0  /tftpboot/
cp   /usr/lib/syslinux/bios/{chain.c32,hdt.c32,ldlinux.c32,libcom32.c32,libgpl.c32,libmenu.c32,libutil.c32,poweroff.c32,reboot.c32,vesamenu.c32} /tftpboot/syslinux/

其中
pxelinux.0是启动文件,hdt是用来显示机器的各种配置的,chain.c32是用来启动本地硬盘的,lib文件都是各种库,poweroff和reboot大家都懂的.

最后就是/tftpboot/pxelinux.cfg/default配置文件的内容了,这个内容是根据我的机器挂载的镜像修改的



UI syslinux/vesamenu.c32
TIMEOUT 600
ONTIMEOUT Boot Local
PROMPT 0
NOESCAPE 1
ALLOWOPTIONS 1

menu hshift 5
menu width 59

menu title pxe boot menu

menu color title        * #ffffffff *
menu color border       * #00000000 #00000000 none
menu color sel          * #ffffffff #76a1d0ff *
menu color hotsel       1;7;37;40 #ffffffff #76a1d0ff *
menu color tabmsg       * #ffffffff #00000000 *
menu color help         37;40 #ffdddd00 #00000000 none
menu vshift 2
menu rows 20
menu helpmsgrow 25
# the command line must be at least one line from the bottom.
menu cmdlinerow 26
menu timeoutrow 26
menu tabmsgrow 28
menu tabmsg press enter to boot or tab to edit a menu entry


label boot local
    COM32 syslinux\chain.c32
    APPEND hd0
    text help
        boot to local hard disk
    endtext

label winpe3 32 (cn)
    kernel syslinux/memdisk
    append iso raw
    initrd iso/winpe_x86.iso
    text help
        boot to windows pe 3 32bit iso (~184mb)
    endtext

label winpe3 64 (cn)
    MENU LABEL Autoinstall win10-amd64
    kernel syslinux/memdisk
    append iso raw
    initrd iso/winpe_amd64.iso
    text help
        boot to windows pe 3 64bit iso (~207mb)
        autoinstall win10 64
    endtext

LABEL Arch32_(NFS)
    TEXT HELP
        Boot the Arch Linux (i686) live medium (Using NFS).
    It allows you to install Arch Linux or perform system maintenance.
    ENDTEXT
    MENU LABEL Install Arch Linux (i686) (NFS)
    LINUX images/ARCH_201510/arch/boot/i686/vmlinuz
    INITRD images/ARCH_201510/arch/boot/intel_ucode.img,images/ARCH_201510/arch/boot/i686/archiso.img
    APPEND archisobasedir=arch archiso_nfs_srv=192.168.2.19:/tftpboot/images/ARCH_201510 ip=:::::eth0:dhcp

LABEL Arch64 (NFS)
    TEXT HELP
        Boot the Arch Linux (x86_64) live medium (Using NFS).
    It allows you to install Arch Linux or perform system maintenance.
    ENDTEXT
    MENU LABEL Install Arch Linux (x86_64) (NFS)
    LINUX images/ARCH_201510/arch/boot/x86_64/vmlinuz
    INITRD images/ARCH_201510/arch/boot/intel_ucode.img,images/ARCH_201510/arch/boot/x86_64/archiso.img
    APPEND archisobasedir=arch archiso_nfs_srv=192.168.2.19:/tftpboot/images/ARCH_201510 ip=:::::eth0:dhcp

LABEL kali2 live(amd64)
    TEXT HELP
        Boot to Kali 2.0 (sana) amd64 Live
    ENDTEXT
    MENU LABEL Kali 2.0 Live(amd64)
    KERNEL images/kali2/live/vmlinuz
    INITRD images/kali2/live/initrd.img
    APPEND netboot=nfs nfsroot=192.168.2.19:/tftpboot/images/kali2 boot=live username=root hostname=kali

# http://www.memtest.org/
LABEL memtest
MENU LABEL Run Memtest86+ (RAM test)
LINUX memtest

# http://hdt-project.org/
LABEL hdt
MENU LABEL Hardware Information (HDT)
COM32 syslinux/hdt.c32
APPEND modules_alias=syslinux/hdt/modalias.gz pciids=syslinux/hdt/pciids.gz

LABEL reboot
MENU LABEL Reboot
COM32 syslinux/reboot.c32

LABEL poweroff
MENU LABEL Power Off
COM32 syslinux/poweroff.c32


期间还自己学会了自制winpe,我做了2个版本的winpe,一个32位,一个64位.


最后,只要把要安装的机器修改为网卡启动,就可以进入我配置的pxe server了

如下图


Sunday, November 13, 2011

让你提升命令行效率的 Bash 快捷键 [完整版]

让你提升命令行效率的 Bash 快捷键 [完整版]:



生活在 Bash shell
中,熟记以下快捷键,将极大的提高你的命令行操作效率。



编辑命令




  • Ctrl + a :移到命令行首

  • Ctrl + e :移到命令行尾

  • Ctrl + f :按字符前移(右向)

  • Ctrl + b :按字符后移(左向)

  • Alt + f :按单词前移(右向)

  • Alt + b :按单词后移(左向)

  • Ctrl + xx:在命令行首和光标之间移动

  • Ctrl + u :从光标处删除至命令行首

  • Ctrl + k :从光标处删除至命令行尾

  • Ctrl + w :从光标处删除至字首

  • Alt + d :从光标处删除至字尾

  • Ctrl + d :删除光标处的字符

  • Ctrl + h :删除光标前的字符

  • Ctrl + y :粘贴至光标后

  • Alt + c :从光标处更改为首字母大写的单词

  • Alt + u :从光标处更改为全部大写的单词

  • Alt + l :从光标处更改为全部小写的单词

  • Ctrl + t :交换光标处和之前的字符

  • Alt + t :交换光标处和之前的单词

  • Alt + Backspace:与 Ctrl + w 相同



重新执行命令




  • Ctrl + r:逆向搜索命令历史

  • Ctrl + g:从历史搜索模式退出

  • Ctrl + p:历史中的上一条命令

  • Ctrl + n:历史中的下一条命令

  • Alt + .:使用上一条命令的最后一个参数



控制命令




  • Ctrl + l:清屏

  • Ctrl + o:执行当前命令,并选择上一条命令

  • Ctrl + s:阻止屏幕输出

  • Ctrl + q:允许屏幕输出

  • Ctrl + c:终止命令

  • Ctrl + z:挂起命令



Bang (!) 命令




  • !!:执行上一条命令

  • !blah:执行最近的以 blah 开头的命令,如 !ls

  • !blah:p:仅打印输出,而不执行

  • !$:上一条命令的最后一个参数,与 Alt + . 相同

  • !$:p:打印输出 !$ 的内容

  • !*:上一条命令的所有参数

  • !*:p:打印输出 !* 的内容

  • ^blah:删除上一条命令中的 blah

  • ^blah^foo:将上一条命令中的 blah 替换为 foo

  • ^blah^foo^:将上一条命令中所有的 blah 都替换为 foo



友情提示:




  1. 以上介绍的大多数 Bash 快捷键仅当在 emacs 编辑模式时有效,若你将 Bash
    配置为 vi 编辑模式,那将遵循 vi 的按键绑定。Bash 默认为 emacs
    编辑模式。如果你的 Bash 不在 emacs 编辑模式,可通过 set -o emacs 设置。

  2. ^S、^Q、^C、^Z 是由终端设备处理的,可用 stty 命令设置。

Monday, November 7, 2011

linux中查看系统资源占用情况的命令

top:
主要参数
d:指定更新的间隔,以秒计算。
q:没有任何延迟的更新。如果使用者有超级用户,则top命令将会以最高的优先序执行。
c:显示进程完整的路径与名称。
S:累积模式,会将己完成或消失的子行程的CPU时间累积起来。
s:安全模式。
i:不显示任何闲置(Idle)或无用(Zombie)的行程。
n:显示更新的次数,完成后将会退出to
显示参数:
PID(Process ID):进程标示号。
USER:进程所有者的用户名。
PR:进程的优先级别。
NI:进程的优先级别数值。
VIRT:进程占用的虚拟内存值。
RES:进程占用的物理内存值。
SHR:进程使用的共享内存值。
S:进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。
%CPU:该进程占用的CPU使用率。
%MEM:该进程占用的物理内存和总内存的百分比。
TIME+:该进程启动后占用的总的CPU时间。
Command:进程启动的启动命令名称,如果这一行显示不下,进程会有一个完整的命令行。
top命令使用过程中,还可以使用一些交互的命令来完成其它参数的功能。这些命令是通过快捷键启动的。
<空格>:立刻刷新。
P:根据CPU使用大小进行排序。
T:根据时间、累计时间排序。
q:退出top命令。
m:切换显示内存信息。
t:切换显示进程和CPU状态信息。
c:切换显示命令名称和完整命令行。
M:根据使用内存大小进行排序。
W:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。

free
1.作用
free命令用来显示内存的使用情况,使用权限是所有用户。

2.格式
free [-b-k-m] [-o] [-s delay] [-t] [-V]

3.主要参数
-b -k -m:分别以字节(KB、MB)为单位显示内存使用情况。
-s delay:显示每隔多少秒数来显示一次内存使用情况。
-t:显示内存总和列。
-o:不显示缓冲区调节列。

uptime
18:59:15 up 25 min,  2 users,  load average: 1.23, 1.32, 1.21
现在的时间
系统开机运转到现在经过的时间
连线的使用者数量
最近一分钟,五分钟和十五分钟的系统负载
参数: -V 显示版本资讯。

vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r  b   swpd free   buff   cache    si   so    bi     bo    in    cs   us  sy  id wa
0  1  24980 10792 8296  47316    5   19   205    52  1161   698  26  3  1  70
1 观察磁盘活动情况
磁盘活动情况主要从以下几个指标了解:
bi:表示从磁盘每秒读取的块数(blocks/s)。数字越大,表示读磁盘的活动越多。
bo:表示每秒写到磁盘的块数(blocks/s)。数字越大,表示写磁盘的活动越多。
wa:cpu等待磁盘I/O(未决的磁盘IO)的时间比例。数字越大,表示文件系统活动阻碍cpu的情况越严重,因为cpu在等待慢速的磁盘系统提供数据。wa为0是最理想的。如果wa经常大于10,可能文件系统就需要进行性能调整了。
2 观察cpu活动情况
vmstat比top更能反映出cpu的使用情况:
us:用户程序使用cpu的时间比例。这个数字越大,表示用户进程越繁忙。
sy: 系统调用使用cpu的时间比例。注意,NFS由于是在内核里面运行的,所以NFS活动所占用的cpu时间反映在sy里面。这个数字经常很大的话,就需要注 意是否某个内核进程,比如NFS任务比较繁重。如果us和sy同时都比较大的话,就需要考虑将某些用户程序分离到另外的服务器上面,以免互相影响。
id:cpu空闲的时间比例。
wa:cpu等待未决的磁盘IO的时间比例。

iostat
用于统计CPU的使用情况及tty设备、硬盘和CD-ROM的I/0量
参数:
    -c      只显示CPU行
    -d      显示磁盘行
    -k      以千字节为单位显示磁盘输出
    -t       在输出中包括时间戳
    -x      在输出中包括扩展的磁盘指标

avg-cpu:  %user   %nice    %sys %iowait   %idle
          20.25    0.18    2.61   76.39    0.57
%iowait 等待本地I/O时CPU空闲时间的百分比
%idle 未等待本地I/O时CPU空闲时间的百分比

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
hda               9.86       284.34        84.48     685407     2036
每秒传输数(tps)、每秒512字节块读取数(Blk_read/s)、每秒512字节块写入数(Blk_wrtn/s)和512字节块读取(Blk_read)和写入(Blk_wrtn)的总数量。

一、如何查看服务器的CPU
今天安装了9台Linux服务器,型号完全不一样(有DELL、HP和IBM服务器),又懒得去对清单,如何在Linux下cpu的个数和核数呢? 另外,nginx的cpu工作模式也需要确切的知道linux服务器到底有多少个逻辑cpu,不过现在服务器那是相当的彪悍,直接上 worker_processes 8吧。
判断依据:
1.具有相同core id的cpu是同一个core的超线程。(Physical id and core id are not necessarily consecutive but they are unique. Any cpu with the same core id are hyperthreads in the same core.)
2.具有相同physical id的cpu是同一颗cpu封装的线程或者cores。(Any cpu with the same physical id are threads or cores in the same physical socket.)
以自己的惠普DL380G6为例说明:
①物理cpu个数:
[root@localhost ~]# cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
②每个物理cpu中core的个数(即核数)
[root@localhost ~]# cat /proc/cpuinfo | grep "cpu cores" | uniq
cpu cores : 4
这样可以推算出自己服务器的逻辑CPU为物理个数*核数,如果你的服务器是server2003,你可以在你的资源管理器里以图形化方式看到你的逻辑CPU个数。

二、查看服务器的内存情况
有时候,你运行了许多大的进程,比如你开启了300个fast-cgi。这时候你感觉系统很慢,便需要查看服务器的内存情况:
[root@server ~]# free -m
                     total used free shared buffers cached
Mem:            249   163    86            0        10         94
-/+ buffers/cache:   58  191
Swap:            511       0  511
参数解释:
total 内存总数
used 已经使用的内存数
free 空闲的内存数
shared 多个进程共享的内存总额
buffers Buffer Cache和cached Page Cache 磁盘缓存的大小
-buffers/cache (已用)的内存数:used - buffers - cached
+buffers/cache(可用)的内存数:free + buffers + cached
可用的memory=free memory+buffers+cached
上面的数值是一台我公司内网供PHP开发人员使用的DELL PE2850,内存为2G的服务器,其可使用内存为=217+515+826。记住,Linux的内存使用管理机制是有多少就用多少(特别是在频繁存取文件后),即Linux内存不是拿来看的,是拿来用的。


编辑推荐:Linux系统内存监控全面讲解:free命令

三、服务器磁盘使用情况
有时感觉硬盘反映很慢,或需要查看日志所在分区时,下列命令可以查看磁盘的使用情况,很有用:
①查看硬盘分区情况
fdisk -l
②查看当前硬盘使用情况
df -h
③查看硬盘性能
# iostat -x 1 10
Linux 2.6.18-92.el5xen                          03/01/2010

avg-cpu: %user %nice %system %iowait %steal %idle
                     1.10 0.00 4.82 39.54 0.07 54.46

Device:   rrqm/s wrqm/s       r/s       w/s     rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util

sda              0.00       3.50    0.40      2.50       5.60   48.00      18.48         0.00   0.97   0.97   0.28
sdb              0.00       0.00    0.00      0.00       0.00      0.00       0.00          0.00   0.00   0.00   0.00
sdc               0.00       0.00    0.00      0.00       0.00      0.00       0.00         0.00    0.00   0.00  0.00
sdd              0.00       0.00    0.00      0.00       0.00      0.00       0.00          0.00    0.00   0.00  0.00
sde              0.00       0.10    0.30      0.20       2.40      2.40       9.60          0.00    1.60   1.60  0.08
sdf             17.40       0.50 102.00    0.20 12095.20   5.60   118.40          0.70    6.81  2.09 21.36
sdg          232.40       1.90 379.70    0.50 76451.20 19.20   201.13          4.94 13.78   2.45 93.16
像我们公司,开发人员占多数。有时为了节约成本,会同时采购一些性价比比较高的二手服务器;这时一般将服务器的硬件的工作模式设置为RAID1,同 时对几种型号的服务器作相同文件的写操作,然后各自执行iostat –d,作对比性测试。这样服务器的硬盘性能孰优孰劣,一下子就能对比出来。


图解:
Tps 该设备每秒I/O传输的次数(每秒的I/O请求)
Blk_read/s 表求从该设备每秒读的数据块数量
Blk_wrth/s 表示从该设备每秒写的数据块数量
编辑推荐:Linux系统监控之磁盘I/O篇


四、查看系统内核
查看系统内核主要为了掌握其版本号,为安装LVS等软件做准备。
uname –a
有关查看内核信息的更多指令,可参考Linux查看版本信息及CPU内核、型号等一文。有关Linux的内核优化,则可参考Linux 2.6.31内核优化指南一文。

五、查看服务器使用的Linux发行版的名称、版本号及描述信息等
lsb_release -a
这是我的某台用于SVN实验的vmware机器情况:







六、查看服务器的平均负载
感觉到系统压力较大时用可top或uptime查看下服务器的平均负载。uptime的另一个用法是查看你的Linux服务器已经稳定运行多少天没有重启了,我这边的机器最高记录是360多天。

七、查看系统整体性能情况
如果感觉系统比较繁忙,可以用vmstat查看系统整体性能情况。vmstat不仅仅适应于linux系统,它一样适用于FreeBSD等unix系统。
vmstat 1 2
如果 r经常大于 4 ,且id经常少于40,表示cpu的负荷很重。
如果pi,po 长期不等于0,表示内存不足。
如果disk 经常不等于0, 且在 b中的队列 大于3, 表示 io性能不好。
编辑推荐:Linux系统监控工具之vmstat详解

八、查看系统已载入的相关模块
Linux操作系统的核心具有模块化的特性,应此在编译核心时,务须把全部的功能都放入核心。你可以将这些功能编译成一个个单独的模块,待需要时再分别载入。比如说在安装LVS+Keepalived,下列用法被经常用到:
#检查内核模块,看一下ip_vs是否被加载 lsmod |grep ip_vsip_vs 77313 0
如果要查看当前系统的已加载模块,直接lsmod。

九、Linux下查找PCI设置
有时需要在Linux下查找PCI设置,可用lspci命令,它可以列出机器中的PCI 设备,比如声卡、显卡、Modem、网卡等,主板集成设备也能列出来。lspci 读取的是hwdata 数据库。有的小伙可能和我一样,最关心的还是网卡型号:
[root@mail ~]# lspci | grep Ethernet
03:00.0 Ethernet controller: Broadcom Corporation NetXtreme II BCM5708 Gigabit Ethernet (rev 12)
07:00.0 Ethernet controller: Broadcom Corporation NetXtreme II BCM5708 Gigabit Ethernet (rev 12)

推荐阅读:七大实用命令行工具 玩转Linux网络配置。至于网络流量的监控,建议用centos自带的工具iptraf,其用法可参考这篇文章。

掌握上面这九条,你对你的Linux服务器的硬件信息基本就可以随时掌控了。

freebsd 查看带宽占用情况,CPU,硬盘IO 虚拟内存等命令

systat 能实时查看各种信息
systat -pigs 默认值CPU
systat -iostat 硬盘IO
systat -swap 交换分区
systat -mbufs 网络缓冲区
systat -vmstat 虚拟内存
systat -netstat 网络
systat -icmp ICMP协议
systat -ip IP协议
systat -tcp TCP协议
systat -ifstat 网卡


显示PCI总线设备信息
pciconf -lv
显示内核加载的模块
kldstat -v
显示指定模块
kldstat -m ipfilter
即插即用设备
pnpinfo

显示设备占用的IRQ和内存地址
devinfo -u
cpu
sysctl -a|grep cpu
sysctl  -a|grep sched 查看使用的调度器,我编译的是ULE


虚拟内存
vmstat
硬盘
gstat
systat -iostat
iostat
网卡
ifconfig
systat -ifstat
网络
netstat
sockstat
tcpdump
trafshow
systat -mbufs
systat -icmp
systat -ip
systat -tcp
只是看流量的话,用systat  -netstat

freebsd 路由问题

使用virtualbox安装的freebsd配置的nginx+php-fpm+mysql安装的wordpress无法使用no-ip的动态域名访问, 可是同样配置的arch上的wordpress却可以从外网无障碍访问?


使用路由的端口映射,把外网的80端口转给内网的虚拟机上,就是使用的NAT(network address translation)(网络地址转换)


仔细研究了研究,2机的不同点只有ip,freebsd的ip为192.168.1.110,arch的ip为192.168.111

然后就想到是不是路由设置的问题?

2台guest机为了和主机通信,都是使用的2个网卡,一个使用的NAT自动获取地址;1个使用的bridged 桥接网络来实现和 host机的链接.

使用netstat -s 来察看

发现freebsd的设定为192.168.1.110的2号网卡em0没有设置网关....难道就是这个问题?

最后,每个路由的不同属性可以在 Flags 列中看到。下边是个关于这些标志和它们的含义的一个简表:
U Up: 路由处于活动状态。
H Host: 路由目标是单个主机。
G Gateway: 所有发到目的地的网络传到这一远程系统上, 并由它决定最后发到哪里。
S Static: 这个路由是手工配置的,不是由系统自动生成的。
C Clone: 生成一个新的路由, 通过这个路由我们可以连接上这些机子。 这种类型的路由通常用于本地网络。
W WasCloned: 指明一个路由——它是基于本地区域网络 (克隆) 路由自动配置的。
L Link: 路由涉及到了以太网硬件。

通过修改/etc/rc.conf 文件,给em0网卡添加路由

在/etc/rc.conf中,添加
defaultrouter="192.168.1.1"

然后重启网络服务
# /etc/rc.d/netif restart
注意: 如果在 /etc/rc.conf 中配置了默认网关, 还需要运行下面的命令:
# /etc/rc.d/routing restart


然后,使用noip注册的域名来访问,OK啦!


参考  :  设置网卡
             网关和路由

用pkg_cutleaves来管理软件包

作者:于江生(北京大学计算机系)
声明:允许未经作者的同意进行非商业目的的转载,但必须保持原文的完整性。

FreeBSD对包的管理极为严格,
# make deinstall clean
可以轻而易举地将一个软件包卸载干净。如果由于某些特殊原因系统不能干净地删除某个目录,它会明白地提示用户哪个目录或哪个文件未清除,待用户进一步查明。与GNU/Linux相比,FreeBSD对port tree的维护和管理要更学院派一些。我喜欢用
# make install clean
来编译源码安装,有时也随意地pkg_add。按照运行依存(run dependency),工具软件之间的关系复杂,呈现网状结构,动一处则牵动甚多。FreeBSD下安装软件简单至极,系统能自动按照依存关系递归地安 装所需要的ports。卸载软件也不难,但如果用户有“洁癖”,不想多保留任何“无用的东西”,则就需要pkg_cutleaves来帮忙管理这些软件包 了。
内容摘要

问题的由来

我们在使用FreeBSD的时候,经常安装软件包。在安装的过程中,有一些build时依存的包会先被安装,而往往它 们不是我们所需要的。例如在安装xvidcap时先安装gawk,安装后也不卸载gawk。如果我们不需要gawk,就不得已在安装成功xvidcap后 把gawk手动卸载。
问题是,包之间的复杂的依存关系让我们手动卸载时经常出错。这时,我们需要工具来梳理已经安装的软件包之间的关系,按照它们之间的依存关系整理出一 个有向非循环图(DAG),我们不妨称之为依存图,以此约束我们每一次的卸载都是从叶子结点开始。幸运的是,我们有pkg_cutleaves这样好用的 管理工具,可以帮助我们递归地卸载某些我们不需要的叶子结点。下面给出一个简短的介绍,假设命令都是root下达的。

显示所有叶子结点信息

首先,我们通过
# pkg_cutleaves -lc > ~/installed
来生成所有的叶子结点,大致样子如下:
ImageMagick-6.4.3.6 - Image processing tools
R-2.7.1 - A language for statistical computing and graphics
WordNet-3.0 - Dictionaries and thesauri with devel. libraries (C, TCL) and browsers
ardour-2.4.1 - Ardour - a digital audio workstation
auctex-emacs22-11.85 - Integrated environment for writing LaTeX using GNU Emacs
bibtool-2.48_1 - Command line manipulation of BibTeX files

卸载我们不需要的叶子结点

我们通过
# pkg_cutleaves -lxg
来生成一个文件:/usr/local/etc/pkg_leaves.exclude,这个文件记录了所有叶子结点的文件名,不包含版本信息。我们可以 编辑修改这个文件,去掉我们不需要的叶子结点。接着,我们可以放心地清除我们不需要的build依存包,一个简单的命令就搞定:
# pkg_cutleaves -Rxg
系统会交互地询问我们是否删除某某某,保证文件/usr/local/etc/pkg_leaves.exclude所记录的就是软件依存图的所有叶子结点。概括而言,pkg_cutleaves清除软件包安装过程中产生的冗余的包,为我们的系统瘦身。

Sunday, November 6, 2011

FNMP freebsd8.2 安装配置 nginx+mysql 51+php-fpm 53

FNMP (Fcgi, Nginx, Mysql and Php)

freebsd8.2 下ports 安装配置 nginx+mysql+php-fpm

 因为php需要mysql支持,所以,我一般按照mysql+php+nginx的步奏来安装.

1 安装 mysql 5.1版本 
(mysql55版本老编译不过去,还是采用稳妥的51来安装)

cd /usr/ports/databases/mysql51-server
make WITH_CHARSET=utf8 WITH_XCHARSET=all WITH_COLLATION=utf8_general_ci install clean


启动 
添加以下内容到/etc/rc.conf中

mysql_enable="YES"
 
配置

/usr/local/etc/rc.d/mysql-server start
给root帐号添加密码
mysqladmin -u root password 'MyPassword'

service mysql-server restart   *mysql重新启动
mysql -u root -p
 show variables like 'char%';    *设定编码察看
配置文件
挑选下面的配置拷贝到/etc目录
比如# cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf
 
/usr/local/share/mysql/my-huge.cnf
/usr/local/share/mysql/my-innodb-heavy-4g.cnf
/usr/local/share/mysql/my-large.cnf
/usr/local/share/mysql/my-medium.cnf
/usr/local/share/mysql/my-small.cnf

2 安装php53+php-fpm


安装 
 cd /usr/ports/lang/php5
 make install clean

 
 cd /usr/ports/lang/php5-extension
 make install clean

我这里主要选择了以下几个
[X] BCMATH bc style precision math functions
[X] BZ2 bzip2 library support
[X] CURL CURL support[X] FTP FTP support                          
[X] GD GD library support
[X] ICONV iconv support[X] MCRYPT Encryption support
[X] MHASH Crypto-hashing support
[X] MYSQL MySQL database support           *重要mysql支持
[X] MYSQLI MySQLi database support         *重要mysql支持
[X] OPENSSL OpenSSL support[X] PCRE Perl Compatible Regular Expression support
[X] XML XML support[X] XMLREADER XMLReader support
[X] XMLWRITER XMLWriter support
[X] ZLIB ZLIB support

配置
1 php
在freebsd默认安装的是没有配置文件的,需要你自己拷贝一个

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

php.ini-development 开发测试版本  php.ini-production 生产适用版本


2  php_fpm
 配置文件 /usr/local/etc/php-fpm.conf

启动
在 /etc/rc.conf 中添加
php_fpm_enable="YES"
启动php-fpm
# /usr/local/etc/rc.d/php-fpm start


3 安装nginx


cd /usr/ports/www/nginx
make install clean

这里除了默认主要添加了ssl支持这个选项,其他就选择默认就可以了
[X] HTTP_SSL_MODULE Enable http_ssl module
[X] HTTP_STATUS_MODULE Enable http_stub_status module
[X] WWW Enable html sample files

编译完成后,启动 nginx
  1. # echo nginx_enable="YES" >> /etc/rc.conf
这里先不急着 start nginx,因为现在还没有对 PHP 的支持。
  1. # vi /usr/local/etc/nginx/nginx.conf
将如下段落前的 “#” 删除,并且将 html 更改为 /usr/local/www/nginx
location ~ \.php$ {
root /usr/local/www/nginx;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
结束后,编辑 /usr/local/etc/nginx/fastcgi_params,加入
  1. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
启动 nginx,看看是否已经对 PHP 了
  1. # /usr/local/etc/rc.d/nginx start
  2. # echo "" > /usr/local/www/nginx/info.php

vi /usr/local/www/nginx/info.php
在文件中输入以下内容
phpinfo();
?>

访问 http://yourdomain/info.php,看到 phpinfo 的页面则证明无误。


参考  : Nginx + PHP-FPM + MySQL On FreeBSD 8.2
           nginx + PHP-FPM + MySQL + eAccelerator FreeBSD 8.0 安装笔记
            FreeBSD上搭建nginx 0.8.x + PHP 5.3.x(FastCGI) + MySQL 5.1.x

Saturday, November 5, 2011

freebsd8.2 安装mysql55后修改默认编码utf8

根据160630的patch状态回帖.可以编辑my.conf来更改默认的新建数据库编码

State Changed
From-To: open->closed
By: ale
When: Tue Sep 13 14:56:05 UTC 2011
Why: You should use my.cnf for this config params.

具体参考 : freebsd mysql5.1 与mysql5.5的参数变更

使用如下patch给/usr/ports/datebase/mysql55-server/Makeconfig就可以了


diff -urN mysql55-server-old/Makefile mysql55-server/Makefile
--- mysql55-server-old/Makefile    2011-09-10 14:38:44.000000000 +0000
+++ mysql55-server/Makefile 2011-09-10 15:16:00.000000000 +0000
@@ -53,6 +53,12 @@
 .if defined(WITH_FASTMTX)
 CMAKE_ARGS+= -DWITH_FAST_MUTEXES=1
 .endif
+.if defined(WITH_CHARSET) && ${WITH_CHARSET} != ""
+CMAKE_ARGS+=       -DDEFAULT_CHARSET=${WITH_CHARSET}
+.endif
+.if defined(WITH_COLLATION) && ${WITH_COLLATION} != ""
+CMAKE_ARGS+=       -DDEFAULT_COLLATION=${WITH_COLLATION}
+.endif
 
 # MySQL-Server part
 .if !defined(CLIENT_ONLY)

然后编译的时候,使用
make WITH_CHARSET="utf8" WITH_COLLATION="utf8_unicode_ci" install clean

参考 :  ports/160630: [PATCH] databases/mysql55-server Add WITH_COLLATION and WITH_CHARSET

以往在freebsd中安装databases/mysql55-{server,client}时,需要如下的参数 :


make WITH_CHARSET=utf8 WITH_COLLATION=utf8_general_ci WITH_XCHARSET=complex install


不过因为在 MySQL 5.5.8 以后的 build system 改成了 CMake,导致 ports maintainer 必须重新设计 Makefile。
而目前的 Makefile 尚未设计完善,所以想把这些 options 传递給 CMake 的话,只能直接改 Makefile 解決:


--- mysql55-server/Makefile.orig       2011-03-19 21:13:47.000000000 +0800
+++ mysql55-server/Makefile    2011-04-08 14:31:05.815962791 +0800
@@ -39,7 +39,10 @@
                -DINSTALL_SQLBENCHDIR="share/mysql" \
                -DINSTALL_SUPPORTFILESDIR="share/mysql" \
                -DWITH_LIBEDIT=0 \
-               -DWITH_LIBWRAP=1
+               -DWITH_LIBWRAP=1 \
+               -DDEFAULT_CHARSET="utf8" \
+               -DDEFAULT_COLLATION="utf8_general_ci" \
+               -DWITH_EXTRA_CHARSETS="complex"
 
 .ifdef USE_MYSQL
 .error You have `USE_MYSQL' variable defined either in environment or in make(1) arguments. Please undefine and try again.

如果在做这修改前不小心把沒设好的 MySQL 整套装起來的话,请记得把 mysql55-client 也砍掉一起重裝。
特別是对那些真正需要用到 WITH_EXTRA_CHARSET="complex" 的人,有一边沒弄好的話,网页上就准备出现一大堆「???????」了。
至於是否能像以前那样直接用 make 送參數,就只能慢慢等 maintainer,看他何時會重新把所有 options 對應上去了。


通过使用port来安装
1.该ports 做得不是太好,不支持 make config 修改选项,只能在命令行用
make WITH_CHARSET=gbk WITH_XCHARSET=all 追加参数,或者直接修改 Makefile文件。

2.WITH_CHARSET=gbk 是将mysql的服务及数据库的默认字符集由latin1改为gbk;WITH_XCHARSET=all 是将mysql软件包所带的字符集(包括gbk 和 gb2132)都释放出来。
mysql 不是真的不支持中文,只是采用了utf8字符集而已。 所以不建议你改变默认字符集,应由程序(如PHP)在连接数据库时选定合适的字符集。如,在config.inc.php之类连接参数文件中设定 mysql_query("set names gbk");改变默认字符集可能导致有些外国编的软件如VBulletin3.60运行错误。建议你在mysql5编译时只增加 WITH_XCHARSET=all 即可。

3.应用 make clean 清除配置(假设你以前编译过)
然后 make WITH_XCHARSET=all
最后 make install 安装编译好的软件包(如你以前以前安装过,最好用make reinstall)

4.如你用phpmyadmin导入以前的数据(gb2132 或gbk格式),在创建数据库时务必将默认参数“整理”改为gbk_chinese_ci
5.可以尝试修改my.cnf文件来改变服务器以及客服端的字符编码
[client]
...
character-set-server=utf8

collation-server=utf8_general_ci

default-character-set=utf8
[mysqld]
...
character-set-server=utf8

collation-server=utf8_general_ci

default-character-set=utf8
[mysql]
.......
default-character-set=utf8

freebsd mysql5.1 与mysql5.5的参数变更

MySQL 5.1

[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8

MySQL 5.5

[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8

察看支持的编码 使用 
mysql> SHOW VARIABLES LIKE  'char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | latin1                           |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/share/mysql/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)


参考 mysql5.1与mysql5.5的参数变更

Wednesday, November 2, 2011

firefox的dns缓存功能

本地使用virtualbox安装了一个arch server假设了一个wordpress博客,发现当ip换掉以后,ff老无法更新新ip,原来firefox有dns缓存功能.

可以手动修改同时按下Ctrl + Shift + Delete,点details,只选中cache,然后点clear,删了。

about:config

network.dnsCacheExpiration  把原来的60改成 10000(表示缓存10000秒)
network.dnsCacheEntries  把默认的20改成1000(表示缓存1000条).如果没 有上面两个项目,新建即可。

但是dns缓存太久了也会出问题,比如有的网站ip换了,就无法访问了。针对这样的问题,可以安装一 个 firefox 插件来开启或者关闭dns cache功能,https://addons.mozilla.org/zh-CN/firefox /addon/5914

参考 : Simple Firefox DNS Hacks To Boost Performance

修改fstab让ntfs分区挂载文件读写权限,文件夹读写执行权限

一直使用的ntfs-3g来读写win下的分区,没有怎么注意权限的问题,可是看到挂载的分区文件都是可执行权限,对安全性以及读写都有问题,因为使用awesome所以一般很少开文件管理器,最多使用mc来批量复制拷贝文件,看到一片绿色可执行,头大.

察看fstab的参数原来可以制定挂载的时候,对文件以及文件夹采用不同的权限.

比如下面这段挂载的参数:

/dev/ /mnt/windows ntfs-3g gid=users,fmask=113,dmask=002 0 0 

以群组users,文件权限664,文件夹权限774来挂载.

fmask,dmask,以及umask的意思就是扣除相应的权限,777-$(*mask)= 来定义挂载的权限.

umask代表user的mask,fmask代表文件file的mask,当然dmask就代表文件夹的mask了.


一.022表示默认创建新文件权限为755 也就是 rxwr-xr-x(所有者全部权限,属组读写,其它人读写) 
二.027表示默认创建新文件权限为750 也就是rxwr-x---(所有者全部权限,属组读写,其它人无)

下表列出了一些umask值及它们所对应的目录和文件权限
常用的umask值及对应的文件和目录权限
umask值        目录         文件
022               755          644
027               750          640
002               775          664
006               771          660
007               770          660
我现在的fstab配置文件如下
/etc/fstab.html
# 
# /etc/fstab: static file system information
#
# <file system> <dir>   <type>  <options>   <dump>  <pass>
tmpfs       /tmp    tmpfs   nodev,nosuid    0   0
UUID=b2b3abc1-d666-4948-9c17-a5e12c637d63 /boot reiserfs defaults 0 1
UUID=afd39ba2-f3f3-4bac-bbc8-dc5307ba4e89 / ext4 defaults 0 1
UUID=26f23578-3dc8-4394-bcba-7320a4aaff7d swap swap defaults 0 0
UUID=90ab4280-c642-47c0-91e4-d3be7d254d1a /home ext4 defaults,user_xattr 0 1
UUID="4D9E019F07B2E598"  /media/wine ntfs-3g gid=users,fmask=113,dmask=002,nls=utf8 0 2

Tuesday, November 1, 2011

mpd 启动错误 Address family not supported by protocol

这2天arch启动的时候tty1老提示 Error : Socked :  Address family not supported by protocol


放google搜索,找到Address family not supported by protocol - IPv6 issue on machines with disabled v6

This happens on a machine that has socket.has_ipv6 set to true as the linux system supports IPv6, but jason has disabled IPv6 by following https://wiki.archlinux.org/index.php/IPv6_-_Disabling_the_Module

手动启动的时候提示如下错误:

listen: bind to '[::1]:6600' failed: Failed to create socket: Address family not supported by protocol (continuing anyway, because binding to '127.0.0.1:6600' succeeded)
playlist directory is not a directory: "/home/jarodlau/.mpd/playlists"

原来是禁用ipv6的结果,另外我没有把播放列表的目录拼写正确,少了个s,应该是playlists,我的写成了playlist

解决办法很简单 :

编辑 /etc/hosts文件 ,把一些ipv6的地址注释掉就ok了,如下

/etc/hosts.html
#
# /etc/hosts: static lookup table for host names
#

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1   localhost.localdomain   localhost arch-laptop
#::1        localhost.localdomain   localhost

# End of file

Monday, October 31, 2011

2个不错的dns

因为电信的114问题,dns劫持严重,于是使用了pdnsd来本地缓存dns信息,可是google和opendns的延迟很严重,所以一直在找其他不错的dns

google的8.8.8.8和8.8.4.4以及opendns的208.67.222.222和208.67.220.220,用的人太多了,gfw重点照顾,所以不太好用了

推荐个台湾中华电信dns:168.95.192.1 和 168.95.192.2

编辑pdnsd的配置文件  sudo vim /etc/pdnsd.conf


~/pdnsd.conf.html
# The following section is most appropriate if you have a fixed connection to
# the Internet and an ISP which provides good DNS servers.
server {
    label= "mydns";
    ip = 168.95.192.1,168.95.192.2;  # Put your ISP's DNS-server address(es) here.
#   proxy_only=on;     # Do not query any name servers beside your ISP's.
                       # This may be necessary if you are behind some
                       # kind of firewall and cannot receive replies
                       # from outside name servers.
    timeout=4;         # Server timeout; this may be much shorter
               # that the global timeout option.
    #uptest=if;         # Test if the network interface is active.
    #interface=eth0;    # The name of the interface to check.
    #uptest=ping;
    #    ping_timeout=100;
    interval=10m;      # Check every 10 minutes.
    purge_cache=off;   # Keep stale cache entries in case the ISP's
               # DNS servers go offline.
} 
然后重启就ok了 sudo rc.d restart pdnsd

Saturday, October 29, 2011

“代码贴吧”网址收集

若想在网上与人分享短小的源代码,推荐用以下网站:
http://pastie.caboo.se/paste/
http://pastie.textmate.org/paste/ (同上)
http://pastebin.com/
http://rafb.net/paste/

三个免费BSD系统的比较

在网上看到这样的评论:

FreeBSD = Popular all-rounder.
NetBSD = Portable (runs on a lot of platforms, including a toaster)
OpenBSD = Security above anything else.


翻译过来就是:

FreeBSD = 流行的多面手
NetBSD = 可移植 (可在很多平台上运行, 包括一个烤面包机)
OpenBSD = 比其他任何东西都安全


应该说,这样的评价,非常精到。用过这三个系统的人我想都会点头同意。

FREEBSD的一些小技巧

1、在 unix-like 中档案结构是满重要的,那么要如何去看相关档案目录结构的说明呢?

难道是 cd 到每个目录下吗?其实有更好的方法..

那就是用好男人 man 来看:

# man hier

2、在 console 下操作时,由于屏幕显示有限,有时需看前面显示的资料,这时你可:

. 按一下 Pause or Scroll Lock键后,则可利用 PageUp PageDown Up Down Home End 等键来看前后显示的资料

. 再按一下 Pause or Scroll Lock则可回复等待输入画面.

3、 使用 pw 来管理你的帐号

在 FreeBSD 下使用 adduser 来新增帐号,有些时会满烦的,可用以下方式:

pw useradd -n -g -m -s /bin/csh

=> -m 代表建立使用者目录

●同时设密码

echo | pw useradd -n -g -m -s /bin/csh -h 0

● 删除帐号

pw userdel -n -r

=> -r 代表同时删除其 home 目录等相关资料

● 修改帐号

pw usermod -g -s /bin/csh

PS:无法修改帐号名称

● lock 及 unlock 使用者帐号

pw lock
pw unlock

4、 激活你的 screensaver

由于 FreeBSD 大多当为主机使用,都是常期不关机的,因此可将屏幕关掉,或使用 ScreenSaver 功能..

使用 ScreenSaver 请在 /etc/rc.conf 这个档加入以下两行:

blanktime="900"

=> 以秒数为单位,如以上为 15 分钟

saver="logo"

=> logo 是 FreeBSD 的吉祥物(图型接口),另也可使用 "daemon",这是文字型式

5、 检查你的某一个 ports 版本及更新

如果要更新所有的 port 只需在 /etc/ports/ 下执行以下指令:

# make update

有时我们可能要检查某一个 ports 的版本,在每个 ports 中的 Makefile 都有这一行

PORTVERSION= x.x.x

如此就可看到这个 ports 的版本了,另外也可使用以下方式(用 grep),比如我们想看 mod_php4 的版本

# grep PORTVERSION= /usr/ports/www/mod_php4/Makefile
PORTVERSION=   1.4.1 -> 版本讯息

如果版本太旧了,则可利用以下方法来更新单一 ports

a. 使用 portupgrade 或 portinstall,请用 man 查看用法

b. 到 FreeBSD 网站查询最新的 ports 信息,并抓 ports 回来更新

网址:http://www.freebsd.org/cgi/cvsweb.cgi/ports/

抓回来后在相关目录更新,例如我们抓回的是 mod_php4.tar.gz,存在 /usr/tmp/ 目录下,则解压缩的方式如下:

# cd /usr/ports/www/mod_php4
# tar xzvf /usr/tmp/mod_php4.tar.gz

如此就 ok 了..

6、 加速你的按键速度..

相信使用 FreeBSD 的人很多都是在 console 下操作吧,有些时,要重复某一按键速度实在很慢..

另一个问题就是 cusror 的位置有些时不太明显..

可用以下方法来改善:

在 /etc/rc.conf 中加入以下资料..

keyrate="fast"
cursor="blink" 或 "destructive"

7、 设定开机画面

使用 windows 的人,都知道所谓的开机画面,在 FreeBSD 下是否也有此功能呢?答案是肯定的,而且设定更为简便,请照以下 steps:

一. 首先制作一个图型文件,档案格式为 logo.pcx 或 logo.bmp,需注意的是 .bmp 的档案存盘时不用压缩,分辨率需为 640 X 480 以内(含),色阶为 256 色..

二. 将此档案 copy 到 /boot/ 这个目录下

三. 设定 /boot/loader.conf,加入以下资料

splash_bmp_load="YES" -> 如果使用 .bmp 格式,此项需设为 YES
splash_pcx_load="YES" -> 使用 .pcx,请将此项设为 YES
vera_load="YES"
bitmap_load="YES"
bitmap_name="/boot/logo.pcx" -> 请依你的档案格式更改
bitmap_type="splash_image_data"

四. 重新激活
ps: 请检查你的 kernel 中是否有设定以下资料,一般在 4.x 中都是内定值..,至于 3.x 版,没用过..^.^

preudo-device splash

如果没有,则需将此行加入并重新编绎核心.


8、 一些 console 下使用的快速键..

相信大多使用 unix-like 的使用者,还是在 console 下执行你的日常维护工作吧,那有那些按键组合可帮我们提高效率呢..

以下按键皆适用于 tcsh 及 bash..

1. 快速移动光标

ctrl+a -> 到最前面
ctrl+e -> 到最后面

2. 编辑方面

ctrl+d -> 删除光标所在字符
ctrl+w -> 删除光标所在字前面的字符
ctrl+u -> bash:将光标前所有的字及字符删掉,tcsh:删除所有字
ctrl+k -> 将光标后所有字符删掉
ctrl+b -> 下个字符
ctrl+f -> 前一个字符
ctrl+m -> 和按下 enter 的结果一样
ctrl+h -> 和按下 del 或 backspace 键一样
del 或 backspace -> 删除前一个字符

如果对按键有研究兴趣者,不彷参考 /usr/share/syscons/keymaps/ 这个目录,其中 us.iso.kbd 是一般我们使用的按键..

9、 快速切换目录

在 FreeBSD 中常常需要切换目录,有时目录名称较长、或是有大小写、或是只记得前面几个字,怎么办..

其实只要输入部份字再加上 * 这个符号就可以了..

如我们要切换到 /usr/local/ 这个目录,只需输入 cd /usr/lo* 就可以了..

如果输入 cd /usr/l* 会如何呢?则会进到 /usr/lib/ 这个目录,这是因为 shell 找到第一个符合的条件便停止了..

10、 有关时间及时区的调校

使用 FreeBSD 大多是拿来当做主机使用,那时间及时区的正确与否就非常重要了,以下介绍如何调整时间及时区

● 时间调整
1. 使用 date 指令

格式 date [yymmdd]HHMM
说明:
yy 年数,如 02 代表 2002 年
mm 月份 01-12
dd 日数 01-31
HH 时数 01-59
MM 分钟 01-59

yy mm dd 皆可省略

例:

?#123;整日期为 2002-03-25
# date 022325

?#123;整时间为 14:20
# date 1420

2. 使用对时主机

# ntpdate clock.stdtime.gov.tw

这项需能连上网络,其中 clock.stdtime.gov.tw 是一标准时间的主机

● 时区的设定

1. 使用 tzsetup

2. 在 /etc/ 下有个档案 localtime,这个就是本机的时区设定档案,因此我们可用个取巧方式(不经由 tzsetup),如以下:

# cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime

这样是不是比较快呢..

11、 如何清空档案内容..

有时我们需将一个档案内容清空,但需保留这个档案..

一般常见的用法是用文字编辑器,如 vi 将这个档案叫出来,再将其内容全部删掉,再存盘..

这样是不是有些烦呢?

有一个更好的方法,使用 true 指令..

如我们要将 /var/log/httpd-access.log 清空,则下以下指令..

# cd /var/log/
# true > httpd-access.log

12、 显示目录及档案 => tree

有时需显示某个目录下所有的目录及档案,虽使用 ls 可达到部份功能,但并不是很方便..

在 dos 中,有个 tree 指令,是不是满好用的..

其实在 FreeBSD 中也有类似的指令 => tree..

以下我们就透过 ports 来安装 tree

# cd /usr/ports/sysutils ; make install

如此会将 tree 这个指令安装在 /usr/local/bin/ 这个目录下..

使用例子:

1. 显示 /usr/ 下的目录及档案

# tree /usr

2. 只显示 /usr 下的目录

# tree -d /usr

详细用法请参考 man tree

PS:可透过管道 | 及 more 来显示

如 tree /usr | more..

13、 如何检视档案类型

由于 FreeBSD 并没有像 dos 或 windows 般有扩展名的观念,因此要如何检查一个档案的类型呢?

用 file 吧..

语法: file

详细用法请参考 man file

14、 重复使用先前使用过的指令

你可以使用以下的方法来重复使用先前使用过的指令:

例如,我们先用 history 显示先前输入的指令:

# history
.
..
...
10 clear
11 cd /usr/local
12 ls
13 ls etc
14 cd ~
15 clear

1. 用 up down 可用上下一个指令,或是 ctrl+p ctrl+n 亦然

2. 配合 ! 的用法,例如以下(ps: 在<...>内代表要输入的东东)

!!
# !! => 重复执行上个一个指令

!<编号>
# !11 => 执行 cd /usr/local

!<指令> 可输入部份字,但前面部份需符合
# !cl
执行 15 clear 指令

! 输入部份字符,部份吻合
# !?etc?
执行 13 ls etc

以上指令在 tcsh 及 bash 下皆可使用,如此是不是可帮你省下许多时间..

15、 如何将 man 的档案印出来..

man 是大家学习 FreeBSD 的最佳资料参考来源,但在显示器上看到底不是很方便,有没有方法转成一般的文件档案呢?

使用 col 这个指令吧..

例我们要将 ssh2 转成 ssh2.txt,则使用以下语法:

# man ssh2 | col -b > ssh2.txt

16、 为何查不到正确的 ports 资料..

在 /usr/ports/ 下有着几千个 ports 的安装资料,有时我们可使用以下方式来查相关的 ports..

以下都是在 /usr/ports/ 这个目录下执行..

1. 先将 ports 资料转换成一个文字文件,如 index.txt

# make print-index > index.txt

2. 使用 make search key="...." 来查

3. 将 ports 内容转换成 README.html

# make readmes

用以上方法,可很容易的查到相关的 ports 资料,但很奇怪的是,为何我更新一个 ports 的资料后(或 ports tree),为何会查不到,查到的仍然是旧的资料..

原来,在 /usr/ports/ 中有一个 INDEX 檔,以上的查询方式都是在这个 INDEX 档中查询,当你更新 ports 资料时,这个 INDEX 档并不会随之更新..

所以为了查询方便,我们需以手动的方式来做..

# make index

做完后,再更新 index.txt 及 README.html..

不过更新 INDEX 档需要一些时间,大约 12-30 分钟..(视计算机等级而定).

17、 用 dig 及 host 来取代 nslookup

一般我们用来检查网络是否有问题,大多是用以下三个指令..

nslookup
ping
traceroute

其中 nsllokup 是用来检查 dns 的相关设定, ping 用来检查自己和对方网络是否通顺,而 traceroute 则是用来检查从自己的计算机到对方的计算机所经过的线路状况..

不过以 nslookup 而言,个人觉得不是很实用,有时我们只是想单纯的查一下某台主机或是 MX 记录,那使用 nslookup 就有点噜嗦了..

在此介绍二个好用的指令 host 及 dig,这二个指令很类似,不过效率较 nslookup 高

# host -a <主机名称> 或

=> 输入主机名称显示 dns 正向解析的部份,输入 ip 则是显示反向(ptr)

# dig <主机名称或ip>

如要显示 MX 记录

# dig 主机名称 mx

个人是觉得比 nslookup 方便多了,你觉得呢.. ^.^

当然功能不只这些,详情请 man dig 或 host..

18、 加快你的开机速度..

严格说起来,FreeBSD 的开机速度算满快的,不过中间会有个 10 秒的延迟时间(以便让你进入 boot 的 command prompt mode),如果你嫌这10秒还是太慢了,那你可将时间?#123;短一些..

FreeBSD 激活时会先去参考 /boot/defaults/loader.conf 档的设定,然后再参考 /boot/loader.conf,一般我们都不去?#123;整 /boot/defaults/loader.conf 这个档,而直接?#123;整 /boot/loader.conf..

只要在 /boot/loader.conf 中加上以下这一行即可

autoboot_delay="秒数" 即可,如

autoboot_delay="7" 代表7秒

这样开机是不是更快了..

另外建议,有时间可参考一下 /boot/defaults/loader.conf 这个档案的设定,其中有许多实用的设定哦..

19、 忘了 root 密码

如果你忘了 root 的密码怎么办,这在 FreeBSD 中满好解决的,请照以下 steps 来处理..

1. 进入单人模式

开机后,当出现

Hit [Enter] to boot immediately, or any other key for command prompt.
Booting [kernel] in 10 seconds...

按 space(或除了 enter 以外的键) 键则会进入 command prompt mode,同时出现一个 ok ..

这时请输入 boot -s 以进入单人模式

2. 选择 sh

进入单人模式后,系统会询问你要使用那个 sh,内定是 /bin/sh ,就使用这个吧,直接按 enter 跳过

3. 将所有的硬盘分割架起来

# mount -a

4. 更改密码

# passwd

长度要在 6 个以上,输入二次

5. 重新激活

# reboot


20、 有关 kernel 的激活..

kernel 对 unix-like 系统来说,是非常重要的一环,如果 kernel 损坏或编译出了问题,可能会导致严重的后果..

FreeBSD 一向以安全著称,在这方面有什么防护的措施呢?

FreeBSD 的 kernel 是放在 / 目录下,一般会有二个

kernel
kernel.GENERIC

如果你有编译过核心(kernel)的话,则原先的 kernel 会转换成 kernel.old,新的核心会变成 kernel,也就是在 / 下的 kernel 档案如下:

kernel => 新编译过的核心
kernel.GENERIC
kernel.old => 原先的核心

那 kernel.GENERIC 是什么呢,这个是系统安装时所装好的,一般都不会变动,即使编译过核心亦然,这个档案可说是救命核心,最好不要乱动,理由如下所言..

FreeBSD 激活时,会使用 kernel 这个核心,如果这个档案不存在,则会抓 kernel.old 这个档案..

如果这二个档案不幸都不在,那怎么办..这时则会进入 command prompt mode,你必需自行输入 kernel 的名称,如

boot 如

boot kernel.GENERIC

了解以上之后,在编译核心无法正常执行时,相信各位知道如何处理了..

21、 FreeBSD 在那激活程序..

一般 FreeBSD 激活后会依序执行以下位置的程序..

1. /etc/rc.conf 这个档案内的程序,如 sendmail、sshd .. 都是在此执行,另外要注意的是,在执行这个档案之前,会先参考 /etc/defaults/rc.conf 的设定

2. /usr/local/etc/rc.d 下的一些 .sh 檔,这些档案的权限都是被设定成 x ,如 apache samba .. 都是在此设定

3. /etc/rc.local 这里也可设定,如我要把 postfix 设定在此,只需在这个档案内加一行,如下

/use/local/sbin/postfix start &

加上 & ,就不会显示出讯息

4. 由 cron 所控制的一些档案,当时间到了,便会自动执行..

那心细的人可能会问, /etc/inetd.conf 内设定的呢?这些在第 1 项就执行了..

了解以上并时时注意是否有不明的程序在执行(可能是被人植入木马程序),也是日常检视系统的重要工作..

22、 加快你的主机名称查询速度..

unix-like 系统,初期都是使用 hosts 这个设定文件来做主机的查询,因此有些程序在查主机名称时,都会参照这个设定档,像 sendmail 就是一个很好的例子..

系统内定第一个是会查 /etc/hosts 这个档案,因此如果里面设定有问题,则会影响到某些程序的执行..

典型的 hosts 档设定如下..以我本身的主机来说

127.0.0.1 localhost.cmlab.idv.tw localhost
61.219.230.5 cmlab.idv.tw cmlab
61.219.230.5 cmlab.idv.tw.

其中第一及第二行最后一个项目是 aliases 名称(主机别名),比如我们在主机

ping cmlab.idv.tw

和

ping cmlab

是同样的结果..

你可把一些常用的主机加到这个项目内,如有一台 mail.cmlab.idv.tw ,假设 IP 为 61.219.230.8

则在这个 hosts 中加入以下

61.219.230.8 mail.cmlab.idv.tw mail
61.219.230.8 mail.cmlab.idv.tw.

这样就行了..

另外如果有自行架设 dns server,请注意在 /etc/host.conf 这个档案,典型的档案如下:

# $FreeBSD: src/etc/host.conf,v 1.6 1999/08/27 23:23:41 peter Exp $
# First try the /etc/hosts file
hosts
# Now try the nameserver next.
bind
# If you have YP/NIS configured, uncomment the next line
# nis

由以上可以看得出来,在查询主机名称时的顺序,是先查 hosts 再查 bind 也就是 dns,你可视你的需要修改其顺序..

另外要注意的就是 /etc/resolv.conf 文件,这个是有关 dns 主机 search 的顺序,如以我的设定如下:

domain cmlab.idv.tw
nameserver 61.219.230.5
nameserver 168.95.1.1
nameserver 168.95.192.1

其中第一行是设定我的网域名称,第 2-4 行则是 dns 主机的设定,建议至少设二个以上,以免一个挂了,还有个备用的..

第2行是我的 dns 主机,3-4 行是 hinet 的 dns 主机..

了解以上并有正确的设定,是有助于主机的查询的及速度的提升..

23、 我要如何分割硬盘..

档案目录对于 FreeBSD (或者是所有 unix-like 系统),都是相当重要的,如开始时,分割的不是很恰当,日后就会遇到一些麻烦,如空间不够..等问题..

那么,我们要如何分割比较恰当呢..

这就有关于你的用途及 FreeBSD 的目录架构了..

一般个人是建议你如此分割:

1. / 根目录,一般在 250 ~ 500 MB 绝对够用,以我的根目录来说,使用一年多了,也才用了 66 MB 多。因为这个目录只是一些 kernel modules sbin bin etc .. 等目录

2. swap 档,一般人都不知道如何设较恰档,这也没一个标准,最主要是要看你的 ram 及硬盘而言,一般设成 1-2.5 倍(相对于你的 ram)。如你的 ram 在 256 mb 以上(含),设成一倍;如在 64 - 128 mb,则建议设成 2 倍

如要检查你的 swap 使用状况,可用以下指令来看..

# pstat -T
44/4044 files
0M/127M swap space

由以上可看出目前使用的 swap 为 0 ,设定的大小为 128 M

3. /var 目录 及 /usr 目录

这二个目录一般都会占比较大的空间,/var 的变动较小,以我个人来说,一年前设为 8 GB ,在目前只使用了不到 2GB。

但要注意的是一般的 mail 软件,都是将信件存在 /var/mail/ 中,如果你的信件很多或很大,这个部份不妨加大一些..

而 /usr 这个目录能大就给它大一点的空间,因为所安装的程序,或是 hone 都是在这个目录下(如果你没将 home 目录设成一个分割) ..

那这两个要如何分配呢,我的建议是 usr 和 var 的比例为 3:1,或是 2:1

4. 有些主机使用者较多,如学校单位。如果你没将 home 设成一个 partition,则 FreeBSD 会将 home 安装在 /usr 目录下,如果为了管理及安全问题,则不妨将 home 分割为单一目录..

至于大小,我想应该 3-5 GB 左右,就绝对够用了

我们来举个实际例子,假设你有一颗 40 GB 的硬盘,使用内存为 128 MB RAM,那我建议你的硬盘如此分割..

1. / => 250 MB
2. swap => 取 2 倍,即 256 MB
3. /home 如要独立出来,给它 3-5 GB
4. 剩下来的以 3:1 或 2:1 的比例设给 /usr 及 /var


以上只是一般建议,最重要的是必需了解各目录的性质及实际使用情况而予以适当的分配,有关这方面的信息,不妨参考 man hier ..

25、 简单编辑你的文件档..

在 unix-like 中,标准的文字编辑器是 vi,不过 vi 对于初学者而言,并不是很容易上手..

而在 FreeBSD 中使用的是 ee ,这个文字编辑器,虽功能没 vi 这么强大,但使用简便..新手不妨初期时使用个编辑器..

其使用方式很简单,只要在 command line 下 ee 即可

# ee <要编辑的档案>
进入 ee 画面后,最上方是一些按键说明,如要取得相关的 ee 说明,可先按 ctrl+c 键,再输入 help,则会显示详细说明

有时可能只想在一个档案内加入一小部份的资料,那使用文字编辑器来处理,就显得没效率,其实我们可以利用 cat > [檔名]和 cat >> [檔名] 来做..

那这二个方式有何差异呢..

cat > [檔名] 时,如果档案不存在,则会自动产生一个档案;如档案存在则会将档案清空..

cat >> [檔名],档案不存在则自动建立此档,如存在则将所编辑的资料加在档案的最后..

举个例子,如我们要编辑 /etc/mail/access 这个是 sendmail 的 relay 设定档..

想在这个档案中加入 192.168.88.5 relay

则可用此方法(假设 access 档案已存在,故使用 cat >> .. 的方法) ..

# cat >> /etc/mail/access
192.168.88.5 RELAY
按 ctrl+d 存盘

如此则可将 192.168.88.5 relay 加到 access 的最后一行..

不过,建议大家,unix-like 的共通文字编辑器是 vi,有时间不妨好好的学一下 vi..

26、 FreeBSD 帐号及密码的注意事项

在架完 FreeBSD 后,再来就是新增人员(使用者)的资料了..

在设定帐号时,请注意以下事项:

1.其长度需在 1-16 个字符
2.使用英文小写字母、数字及 - _ 符号

在设定密码时,请注意以下事项

1. 密码可设可不设,但建议你一定要设
2. 长度最好在 6 个字以上,你知道据安全专家的说法,破解一个长度6及长度4密码,至少要多花 10 倍的时间
3. 请勿用你的生日、相关证照号码、电话..等资料,而使用大小写英文字母及混合特殊符号,如 ct!Home-12 就是一个相当不容易破解的密码
4. 长度最长我曾设到 16 位数(最大长度多少,有兴趣者可自行试试)

27、 有关系统的一些激活问题..

有时,会修改 FreeBSD 的一些设定,如一些网络参数、软件的 .conf 设定,或是 /etc/r.conf 檔,那么如何让设定值生效,是否一定要重新开机呢?

这可分为三方面来说..

1. 网络相关设定,如网络适配卡、hostname .. 等,只要执行以下指令就可以重新加载你的网络设定..

# sh /etc/netstart

netstart 是一个 script 档案,执行时会去参考 /etc/rc.network。因此只要执行这个指令就能够重新激活网络设定..

有兴趣者可参考一下 netstart 及 rc.network 这二个档的内容,研究看看到底做了些什么东东..

2. 修改了 rc.conf 中其它的设定

可参考以下三个 step 来做..

. 进入单人模式
# shutdown now

. 选择 sh,造内定值即可,即 /bin/sh

. 离开单人模式
# exit

这样就会重新激活系统了..

3. 软件的设定,如一些 *.conf 檔,像是修正了 apache sendmail .. 等

通常在 /etc/local/rc.d/ 这个目录中会有相关的 .sh 檔,直接执行就可以了,注意的是一般要加上参数,一般的参数有以下(视软件而定会有不同)..

start 激活
stop 停止
reload 或 restart 重新激活

第二种方法就是直接使用 killall,如我们要重新激活 inetd,可用以下方法:

# killall -1 inetd

或是

# killall -HUP inetd

这样就可以重新激活这个 daemon 了

28、 保护你的档案
在系统中,有许多档案或一些设定档是非常重要的,加上 FreeBSD (或是一般 unix-like)系统都没有类似 ms 的垃圾回收桶的功能,万一不幸删掉(或修改)某个重要的档案,可能会造成不小的困扰..

虽然,FreeBSD 对于档案的权限设定的非常严密,但有些时候,我们以 root 身份工作,仍会有此风险..

那要如何避免这种情形发生或是保护某些重要的档案呢?

我们可以用 chflags 来达成这个目的,顾名思义, chflags 是由二个字所组成,即 change 和 flags(档案的旗标)..

例如我们要保护 /etc/inetd.conf 这个档案,以免误删或被修改,则指令如下:

# chflags schg /etc/inetd.conf

这样就可以保护这些档案,如要显示这些档案的旗标(flag),可用 ls 来看..

# ls -lo /etc/inetd.conf => l 是小写的 L

-rw------- 1 root wheel schg 47 Mar 28 21:29 inetd.conf

要解除旗标设定,可用 unschg,也就是在 schg 前加 un ,如:

# chflags unschg /etc/inetd.conf

大部份我们进入主机都是以 root 身份执行,因此稍不小心,可能就..因此建议将一些重要的档案及设定档设定为 schg flags,以保护档案..

如 /bin、 /sbin、 /etc/.conf、 /usr/lcaol/etc/*.conf .. 这些档案最好都是定成 schg 旗标..

chflags 详细用法请参考 man chflags..

29、 谈谈软件的安装路径..

FreeBSD 对于档案目录的观念非常重,什么样的档案要放在那个目录都有规定,这也是有别于其它 unix-like 系统..

一般我们将软件安装分成二个部份,在这里来探讨一下软件到底都安装到那去了..

在安装软件时,通常会安装诸如 执行档、设定档、man file、doc 文件..

1. 安装系统时所安装的软件

执行档通常装在 /bin、/sbin、/usr/bin、/usr/sbin、/usr/libexec 下
设定文件通在在 /etc 目录下
man file 通常在 /usr/man 或 /usr/share/man
doc 文件通常在 /usr/share/doc 下

2. 经由 package 或 port 安装的软件

执行档通常装在 /usr/local/bin、/usr/local/sbin、/usr/local/libexec 下
设定文件通在在 /use/local/etc 目录下
man file 通常在 /usr/local/man ..
doc 文件通常在 /usr/share/doc 下

以上并不是绝对的(所以我用通常..^.^),因为有些特殊情况需视软件而定..

如此有时可能会造成些许困扰,因为如 ssh 、 bind .. 大多是在系统安统时就安装的,那如果升级新版时怎么办..

由 于 FreeBSD 找寻执行档时会依照 $PATH 这个环境变量找寻,因为 /bin、/sbin、/usr/bin、/usr/sbin 比 /usr/local/bin 或 /usr/local/sbin 先,所以会先执行..也就是先抓到旧版的执行档..

因此在更新时要注意这种情况,以免更新了还是执行旧的程序..

那么如何看安时程序时的相关档案的安装路径呢?通常有以下方法

1. 看 port 的 pkg-plist 檔
2. 有时用 man [执行档名称] 可看到一些重要档案的位置
3. 看说明文件,通常在 /usr/local/share/doc 目录下

30、 如何在 FreeBSD 同一片网硌卡加一个 ip 地址

有的时候,需要在同一片网络卡上加 ip 以达到一些特殊的要求..

在 FreeBSD 这是非常简单的,假设原先网络卡的 ip 是 211.211.211.211,想加一个 211.211.211.212,使用的是 rl0 这片网络卡,则做法如下..

1. 在 /etc/rc.conf 加上这一行

ifconfig_rl0_alias0="inet 211.211.211.212 netmask 255.255.255.255"

其中的 alias0 就是加装的第一个 ip,如要设定多个可在此修正,如 alias1、alias2.. aliasN

2. 重新激活网络卡,执行 /etc/netstart

# /etc/netstart

31、 简单的加密你的资料
在实务上我们常需要将某些敏感的资料加密,一般我们可使用 pgp 或在这里所介绍的 crypt 来达到这个功能..

crypt 是透过 | 这个管道转换功能及密码设定来达成加密的功能,也就是说将你所设的密码当做加密产生的种子随机数..

先说明一下 crypt 的使用方法:

crypt [-s] [-k] [password]

一般 -s 及 -k 参数可不予理会,password 是加密或解密的密码

在这里假设我们先产生一个 test.txt 档,内容为 test,然后用 1234 这个密码加密

# cat > test.txt
test
按 ctrl+d 存盘

先看看 test.txt 内容

# more test.txt
test

将 test.txt 这个档案用 1234 这个密码加密,需配合管道 | 及转向符号使用,加密后的文件名称为 test2.txt

# cat test.txt | crypt 1234 > test2.txt

如此则会产生一个加密后的档案 test2.txt,另外密码可先不输入,按 enter 键后会显示 enter key: 让你输入密码

我们来看看加密后的档案内容,原先是 test 结果变成了:

# more test2.txt
e&h => 已经不是原来的 test

接着来将 test2.txt 解开,如下,假设解开后的档名为 test3.txt:

# cyrpt 1234 < test2.txt > test3.txt

以上的意思是

1234 加密时的密码
< test2.txt 将加密的档案转给 crypt 处理
> test3.txt 解密后的资料转向到 test3.txt

再显示 test3.txt 的内容是否和原来一样,即 test

# more test3.txt
test


PS:

1. 加密或解密时档名如和源文件一样,则会将原来的档案取代
2. 另有一个指令 enigma 和 crypt 用法完全一样

参考资料 man crypt

32、用 telnet 简测你的服务器(Server)有无在跑

有时你可以用 telnet 测试你的 Server 有无正常的 run
例如:我要查看 ftpd 有无在跑
可下指令:# telnet localhost 21
若跑出类似以下的讯息, 表示有在跑
--------------------------------
Trying 127.0.0.1...
Connected to localhost.ahkun.
Escape character is '^]'.
220-=(<*> )=-.:. (( Welcome to PureFTPd 1.0.11 )) .:.-=(<*> )=-
220-You are user number 1 of 50 allowed
220-Local time is now 18:52 and the load is 0.06. Server port: 21.
220 You will be disconnected after 15 minutes of inactivity.

若跑出以下的讯息, 表示没在跑
------------------------------------------------
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

同理你也可以用:
telnet localhost 25 测 stmpd
telnet localhost 110 测 pop3d

结论:
正确来讲是用 telnet 测 port 啦~

33、 如何防止别人登录到你的计算机

有时,我们并不想让别人直接登入到我们的主机或是只想限定某些人可使用,如 telnet、ssh..

或者是开放了些服务,如 ftp、smtpd ..等,由于这些都必需在主机端建有帐号,因此防止不相干的人登入到你的主机是非常重要的,也是一个安全上的考量..

但一般限定的方式都是使用 ip 或是 domain 的方式,那么有无方法可解决这个问题呢?

答案是可从 /etc/login.access 来着手,这个档案就是限定 login 的..

一个典型的设定如下,例如我们想限定只有 root 及 使用者 john 可登入到主机(含local 及 remote):

-:ALL EXCEPT root john:ALL

可分为三个项目,每个项目间以 : 分隔,说明如下:

1. + 或 - 代表允许或禁止
2. 设定的使用者或群组,可用 ALL EXCEPT 来强化限定的范围

3. 限定的来源,如 ALL 代表全部、61.219.230. 、LOCAL、console、.bad.com ..等

适当的设定,可让你的系统更加的安全..

详细用法可 man login.access..


34、 一个 ./ 的技巧

众所周知,在 nix-like 中执行目前目录中的可执行文件时,必需加上 ./ 这二个符号,很多 unix-like 的初学者都会感觉较不习惯,因为在 dos 或 windows 的 dos 窗口中并不是如此..

其实在 unix-like 中如此设定是有其安全考量的,在使用指令或执行文件时,一般是建议使用完整的目录,例如要使用 ls,则如下:

# /bin/ls

当然,你直接输入 ls 亦可正确的执行,这是因为你所使用的 shell 中环境变量 $PATH 的因素..

你可键入 echo $PATH,来看目前 $PATH 的设定,当我们执行一个指令时,系统会依照这个环境变量来搜寻这个执行档并加以执行..

内定 shell 不会将目前目录 . 加到 $PATH 中,因此如果你想在目前目录执行这个目录下的执行文件而不想加上 ./ 时,可在你的 shell 中将 . 加入..

如一般在 FreeBSD 都是使用 tcsh,你可编辑个人 home 目录下的 .cshrc 檔 set path = ( ........ ,在此加入 . 即可..

记得重新退出 shell 再登录,以便让你的新设定生效.

35、在提示列秀出 [使用者]目前工作目录 (可以少打很多次pwd)

# cd /root
编辑 .cshrc

# set prompt="'hostname -s'# " ### 找到这一行并 mark 起来
set prompt = "[%n]%/# " ### 新增此行并存盘离开

# logout

login: root ....重新登入

ps.其它user id 是否可照办? 我还没试过哩..刚装freebsd而已

36、rm 是 DEL 一个档案
rm -r 是 DEL 整个目录

37、介绍一些一些有用的参数(注意大小写)..

%B 代表高亮度
%n 代表用者名称
%m 主机名称
%/ 目前的目录

一般我们可能比较在意以上几点,如我要管理好几台 FreeBSD 主机,那么以上几个参数就很重要了..

举个实例来说,如我想把我的 prompt 改成这样,而且要高亮度显示:

使用者名称@主机名称[目录名称]>

则 prompt 的设定应该是这样的..

set prompt = "%B%n@%m[%/]> "

注 意 = 左右都要有一个空白,否则会出错,导致无法登入,另外如果要统一所有使用者的 prompt 的话,除了更改 /home/使用者/.cshrc 外,最好也在 /etc/csh.cshrc 中设定这一行,另外如要新增新用者也用这种提示,则请在 /usr/share/skel/dot.cshrc 中也加上(或改成)这一行,这是因为:

tcsh 在使用时会先参考 /etc/csh.cshrc 这个档案,再依 user 去抓其 home 目录下的 .cshrc,而设定 /usr/share/skel/dot.cshrc 是让你新使用者时,自动将其 prompt 设定成 set prompt = "%B%n@%m[%/]> "

这种设法,当你在管理多台主机及切换目录时,就不易搞错而发生一些惨剧