2012年3月2日星期五

Users and Groups

1.Introducing sudo
   On Red Hat, the sudo command is not enabled by default, and you’ll need to enable it. To do this, you need to use a command called visudo to edit the sudo command’s configu-ration file, /etc/sudoers. To do this, you need to log on as the root user and run the visudo command.
          # visudo


   把下面这行注释去掉,This enables any member of a group called wheel to use the sudo command.
          # %wheel  ALL=(ALL)       ALL
   用下面的命令,把ataylo用户添加到wheel组里,这样ataylor就可以使用sudo命令了。
          # usermod –G wheel ataylor

2.Creating Users
   $ sudo useradd –m –c 'John Smith' jsmith

   Option Description
     -c      Add a description of the user
     -d      homedir The user’s home directory
     -m     Create the user’s home directory
     -M    Do not create the user’s home directory (Red Hat only)
     -s      shell Specify the shell the user will use

   2.1 User Default Settings

     On both Red Hat and Ubuntu distributions, the defaults are contained in the /etc/default/useradd file, and you can display the current defaults using the following command:
        $ sudo /usr/sbin/useradd -D 或者 $ sudo cat /etc/default/useradd
     内容如下
     # useradd defaults file
     GROUP=100
     HOME=/home
     INACTIVE=-1
     EXPIRE=
     SHELL=/bin/bash
     SKEL=/etc/skel


     Option     Description
     SHELL    The path to the default shell
     HOME    The path to the user’s home directory
     SKEL      The directory to use to provide the default contents of a user’s new home directory
     GROUP  The default group ID
     INACTIVE   The maximum number of days after password expiration that a password can be changed
     EXPIRE   The default expiration date of user accounts


     The useradd -D Defaults(例:$ sudo useradd -D -s /bin/bash)
     Option      Description
      -b            path/to/default/home Specifies the path prefix of a new user’s home directory
      -e            date Specifies the default expiration date
      -f             days Specifies the number of days after a password has expired before the account will be disabled
      -g            group Specifies the default group
      -s            shell Specifies the default shell

   2.2 Creating Groups
   So how do we tell what groups our new user belongs to? To check the details of a particular user, we can use the id command.
     $ id jsmith
     uid=1003(jsmith) gid=1003(jsmith) groups=1003(jsmith)

    • create new groups

     $ sudo groupadd printing
     $ sudo groupadd finance
    • create users to some groups
     $ sudo useradd -m -c 'Anne Taylor' -G printing,finance ataylor

   2.3 Deleting Users and Groups
    • delete a user
      $ sudo userdel ataylor

     You can force Linux to delete the user’s home directory using the –r option of the userdel command. This will delete the /home/username directory and all files in it, but it won’t delete any files outside of this directory that might also belong to the user.

     But if you do decide to delete a user, you can run the command find / -user UID –o –group GID to find all the files associated with the user you have just deleted.


    • delete a group
      $ sudo groupdel finance

   2.4 Passwords
      $ passwd
      $ sudo passwd jsmith

   2.5 Password Aging
      $ sudo chage -M 30 ataylor

   2.6 Disabling Users

     As the root user, you can also use the passwd command to disable and enable user accounts using the –l, or lock, option. For example, consider the following:
      $ sudo passwd –l ataylor
     You can then unlock the user using the –u, or unlock, option.
      $ sudo passwd –u ataylor

     This doesn’t lock a user out but disables the user’s getting shell access.
      $ sudo usermod –s /sbin/nologin

   2.7 Storing User and Group Data

     The first file, /etc/passwd, contains a list of all users and their details.   Listing 4-12 shows examples of some passwd entries.

      Listing 4‑12. /etc/passwd Entries
      root:x:0:0:root:/root:/bin/bash
      daemon:x:2:2:daemon:/sbin:/sbin/nologin

     Each entry can be broken into its component pieces, separated by a colon.
      username:password:UID:GID:GECOS:Home Directory:Shell
                                                       ↑备注,说明

     On Linux hosts, information about groups is stored in the /etc/groups file.   Listing 4-13 shows a sample from this file.
      Listing 4‑13. Sample of the /etc/groups File
      root:x:0:root
      ataylor:x:501:finance,printing
     The /etc/group file is structured much like the /etc/passwd file, with the data separated by a colon. The file is broken into a group name, a password, the GID, and a   comma-  separated list of the members of that group.
      groupname:password:GID:member,member
                                                 ↑组下面的用户

   2.8 Configuring Your Shell and Environment
      • Environment Variables
        Name        Used For
        HOME      The user’s home directory
        LANG       Defines which language files applications should use
        LS_COLORS      Defines colors used by the ls command
        MAIL        The location of the user’s mailbox
        PATH        A colon-separated list of directories where shells look for executable files

        PS1           Defines the normal prompt
        SHELL      The current shell
        _                Contains the last command executed in this session

      • Command aliases
         $ alias rm='rm -i'
         $ unalias rm
         $ alias
















