首页 > 代码库 > Linux安装与基础命令

Linux安装与基础命令

  1. 首先准备一个liunx镜像和一台安装好虚拟机的电脑。

  2. 配置好虚拟机,选在稍后安装。选择镜像文件,开启虚拟机进行安装。

  3. 配置yum源。首先插入光盘。由于是虚拟机,用的光盘镜像,

    在/mnt/下面创建一个文件夹yum 。      复制所有光盘里的内容到/mnt/yum   cp -rf /media/RHEL_5.9\ x86_64\ DVD/*  /mnt/yum

  4. 用vim编辑器配置文件   vim /etc/yum.repo.d/rhel-debuginfo.repo

    1. 配置如下:


      [rhel]

      name=Red Hat Enterprise Linux

      baseurl=file:///mnt/yum/Server

      enabled=1

      gpgcheck=0

      gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

  5. 查看yum配置情况

    root@localhost ~]# yum repolist

    rhel                                                   | 1.5 kB     00:00     

    rhel/primary                                           | 932 kB     00:00     

    rhel                                                                3335/3335

    repo id                    repo name                                    status

    rhel                       Red Hat Enterprise Linux                     3,335


    yum配置成功。

6。查看当前主机名

    [root@localhost ~]# hostname 

    localhost.localdomain

7。修改主机名

    [root@localhost ~]# hostname weiliyang.nsd1405.com

    [root@localhost ~]# hostname 

    weiliyang.nsd1405.com

8.查看当前ip地址

    

    [root@localhost ~]# ifconfig eth0

    eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:29:F0  

              inet6 addr: fe80::20c:29ff:fe55:29f0/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

              RX packets:117 errors:0 dropped:0 overruns:0 frame:0

              TX packets:27 errors:0 dropped:0 overruns:0 carrier:0

              collisions:0 txqueuelen:1000 

              RX bytes:11674 (11.4 KiB)  TX bytes:5927 (5.7 KiB)

9.修改主机ip地址,临时设置为192.168.1.10

    [root@localhost ~]# ifconfig eth0 192.168.1.10/24 

    [root@localhost ~]# ifconfig eth0

    eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:29:F0  

              inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0

              inet6 addr: fe80::20c:29ff:fe55:29f0/64 Scope:Link

              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

              RX packets:117 errors:0 dropped:0 overruns:0 frame:0

              TX packets:38 errors:0 dropped:0 overruns:0 carrier:0

              collisions:0 txqueuelen:1000 

              RX bytes:11674 (11.4 KiB)  TX bytes:8275 (8.0 KiB)

10.查看cpu信息  

    [root@localhost ~]# cat /proc/cpuinfo 

    processor       : 0

    vendor_id       : GenuineIntel

    cpu family      : 6

    model           : 37

    model name      : Intel(R) Core(TM) i3 CPU       M 350  @ 2.27GHz

    stepping        : 2

    cpu MHz         : 2261.002

    cache size      : 3072 KB

11.查看内存信息

    [root@localhost ~]# cat /proc/meminfo 

    MemTotal:       507508 kB

    MemFree:         39700 kB

    Buffers:          5256 kB

    Cached:         187540 kB

    SwapCached:          0 kB

    Active:         293652 kB

    Inactive:       105868 kB

12.查看系统具体属于RedHat哪一个版本

[root@localhost ~]# lsb_release  -d

    Description:    Red Hat Enterprise Linux Server release 5.9 (Tikang

13.查看当前系统时间   

    [root@localhost ~]# date

    2014年 07月 06日 星期日 17:22:52 CST

14.列出/etc目录属性

    drwxr-xr-x 102 root root 12288 07-06 16:43 /etc/


15.递归显示/boot下的目录和文件   

    [root@localhost ~]# ls  -R /boot/

    /boot/:

    config-2.6.18-348.el5      lost+found                 vmlinuz-2.6.18-348.el5

    grub                       symvers-2.6.18-348.el5.gz

    initrd-2.6.18-348.el5.img  System.map-2.6.18-348.el5


    /boot/grub:

    device.map     grub.conf         minix_stage1_5     stage2

    e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5

    fat_stage1_5   jfs_stage1_5      splash.xpm.gz      vstafs_stage1_5

    ffs_stage1_5   menu.lst          stage1             xfs_stage1_5


    /boot/lost+found:

15.显示root下面所有文件包括隐藏文件   

    [root@localhost ~]# ls -a /boot/

    .                          lost+found

    ..                         symvers-2.6.18-348.el5.gz

    config-2.6.18-348.el5      System.map-2.6.18-348.el5

    grub                       vmlinuz-2.6.18-348.el5

    initrd-2.6.18-348.el5.img  .vmlinuz-2.6.18-348.el5.hmac

16.进入/tmp目录,删除所有文件和目录,创建file1.txt file2.txt file3.txt file13.txt filea.txt            fileab.txt

    [root@localhost tmp]# cd /tmp/

    [root@localhost tmp]# rm -rf *

    [root@localhost tmp]# touch file1.txt file2.txt file3.txt file13.txt filea.txt          fileab.txt

    [root@localhost tmp]# 

    [root@localhost tmp]# ls

    file13.txt  file1.txt  file2.txt  file3.txt  fileab.txt  filea.txt

17.显示file开头的,以.txt结尾的,中间2个字符的文件

    [root@localhost tmp]# ls file??.txt

    file13.txt  fileab.txt

    [root@localhost tmp]# 

18.显示file开头的,以.txt结尾的,中间是单个数字的文件

    [root@localhost tmp]# ls file[0-9].txt

    file1.txt  file2.txt  file3.txt

19.显示file开头的,以.txt结尾的,中间部分可能是1 3 a ab的文件

    [root@localhost tmp]# ls file{1,3,a,ab}.txt

    file1.txt  file3.txt  fileab.txt  filea.txt

20.查看/boot和/etc/pki分别占用多大空间

    [root@localhost tmp]# du -sh  /root/

    385M    /root/

    

    [root@localhost tmp]# du -sh /etc/pki/

    792K    /etc/pki/

21.查看/etc/passwd前5行   

    [root@localhost tmp]# head -5 /etc/passwd

    root:x:0:0:root:/root:/bin/bash

    bin:x:1:1:bin:/bin:/sbin/nologin

    daemon:x:2:2:daemon:/sbin:/sbin/nologin

    adm:x:3:4:adm:/var/adm:/sbin/nologin

    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

22.查看/etc/passwd尾5行   

    [root@localhost tmp]# tail -5 /etc/passwd

    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

    avahi-autoipd:x:100:159:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin

    gdm:x:42:42::/var/gdm:/sbin/nologin

    sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

    weiliyang:x:500:500::/home/weiliyang:/bin/bash

23.查看/etc/passwd的第8-12行   

    [root@localhost tmp]# head -12 /etc/passwd|tail -5 /etc/passwd

    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

    avahi-autoipd:x:100:159:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin

    gdm:x:42:42::/var/gdm:/sbin/nologin

    sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

    weiliyang:x:500:500::/home/weiliyang:/bin/bash

24.统计系统中有多少个账户

    [root@localhost tmp]# cat -n /etc/passwd|wc -l

    35

25.计算/etc目录下.conf配置文件的个数

    [root@localhost tmp]# ls /etc/*.conf|wc -l

    48

26.显示/etc/passwd中以root开头的内容

    [root@localhost tmp]# grep ^root /etc/passwd

    root:x:0:0:root:/root:/bin/bash

27.显示/etc/passwd中以bash结尾的内容

    [root@localhost tmp]# grep bash$ /etc/passwd

    root:x:0:0:root:/root:/bin/bash

    weiliyang:x:500:500::/home/weiliyang:/bin/bash

28.分别使用gzip和bzip2和zip对/root/gztest.txt进行压缩和解压

        gzip压缩

    [root@localhost ~]# ls

    anaconda-ks.cfg  Desktop  gztext.txt  install.log  install.log.syslog  lianxi

    [root@localhost ~]# gzip -9 gztext.txt 

    [root@localhost ~]# ls

    anaconda-ks.cfg  gztext.txt.gz  install.log.syslog

    Desktop          install.log    lianxi

    

        gunzip解压

    [root@localhost ~]# gunzip gztext.txt.gz 

    [root@localhost ~]# ls

    anaconda-ks.cfg  Desktop  gztext.txt  install.log  install.log.syslog  lianxi

    

        gzip2压缩   

    [root@localhost ~]# ls

    anaconda-ks.cfg  Desktop  gztext.txt  install.log  install.log.syslog  lianxi

    [root@localhost ~]# bzip2 gztext.txt 

    [root@localhost ~]# ls

    anaconda-ks.cfg  gztext.txt.bz2  install.log.syslog

    Desktop          install.log     lianxi

        gzip2解压

    [root@localhost ~]# bunzip2 gztext.txt.bz2 

    [root@localhost ~]# ls

    anaconda-ks.cfg  Desktop  gztext.txt  install.log  install.log.syslog  lianxi

        tar压缩

    

    [root@localhost ~]# ls

    anaconda-ks.cfg  Desktop  gztext.txt  install.log  install.log.syslog  lianxi

    [root@localhost ~]# tar -zcf gztext.txt.tar.gz gztext.txt 

    [root@localhost ~]# ls

    anaconda-ks.cfg  gztext.txt         install.log         lianxi

    Desktop          gztext.txt.tar.gz  install.log.syslog

        tar解压

    [root@localhost ~]# tar -zxf gztext.txt.tar.gz 

    [root@localhost ~]# ls

    anaconda-ks.cfg  gztext.txt         install.log         lianxi

    Desktop          gztext.txt.tar.gz  install.log.syslog

29.把/etc/mail打包并压缩到/root/mail.tar.gz

    [root@localhost ~]# tar -zcf /root/mail.tar.gz /etc/mail

    [root@localhost ~]# ls /root/

    anaconda-ks.cfg  -D       gztext.txt         install.log         lianxi

    -d               Desktop  gztext.txt.tar.gz  install.log.syslog  mail.tar.gz

30,把/etc/mail打包并压缩到/root/mail.tar.bz2   

    [root@localhost ~]# tar jcf /root/mail.tar.bz2 /etc/mail

    [root@localhost ~]# ls /root/

    anaconda-ks.cfg  -D       gztext.txt         install.log         lianxi

    -d               Desktop  gztext.txt.tar.gz  install.log.syslog  mail.tar.bz2

31.将mail.tar.gz解压到/tmp下,递归查看/tmp/etc下的内容,然后删除/tmp/etc目录

    [root@localhost ~]# tar -zxf mail.tar.gz -C /tmp/

    递归查看/tmp/etc

    [root@localhost ~]# ls -R /tmp/

     /tmp/etc:

    mail

    

    /tmp/etc/mail:

    access          helpfile          Makefile     submit.mc

    access.db       local-host-names  sendmail.cf  trusted-users

    domaintable     mailertable       sendmail.mc  virtusertable

    domaintable.db  mailertable.db    submit.cf    virtusertable.db

    [root@localhost ~]# rm -rf /tmp/etc/

    [root@localhost ~]# ls -R /tmp/

    /tmp/:

    file13.txt  file2.txt  fileab.txt  scim-bridge-0.3.0.lockfile-0@localhost:0.0

    file1.txt   file3.txt  filea.txt

32.分别查看mail.tar.gz与mail.tar.bz2文件里面内容

    [root@localhost ~]# file mail.tar.gz 

    mail.tar.gz: gzip compressed data, from Unix, last modified: Sun Jul  6 19:38:22 2014

    [root@localhost ~]# file mail.tar.bz2 

    mail.tar.bz2: bzip2 compressed data, block size = 900k

33.

    创建账户

                student

    stu01,宿主目录设为/opt/stu01

        [root@localhost ~]# useradd  -d /opt/stu01 stu01 

    stu02,uid为10001,账户在2015-06-30号过期,基本组设为stu01

        [root@localhost ~]# useradd -u 10002 -g stu01 -e 2015-06-30 stu02

    sys01,不用于登录

        [root@localhost ~]# useradd -s /sbin/nologin sys01

    sys02,不创建宿主目录    

        [root@localhost ~]# useradd -M sys02

33.查看/etc/passwd文件的第一行

    [root@localhost ~]# head -1 /etc/passwd

    root:x:0:0:root:/root:/bin/bash

34.查看/etc/shadow文件的第一行

    [root@localhost ~]# head -1 /etc/shadow

    root:$1$1FskXDzB$KJvDMEmf8TKu18qJvd2nO0:16247:0:99999:7:::

 

35.针对student操作

设置密码为123456,然后用student登录自己修改密码

    [root@localhost ~]# passwd student

    Changing password for user student.

    New UNIX password: 

    BAD PASSWORD: it is too simplistic/systematic

    Retype new UNIX password: 

    passwd: all authentication tokens updated successfully.

清空student的密码,查看/etc/shadow里面与student相关的内容 

    清空前

[root@localhost ~]# grep student /etc/shadow

student:$1$dgLGRByb$xJrSGtmn7MSWlhA6zqMbR1:16257:0:99999:7:::

    清空后

[root@localhost ~]# passwd -d student

Removing password for user student.

passwd: Success

[root@localhost ~]# grep student /etc/shadow

    student::16257:0:99999:7:::

36.手动创建用户的过程 (禁止useradd)

        1)、/etc/passwd

    /etc/shadow

    /etc/group

      /etc/gshadow

2)、/home/xxxx

3)、/var/spool/mail/xxxx

4)、/etc/skel/.*

5)、权限

 

    [root@localhost ~]# vim /etc/passwd 

    yang:x:10005:10005::/homeyang:bin/bash

    

   [root@localhost ~]# vim /etc/shadow

    yang:!!:16258:0:99999:7:::

  

    [root@localhost ~]# vim /etc/group

    yang:x:10005:


    [root@localhost ~]# vim /etc/gshadow

    yang:!::

    

    [root@localhost ~]# mkdir /home/yang


    [root@weiliyang weiliyang]# touch /var/spool/mail/yang

37.为虚拟机添加一块80GB、SCSI接口的硬盘

    划分2个20GB的主分区,剩余作为扩展分区

    

    [root@localhost ~]# fdisk -l /dev/sdb


    Disk /dev/sdb: 85.8 GB, 85899345920 bytes

    255 heads, 63 sectors/track, 10443 cylinders

    Units = cylinders of 16065 * 512 = 8225280 bytes

    Disk /dev/sdb doesn‘t contain a valid partition table

    

        fdisk /dev/sdb

        按n键添加分区

        

    Command (m for help): n

    Command action        (e为交换分区,p为主分区)

       e   extended

       p   primary partition (1-4)

    p

    Partition number (1-4): 3  (输入分区编号)

    First cylinder (4867-10443, default 4867): (规划分区大小) 

    Using default value 4867    

    Last cylinder or +size or +sizeM or +sizeK (4867-10443, default 10443): +20g

       

38.添加交换分区

    Command (m for help): n

    Command action

       e   extended

       p   primary partition (1-4)

    e

    Selected partition 4

    First cylinder (7300-10443, default 7300): 

    Using default value 7300

    Last cylinder or +size or +sizeM or +sizeK (7300-10443, default 10443): 

    Using default value 10443

    p查看分区列表

     Device Boot      Start         End      Blocks   Id  System

    /dev/sdb1               1        2433    19543041   83  Linux

    /dev/sdb2            2434        4866    19543072+  83  Linux

    /dev/sdb3            4867       10443    44797252+   5  Extended

    /dev/sdb5            4867        7299    19543041   83  Linux

    /dev/sdb6            7300        8516     9775521   83  Linux

39.将第一个逻辑分区类型改为swap(id=82)

    Command (m for help): t

    Partition number (1-6): 5

    Hex code (type L to list codes): 82

    Changed system type of partition 5 to 82 (Linux swap / Solaris)

40.将第二个逻辑分区类型改为vfat(id=b或c)

    Command (m for help): t

    Partition number (1-6): 6    

    Hex code (type L to list codes): b

    Changed system type of partition 6 to b (W95 FAT32)

41.按w保存退出

    Command (m for help): w

    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    WARNING: If you have created or modified any DOS 6.x

    partitions, please see the fdisk manual page for additional

    information.

    Syncing disks.

42.使用partprobe更新分区表【最好是重启一次】

    [root@localhost ~]# partprobe 

    [root@localhost ~]# 


43.将/dev/sdb1格式化成ext3分区 

    [root@localhost ~]# mkfs.ext3 /dev/sdb1

    mke2fs 1.39 (29-May-2006)

    Filesystem label=

    OS type: Linux

    Block size=4096 (log=2)

    Fragment size=4096 (log=2)

    2443200 inodes, 4885760 blocks

    244288 blocks (5.00%) reserved for the super user

    First data block=0

    Maximum filesystem blocks=4294967296

    150 block groups

    32768 blocks per group, 32768 fragments per group

    16288 inodes per group

    Superblock backups stored on blocks: 

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 

        4096000

    Writing inode tables: done                            

    Creating journal (32768 blocks): done

    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 39 mounts or

    180 days, whichever comes first.  Use tune2fs -c or -i to override.

44.将/dev/sdb6格式化成FAT32分区

    [root@localhost ~]# mkfs.vfat /dev/sdb6

    mkfs.vfat 2.11 (12 Mar 2005)

45.将/dev/sdb1挂载到/mnt/part1,在这个挂载目录新建一个file.txt文件和一个now的目录

    [root@localhost ~]# mount /dev/sdb1 /mnt/part1/

    [root@localhost ~]# mount|grep sdb1

    /dev/sdb1 on /mnt/part1 type ext3 (rw)

    在/mnt/part1/下建立一个新的测试文件并查看

    [root@localhost ~]# touch /mnt/part1/file.txt

    [root@localhost ~]# ls /mnt/part1/

    file.txt  lost+found

46.分别卸载/dev/sdb1、/dev/sdb6

    [root@localhost ~]# umount /dev/sdb1    

    [root@localhost ~]# umount /dev/sdb6 

本文出自 “隗” 博客,请务必保留此出处http://9072070.blog.51cto.com/9062070/1435136