2012年3月3日星期六

教你如何查看linux版本(转)


1. 查看内核版本命令:
1) [root@localhost ~]# cat /proc/version
   Linux version 2.6.18-238.12.1.el5 (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)) #1 SMP Tue May 31 13:23:01 EDT 2011

2) [root@localhost ~]# uname -a
   Linux localhost.localdomain 2.6.18-238.12.1.el5 #1 SMP Tue May 31 13:23:01 EDT 2011 i686 i686 i386 GNU/Linux

3) [root@localhost ~]# uname -r
   2.6.18-238.12.1.el5

2. 查看linux版本:
1) 登录到服务器执行 lsb_release -a ,即可列出所有版本信息,例如:
   [root@localhost ~]# [root@q1test01 ~]# lsb_release -a
   LSB Version:    :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-ia32:printing-4.0-noarch
   Distributor ID: CentOS
   Description:    CentOS release 5.6 (Final)
   Release:        5.6
   Codename:       Final

   注:这个命令适用于所有的linux,包括Redhat、SuSE、Debian等发行版。

2) 登录到linux执行cat /etc/issue,例如如下:
   [root@localhost ~]# cat /etc/issue
   CentOS release 5.6 (Final)
   Kernel \r on an \m

3) 登录到linux执行cat /etc/redhat-release ,例如如下:
   [root@localhost ~]# cat /etc/redhat-release
   CentOS release 5.6 (Final)

4) 登录到linux执行rpm -q redhat-release ,例如如下:
   [root@q1test01 ~]# rpm -q redhat-release

2012年3月2日星期五

Package Management

1.Package Management on Red hat Linux
   1.1 Yellowdog Updater Modified (Yum)

         • Options Available with Yum
            option        description
            search        Allows you to search for packages available for download
            list              Lists all the packages available for download
            install          Installs a package or packages
            check-update Lists packages available for update
            update        Updates the package specified or downloads and installs any available updates
            remove       Removes a package or packages from your host

            info             Provides package information about a package
            provides     Tells you what package a particular file or feature belongs to
            list ­updates   Lists all the packages with updates available only
            list ­available  Lists all the available packages only
            localinstall    Installs a locally downloaded RPM package
            clean ­all        Cleans up downloaded package files that are no longer needed

   1.2 Configuring Yum

     Yum has its configuration files stored under /etc/yum.conf and /etc/yum.repos.d/, and it stores state files in the directories /var/lib/yum and /var/cache/yum.

     You will not normally need to make changes to the default /etc/yum.conf. An example of when you might want to change configuration is to add a proxy server that Yum should use when downloading packages. The defaults will generally suit most environments. A full list of the available configuration settings for Yum is available through the yum.conf­ man page.
          $­man ­yum.conf

     The files defining what repositories are available to Yum are contained in the /etc/yum.repo.d directory. Let’s look at a generic repository file:
       $­cat­/etc/yum.repo.d/myrepo.repo
       [myrepo]
       name=myrepo
       baseurl=http://myrepo.mydomain.com/pub/linux/releases/$releasever/$basearch/os/
       enabled=1
       gpgcheck=1
       gpgkey=http://myrepo.mydomain.com/linux/RPM-GPG-KEY-linux

        option         description

        [repo-id]     The repo_id is the unique name for the repository.
        name           The name is a description of the repository.
        baseurl        The baseurl is the URL to the repository. This is generally an HTTP URL, much like
                           you would use in a web browser.
        enabled       You can enable or disable the package by specifying 0 for disabled and 1 for enabled.
        gpgcheck    This option tells Yum to check the GPG keys used to “sign” packages so that you can
                           be sure the package hasn’t been tampered with. Specifying 0 turns off checking and
                           1 indicates checking is on.
        gpgkey       This is the URL where Yum should find the repository’s GPG key.

   1.3 Red Hat Package Management (RPM)
         • Options Available with rpm

           options and Flags        description
            -q­|­­--­query                  Allows you to query the RPM database and find information about installed
                                              packages on your host
            -i­|­­--­install                    Installs a local package on your host
            -e­|­­--­erase                   Removes a package from your host
            -U­|­­--­upgrade              Upgrades an existing package or installs a package on your host if it is not
                                              already installed
            -F­|­­--­freshen                Upgrades a package only if an earlier version is installed
            -V­|­­--­verify                  Verifies an installed package


            #­rpm ­­--­query ­­--­all
         This command, which uses the ­--­query flag together with the ­--­all flag, indicating that you are querying all packages, lists all the packages installed.

   1.4 Compiling from Source
         • Configure
            $ ./configure
         • Compile and Make
            # make
         • Install
            $ sudo­ make ­install

         • Uninstall
            $­sudo­ make ­uninstall












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