Linux Basics

0.Others
    • 启动到text模式
      修改/etc/inittab文件,将id:5:initdefault:一行中的5改为3
    • 启动到xwindow模式   
     init 5
1.Getting Help
    • man ls
    • ls --help
    • man -K user
         This would search all man pages for the keyword user and return a list of all man pages con-taining
         the term. You will then be prompted to view each page that is returned, skip a page and go to the next
         page, or quit the search.
    • whatis useradd
    • apropos­ whoam
         The apropos command also searches the whatis database but searches for strings rather than complete
         words.

2.Users and Groups
    • Users
         /etc/passwd
    • Groups
         /etc/group

3.Services and processes
    • ps -A
          ps command with the -A flag (for all) to list all the processes currently running on our host.
    • top
          The top command starts an interactive monitoring tool that updates every few seconds with the top
           running processes on your host.

4.Files and File Systems
    • pwd
    • linux directory structure

         Directory Description
         /bin/       User commands and binaries.
         /boot/     Files used by the boot loader. (We talk about boot loaders in Chapter 5.)
         /dev/      Device files.
         /etc/       System configuration files.
         /home/   User’s home directories.
         /lib/        Shared libraries and kernel modules.
         /media/   Removable media is usually mounted here (see Chapter 8).
         /mnt/      Temporary mounted file systems are usually mounted here (see Chapter 8).
         /opt/       Add-on application software packages.
         /proc/     Kernel and process status data is stored in here in   text-  file format.
         /root/      The root user’s home directory.
         /sbin/      System binaries.
         /srv/        Data for services provided by this host.
         /tmp/       Directory for temporary files.
         /usr/        User utilities, libraries, and applications.
         /var/        Variable or transient files and data, for example logs, mail queues, and print jobs.
 
    • ls(­ls­-la)
         the l and a switches have been added to the ls command. The l switch, which is an abbreviation of
         long, uses a long listing format, which as you can see shows a lot more information. The a switch tells
         ls to list all files and directories, even hidden ones.

             -rw-r--r--­­­1­root­­root­­­­­­0­­2008-07-­15­20:47­.autofsck
             drwxr-xr-x­­­2­root­­root­­­4096­­2008-05-­18­04:11­bin

        Each line of the listing contains seven pieces of information about each object:
       - Unix file type
       - Permissions
       - Number of hard links
       - User and group ownership
 - Size
 - Time and date
 - Name

   4.1 File Types and Permissions
        • File types
           Type Description
           -        File
           d        Directory
           l         Link
           c        Character devices
           b        Block devices
           s        Socket
           p        Named pipe

        • Permissions
          Each file on your host has three classes of permissions:
    - User
    - Group
    - Other (everyone else)

       

          With chmod, each class is abbreviated to a single letter:
          u: User
          g: Group
          o: Other or everyone
          a: All

          chmod ­u+x­ myfile
          ­chmod­­ u-­x,og+w­ myfile
          chmod­ 654 ­myfile
          
          行动: 
            + 表示添加权限 
             - 表示删除权限 
            = 表示使之成为唯一的权限 


          We can also apply the permissions of one class to another class by using the = symbol:

            ­chmod­ u=g ­myfile --用户和组的权限一样

         

          You can also set permissions for multiple files by listing each file separated by space like so:
            ­chmod­ u+r ­file1 ­file2 ­file3
          
          You can also use the asterisk symbol to specify all files and add the -R switch to recurse into lower 
          directories like so:
            chmod­ -R ­u+x ­/usr/local/bin/*

          其中:rwx也可以用数字来代替 
             r ------------4 
             w -----------2 
             x ------------1 
             - ------------0 

         当大家都明白了上面的东西之后,那么我们常见的以下的一些权限就很容易都明白了: 
            -rw------- (600) 只有所有者才有读和写的权限 
            -rw-r--r-- (644) 只有所有者才有读和写的权限,组群和其他人只有读的权限 
            -rwx------ (700) 只有所有者才有读,写,执行的权限 
            -rwxr-xr-x (755) 只有所有者才有读,写,执行的权限,组群和其他人只有读和执行的权限 
            -rwx--x--x (711) 只有所有者才有读,写,执行的权限,组群和其他人只有执行的权限 
            -rw-rw-rw- (666) 每个人都有读写的权限 
            -rwxrwxrwx (777) 每个人都有读写和执行的权限

   4.2 Users, Groups, and Ownership
        use the chown command to change user and group ownership.
          chown ­jsmith ­myfile
          chown ­jsmith:admin­ myfile
          chown­-R ­jsmith:admin ­/home/jsmith/*

   4.3 Size and Space
         • ­ls­-lh
              -rw-rw-r--­­1­jsmith­jsmith­­51K­­2008-08-­17­23:47­myfile
           On the previous line, you can see that the myfile file is 51 kilobytes in size.

         • du­-sh /usr/local/bin
            ­  4.7M ­­­­­/usr/local/bin
            The -s switch summarizes the total, and the -h switch displays the size in a   human-  readable form.

         • df -h
            This command displays all of your disks and storage devices and the free space present on them.
            We’ve executed the command and added the -h switch, which returns   human-  readable sizes.

   4.4 Date and Time
         • ­ls­-lu
           display the last accessed time for a file by listing it with the -u switch

         • ­ls­-lc
           list creation dates by using the -c switch

         • date
           Using date on the command line without any options will return the current time and date like so:
              Tue ­Aug­ 19­13:01:20 ­EST ­2008

5.Working with Files
   5.1 Reading Files
   ­• cat ­/etc/hosts
     The cat command is so named because it “concatenates and prints files
   ­• less ­/etc/services

     From inside the less interface, you can scroll through the file.
       - spacebar : 下一页
       - Enter key: 下一行To scroll backward,
       - B key     : 上一行(也可以用上下键)
       - Q key    : 退出
   ­• ­grep­ localhost­ /etc/hosts
     you can see a very simple grep search for the string localhost in the file /etc/hosts.
       $­grep­localhost­/etc/hosts
         127.0.0.1 ­­­­­­­­­­­­­­­localhost.localdomain­ localhost ­localhost
     You can also recursively search down into lower directories by adding the -r switch like so:
       $­grep ­-r­ localhost­ /etc

   5.2 Searching for Files
   ­• ­find ­/home/­ -type ­f­ -iname ­myfile*
     The -iname option searches for a   case-  insensitive pattern, in this case, all files starting with myfile.
   ­• ­find ­/home/­ -type ­f­ -iname ­myfile* -exec grep mail {} \;
   • ­find ­/ ­-nouser ­-o­ -nogroup  
     This command, run as root, will search the whole directory tree for any files that don’t belong to a valid
     user or group.

   5.3 Copying Files
   ­• cp ­-i­ /home/jsmith/*.c ­./
     We’ve also added the -i switch to make sure we’re prompted if a file already exists.
   ­• ­cp ­-r ­/home/jsmith ­/backup
     The previous line copies the /home/jsmith directory and all files and directories beneath it to the /backup
     directory.
   ­• ­cp ­-p ­/home/jsmith ­/backup
     Lastly, when copying files using the cp command, some items about the file, such as dates, times, and
     permissions, can be changed or updated. If you want to preserve the original values on the copy, you can
     use the -p switch.
   ­• ­cat ­/home/jsmith/myfile ­> ­/home/jsmith/yourfile
     The use of the > symbol sends the output from one command to the command or action on the other side
     of the > symbol. In this case, the output of the cat command is redirected into a file called yourfile.
     If this file doesn’t exist, it will be created. If it does exist, its content will be overwritten.
   ­• ­cat ­/home/jsmith/myfile ­>> ­/home/jsmith/yourfile
     Using the >> syntax will append the output from the cat of myfile to the end of yourfile.
     If yourfile does not exist, it will be created.

   5.4 Moving and Renaming Files
   ­• ­mv ­-i­ ~/myfile ­~/mynewfile
      The -i option again ensures we get prompted if the target file already exists. You can also rename files in
      place with the mv command:

   5.5 Deleting Files
   • rm­ -fr ­/home/jsmith/backup

   5.6 Linking Files
   • ­ln­ -s ­/home/jsmith/myfile
     The previous line would create a symbolic link called myfile to the /home/jsmith/myfile file.

   5.7 Linking Files
   • vi file

vim 替换^M(转)


【原因】
回车表示:dos中CRLF 回车(CR, ASCII 13, r) 换行(LF, ASCII 10, n),两个符号。linux中只有一个。
在Linux下使用vi来查看一些在Windows下创建的文本文件,有时会发现在行尾有一些“^M”。

【処理方法】
1.使用dos2unix命令。一般的分发版本中都带有这个小工具,使用起来很方便:
$ dos2unix myfile.txt
2.使用vi的替换功能。启动vi,进入命令模式,输入以下命令:
:%s/^M$//g # 去掉行尾的^M。
:%s/^M//g # 去掉所有的^M。
:%s/^M/[ctrl-v]+[enter]/g # 将^M替换成回车。
:%s/^M/r/g # 将^M替换成回车。

注意:这里的“^M”要使用“CTRL-V CTRL-M”生成,而不是直接键入“^M”。

2012年2月28日星期二

OpenVPNのインストール(转)


CentOS5でOpenVPNサーバを構築し、Windowsクライアントからアクセスする方法です。

OpenVPNのインストール
まずOpenVPNのインストールをしなくてはいけません。

CentOS標準のリポジトリにはOpenVPNが無いので、下記配布サイトからRPMインストールしようとしたができませんでした。
http://dag.wieers.com/rpm/packages/openvpn/

で、RPMforge というリポジトリにopenvpnあるようなので、yumで使えるようにします。
まず、リポジトリの優先順位を設定するために、yum-priorities をインストールします。
[root@localhost pluginconf.d]# yum install yum-priorities

そして、RPMforge を http://packages.sw.be/rpmforge-release/ からダウンロードし、リポジトリのインストール、RPMforgeのGPGキーインストールをします。
wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.1-1.el5.rf.i386.rpm
[root@localhost installsoft]# rpm -ihv rpmforge-release-0.5.1-1.el5.rf.i386.rpm
rpm --import rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt


CentOS Baseのリポジトリ優先度を1にします。(rpmforgeよりも優先にするため)
# vi /etc/yum.repos.d/CentOS-Base.repo
 ↓[base]や[updates],[addons],[extras]などそれぞれに下記行を追加します。
priority=1

RPMforgeのリポジトリの優先を CentOS Baseのリポジトリ優先度より低くします。(今回は10)
[root@localhost yum.repos.d]# vi /etc/yum.repos.d/rpmforge.repo
### Name: RPMforge RPM Repository for Red Hat Enterprise 5 - dag
### URL: http://rpmforge.net/
[rpmforge]
name = Red Hat Enterprise $releasever - RPMforge.net - dag
baseurl = http://apt.sw.be/redhat/el5/en/$basearch/rpmforge
mirrorlist = http://apt.sw.be/redhat/el5/en/mirrors-rpmforge
#mirrorlist = file:///etc/yum.repos.d/mirrors-rpmforge
enabled = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag
gpgcheck = 1
priority=10 ←追加


これで、下記のように yum より openvpn のインストールを行います。(リポジトリの優先度順にインストールパッケージがあるかチェックしていってるようです)
[root@localhost yum.repos.d]# yum install openvpn

2012年2月19日星期日

Mounting cdrom in cent os 5 linux

CDRROM, type as root
Code:
mount /dev/cdrom /mnt
For DVD
Code:
mount /dev/dvd /mnt

2011年11月28日星期一

使用SELECT * 语句的缺点(转)


使用SELECT * 语句的缺点
1、SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否有用,这会对服务器资源造成浪费,甚至会对服务器的性能产生一定的影响。
2、如果表的结构在以后发生了改变,那么SELECT * 语句可能会取到不正确的数据甚至是出错。使用字段名可以快速定位影响范围。
3、执行SELECT * 语句时,SQL Server首先要查找出表中有哪些列,然后才能开始执行SELECT * 语句,这在某些情况会产生性能问题。
4、使用SELECT * 语句将不会使用到覆盖索引,不利于查询的性能优化。
5、在文档角度来看,SELECT * 语句没有列明将要取出哪些字段进行操作,所以也是不推荐的。

2011年10月27日星期四

给胡XX、温XX的信

给胡XX、温XX的信:7 ~5 k8 a# A% X
主X、总X;
    你们好!   
    据国家有关当局统计,改革开放三十年来,升值最快的是住房、墓地、乌纱帽、月饼和二奶。贬值最快的是职称、文凭、道德、诚信和人民币。
   中国已初步建设成为一个由月光族、啃老族、打工族、蜗居族、蚁族、牢骚族、抱怨族、行骗族、逐利族和隐婚族组成的多民族国家。
    这是一个给力的一年,这是一个杯具的一年,这也是一个纠结的一年。
    这一年,江西的一个钉子户用最无奈的方式结束了自己的生命,他的死证明不了有关部门的野蛮与无耻,只能证明汽油一点就着。
    这一年,8名香港游客死在了菲律宾警察的枪口下,让香港人知道菲律宾除了有“菲佣”,还有“菲警”。
    这一年,一个卖汽车的和一个卖运动鞋的在关于谁“给力”和谁“不给力”的问题上发生了分歧从而引发了“3Q大战”,最后“狗日的腾讯”做出了“一个艰难的决定”,末了才知道这俩孙子何止是“坑爹”,简直就是“坑爹”。  
    这一年,李刚没有出名,李刚的儿子也没有出名,但是李刚的儿子的爸爸出名5 j3 Z- H; e& q* J$ h: }0 A
了。   
    最近,国家某部公布了一项统计数据,告诉人们:你要不是三大式人物(大款,大官,大腕)而想在北京买套100平方米总价300万的房,社会阶层所付出的代价请看:
1 农民:种三亩地每亩纯收入400元的话要从唐朝开始至今才能凑齐(还不能有灾年);7 ?/ D1 |" L" b2 S& Z! O
, j9 K# |& U; b5 Q
2 工人:每月工资1500元需从鸦片战争上班至今(双休日不能休);
3 白领:年薪6万,需从1960年上班就拿这么多钱至今不吃不喝(取消法定假日);+ L9 w! H4 l. p. i; l/ F1 y
4 抢劫犯:连续作案2500次(必须事主是白领)约30年。8 G9 [6 j) M, U8 `' q
5:妓女:连续接客10000次,以每天都接一次客,需备战10000天,从18岁起按此频率接客到46岁(中间还不能来例假)  c& d7 ^) q8 L3 J( Q5 M4 v
以上还不算装修、家具、家电等等费用。
    中国的现状(经典)% V- T9 C5 Q6 s( i0 S) j
1、ATM取出假钱--->银行无责
2、网上银行被盗--->储户责任
3、银行多给了钱--->储户义务归还
4、银行少给了钱--->离开柜台概不负责. q' H$ l* Q+ T$ Y% N" x
5、ATM机出现故障少给钱--->用户负责
6、ATM机出现故障多给钱--->用户盗窃
7、广东开平银行行长贪污4亿--->判2年: R0 k) V9 \- R# S; D% j# H
8、ATM多吐7万给老百姓许霆--->判无期
    声明:转发不
会送人民币,但如果觉得作为消费者,全中国十三亿储户不公平的,请转发 ,有多少群转发多少群 。- m# G3 d" c# Z! T: g* U- P
    中国现状:2 j7 c/ k( [- n1 \
生不起,剖腹一刀五千起;* P0 U! o( e. P+ q# F$ P; q# A
读不起,选个学校三万起;
住不起,二万多元一平米;
老婆不是娶不起,没房没车谁嫁你
养不起,父母下岗儿下地;4 H5 ^$ j: K  Q; L7 O
病不起,药费利润十倍起;0 d2 H  E/ x4 K& }5 u( L+ W
活不起,一月辛劳一千几;* l9 M- K3 i7 i% F  F- @5 z
死不起,火化下葬三万几。
    总结(八个大字): 求生不得,求死不能, f' T- U2 B! n# H
    多传传,让领导人们看看, 就不信传不到中央胡书记与温总理那里去。 有群的都转发 加一句:谁看到谁最少转发一个群,转发2个以上群的,愿他买彩票中500万 。; d( ]% @+ i: t, v8 f0 a$ T8 I
教育:希望进去,绝望出来;  
房产:蜗居进去,房奴出来;( m% n! X: D4 N8 v. `6 O' i
演艺:玉女进去,小姐出来;* I0 s# ?+ k7 \% R" |( B
信访:窦娥进去,疯子出来;
官场:海瑞进去,和绅出来;6 C: F0 e. s/ Y: S
煤窑:蹲着进去,躺着出来;
大学:校花进去,残花出来!
股市:杨百万进去,杨白劳出来;宝马进去,自行车出来;西装革履进去,三角裤出来。3 B$ ?1 v! e# T, ]
    老百姓在思考,为啥玩不过政府呢; ]- g- O4 a- d8 g8 s
原因如下:
1、你和他讲道理,他和你耍流氓;
2、你和他耍流氓,他和你讲法制;' D+ }5 A8 l: ~  H9 o& u9 z* o9 o
3、你和他讲法制,他和你讲政治;9 a1 s: b: i( F; }  E: Q& B8 k
4、你和他讲政治,他和你讲国情;; d3 H2 N! E0 j/ @0 ^( W& V
5、你和他讲国情,他和你讲接轨;+ X0 ~$ U$ I9 d% r) z
6、你和他讲接轨,他和你讲文化;
7、你和他讲文化,他和你讲孔子;; N' r0 r6 b7 b  d
8、你和他讲孔子,他和你讲老子;
9、你和他讲老子,他给你装孙子!

用奶粉灭掉00后,. X* k# X6 s4 v  u# w" l
用考试灭掉90后,/ q1 u  U2 f& t
用房价灭掉80后," q5 M2 }( c  b9 [
用失业灭掉70后,- C" _% C; Q& F" T
用城管灭掉60后,
用下岗灭掉50后,
用拆迁灭掉40后,
用医改灭掉30后,
……最后,活着且活得很好的人都去开两会。
有人问:“啥是两会呢”
农民代表答:会养猪、会交配。
工人代表答:会挣钱、会消费。$ |; n# \% A  i; a4 U) _
民工代表答:会讨薪、会下跪。* ?' V% o  O- B7 e3 h; i. s. M
保姆代表答:会插足、会叠被。( h. C+ L7 }1 h3 y7 ~
艺人代表答:会炒作、会陪睡。
商人代表答:会赚钱、会逃税。, c6 B) }& i1 b1 T3 g. i( e
官员代表答:会撒谎、会受贿。( G0 `$ |& H( Q; J- p5 c4 E2 J6 C8 f7 L
股民代表答:会割肉,会流泪。
革命工作苦啊:
反应慢的会被玩死;
能力差的会被闲死;
胆子小的会被吓死;
酒量小的会被灌死;! x" J+ }1 {4 ]& r7 N
身体差的会被累死;; q) v. N2 s: C# X
讲话直的会被整死;: w' a2 l$ U* E8 H
能干活的会被用死。) ^  w- {6 ~9 t! f
所以呀:
人不能太敬业了。
董存瑞拿得太稳了;  n/ p+ h' T' \5 A; d
刘胡兰嘴巴太紧了;) @8 h  V- j  l
邱少云趴得太死了;
黄继光扑得太准了;
张思德跑得太晚了;
白求恩会得太多了。6 |1 \  y/ m' j6 S" S- @  W* l
教训呀,
心态好才是最重要的!2 i5 H1 z) e; `9 _4 j$ C. c% Y
不然人在天堂,: z# \2 r4 B0 }0 I$ N2 {
钱在银行