标签归档:Linux

CentOS添加环境变量

在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行php -v命令查看当前php版本信息时时,则会提示命令不存在的错误,下面我们详细介绍一下在linux下将php和mysql加入到环境变量中的方法(假设php和mysql分别安装在/usr/local/webserver/php//usr/local/webserver/mysql/中)。

方法一:直接运行命令

export PATH=$PATH:/usr/local/webserver/php/bin
export PATH=$PATH:/usr/local/webserver/mysql/bin

使用这种方法,只会对当前会话有效,也就是说每当登出或注销系统以后,PATH 设置就会失效,只是临时生效。

方法二:执行vi ~/.bash_profile修改文件中PATH一行,将/usr/local/webserver/php/bin/usr/local/webserver/mysql/bin 加入到PATH=$PATH:$HOME/bin一行之后

这种方法只对当前登录用户生效

方法三:修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码

PATH=$PATH:/usr/local/webserver/php/bin:/usr/local/webserver/mysql/bin
export PATH

最后:执行 命令source /etc/profile或 执行点命令 ./profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。

CentOS磁盘挂载

以下是一次完整的磁盘挂载、分区、格式化、LVM挂载等的过程

[root@centos ~]# fdisk -l

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbfe84878

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1  4294967295  2147483647+  ee  GPT

Disk /dev/sdb: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0001c0be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     2099199     1048576   83  Linux
/dev/sdb2         2099200   250068991   123984896   8e  Linux LVM

Disk /dev/mapper/centos-root: 34.6 GB, 34615590912 bytes, 67608576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-var: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@centos ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 1
Value out of range.
Partition number (2-4, default 2): q
Partition number (2-4, default 2): ^C
[root@centos ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n   
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p^H^C
[root@centos ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m        
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-250069679, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-250069679, default 250069679): 
Using default value 250069679
Partition 1 of type Linux and of size 119.2 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos ~]# fdisk -l

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbfe84878

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   250069679   125033816   83  Linux

Disk /dev/sdb: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0001c0be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     2099199     1048576   83  Linux
/dev/sdb2         2099200   250068991   123984896   8e  Linux LVM

Disk /dev/mapper/centos-root: 34.6 GB, 34615590912 bytes, 67608576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-var: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@centos ~]# l
Display all 155 possibilities? (y or n)
l.                           locate
l2ping                       lockdev
l2test                       logger
last                         login
lastb                        loginctl
lastcomm                     logname
lastlog                      logout
lchage                       logrotate
lchfn                        logsave
lchsh                        logview
ld                           look
ldattach                     losetup
ld.bfd                       lp
ldconfig                     lpadmin
ldd                          lpasswd
ld.gold                      lpc
ldns-chaos                   lpc.cups
ldns-compare-zones           lp.cups
ldnsd                        lpinfo
ldns-dane                    lpmove
ldns-dpa                     lpoptions
ldns-gen-zone                lppasswd
ldns-key2ds                  lpq
ldns-keyfetcher              lpq.cups
ldns-keygen                  lpr
ldns-mx                      lpr.cups
ldns-notify                  lprm
ldns-nsec3-hash              lprm.cups
ldns-read-zone               lprsetup.sh
ldns-resolver                lpstat
ldns-revoke                  lpstat.cups
ldns-rrsig                   ls
ldns-signzone                lsattr
ldns-test-edns               lsblk
ldns-testns                  lscgroup
ldns-update                  lscpu
ldns-verify-zone             lsinitrd
ldns-version                 lsipc
ldns-walk                    lslocks
ldns-zcat                    lslogins
ldns-zsplit                  lsmcli
--More--^C
[root@centos ~]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
└─sda1            8:1    0 119.2G  0 part 
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos ~]# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root xfs       33787784 3374740  30413044  10% /
devtmpfs                devtmpfs   8030912       0   8030912   0% /dev
tmpfs                   tmpfs      8042080       0   8042080   0% /dev/shm
tmpfs                   tmpfs      8042080    9136   8032944   1% /run
tmpfs                   tmpfs      8042080       0   8042080   0% /sys/fs/cgroup
/dev/sdb1               xfs        1038336  193472    844864  19% /boot
/dev/mapper/centos-home xfs       20961280   32956  20928324   1% /home
/dev/mapper/centos-var  xfs       52403200  315164  52088036   1% /var
tmpfs                   tmpfs      1608420      56   1608364   1% /run/user/0
[root@centos ~]# mkfs.
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat
mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs
[root@centos ~]# mkfs.
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat
mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs
[root@centos ~]# mkfs.xfs /dev/sda
sda   sda1  
[root@centos ~]# mkfs.xfs /dev/sda
sda   sda1  
[root@centos ~]# mkfs.xfs /dev/sda1
mkfs.xfs: /dev/sda1 appears to contain an existing filesystem (ntfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos ~]# mkfs.xfs /dev/sda1 -f
meta-data=/dev/sda1              isize=512    agcount=4, agsize=7814614 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=31258454, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=15262, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos ~]# cd /
[root@centos /]# mkdir data
[root@centos /]# ll
total 28
lrwxrwxrwx.   1 root root    7 Mar  7 22:08 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Mar  7 22:46 boot
drwxr-xr-x.   2 root root    6 Mar  8 09:43 data
drwxr-xr-x.  21 root root 3540 Mar  8 09:33 dev
drwxr-xr-x. 139 root root 8192 Mar  7 23:44 etc
drwxr-xr-x.   3 root root   19 Mar  7 22:12 home
lrwxrwxrwx.   1 root root    7 Mar  7 22:08 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Mar  7 22:08 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Nov  5  2016 media
drwxr-xr-x.   2 root root    6 Nov  5  2016 mnt
drwxr-xr-x.   3 root root   16 Mar  7 22:39 opt
dr-xr-xr-x. 308 root root    0 Mar  8 07:44 proc
dr-xr-x---.  19 root root 4096 Mar  8 09:23 root
drwxr-xr-x.  38 root root 1140 Mar  8 09:23 run
lrwxrwxrwx.   1 root root    8 Mar  7 22:08 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Nov  5  2016 srv
dr-xr-xr-x.  13 root root    0 Mar  7 23:44 sys
drwxrwxrwt.  23 root root 4096 Mar  8 09:43 tmp
drwxr-xr-x.  13 root root  155 Mar  7 22:08 usr
drwxr-xr-x.  21 root root 4096 Mar  7 22:39 var
[root@centos /]# mount /dev/sda1 /data
[root@centos /]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        33G  3.3G   30G  10% /
devtmpfs                devtmpfs  7.7G     0  7.7G   0% /dev
tmpfs                   tmpfs     7.7G     0  7.7G   0% /dev/shm
tmpfs                   tmpfs     7.7G  9.0M  7.7G   1% /run
tmpfs                   tmpfs     7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/sdb1               xfs      1014M  189M  826M  19% /boot
/dev/mapper/centos-home xfs        20G   33M   20G   1% /home
/dev/mapper/centos-var  xfs        50G  308M   50G   1% /var
tmpfs                   tmpfs     1.6G   56K  1.6G   1% /run/user/0
/dev/sda1               xfs       120G   33M  120G   1% /data
[root@centos /]# cd /etc/fstab^C
[root@centos /]# vi /etc/fstab 
[root@centos /]# 
[root@centos /]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
└─sda1            8:1    0 119.2G  0 part /data
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# fdisk -l

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbfe84878

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   250069679   125033816   83  Linux

Disk /dev/sdb: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0001c0be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     2099199     1048576   83  Linux
/dev/sdb2         2099200   250068991   123984896   8e  Linux LVM

Disk /dev/mapper/centos-root: 34.6 GB, 34615590912 bytes, 67608576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-var: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@centos /]# fdisk -l

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbfe84878

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   250069679   125033816   83  Linux

Disk /dev/sdb: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0001c0be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     2099199     1048576   83  Linux
/dev/sdb2         2099200   250068991   123984896   8e  Linux LVM

Disk /dev/mapper/centos-root: 34.6 GB, 34615590912 bytes, 67608576 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 17.2 GB, 17179869184 bytes, 33554432 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-home: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-var: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@centos /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbfe84878

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   250069679   125033816   83  Linux

Command (m for help): ^C
[root@centos /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m  
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-250069679, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-250069679, default 250069679): 
Using default value 250069679
Partition 1 of type Linux and of size 119.2 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos /]# fdisk /dev/sda^C
[root@centos /]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
└─sda1            8:1    0 119.2G  0 part /data
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# unmount /dev/sda
bash: unmount: command not found...
[root@centos /]# yum install unmount
Loaded plugins: fastestmirror, langpacks
base                                        | 3.6 kB     00:00     
epel/x86_64/metalink                        | 7.1 kB     00:00     
extras                                      | 3.4 kB     00:00     
updates                                     | 3.4 kB     00:00     
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirror01.idc.hinet.net
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
No package unmount available.
Error: Nothing to do
[root@centos /]# umount /dev/sda
umount: /dev/sda: not mounted
[root@centos /]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
└─sda1            8:1    0 119.2G  0 part /data
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# umount /data
[root@centos /]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
└─sda1            8:1    0 119.2G  0 part 
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# mkfs.xfs /dev/sda
mkfs.xfs: /dev/sda appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos /]# mkfs.xfs /dev/sda -f
meta-data=/dev/sda               isize=512    agcount=4, agsize=7814678 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=31258710, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=15263, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos /]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk 
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# mount /dev/sda /data
[root@centos /]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 119.2G  0 disk /data
sdb               8:16   0 119.2G  0 disk 
├─sdb1            8:17   0     1G  0 part /boot
└─sdb2            8:18   0 118.2G  0 part 
  ├─centos-root 253:0    0  32.2G  0 lvm  /
  ├─centos-swap 253:1    0    16G  0 lvm  [SWAP]
  ├─centos-home 253:2    0    20G  0 lvm  /home
  └─centos-var  253:3    0    50G  0 lvm  /var
[root@centos /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x70b67fc3.

Command (m for help): d
No partition is defined yet!

Command (m for help): ^C 
[root@centos /]# umount /data
[root@centos /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x181824cf.

Command (m for help): p  

Disk /dev/sda: 128.0 GB, 128035676160 bytes, 250069680 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x181824cf

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): ^C 
[root@centos /]# pvcreate /dev/sda
WARNING: xfs signature detected on /dev/sda at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sda.
  Physical volume "/dev/sda" successfully created.
[root@centos /]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda          lvm2 ---   119.24g 119.24g
  /dev/sdb2  centos lvm2 a--  <118.24g      0 
[root@centos /]# vgcreate -s 4m datavg /dev/sda
  Volume group "datavg" successfully created
[root@centos /]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <118.24 GiB
  PE Size               4.00 MiB
  Total PE              30269
  Alloc PE / Size       30269 / <118.24 GiB
  Free  PE / Size       0 / 0   
  VG UUID               ykO7iD-Z2Vr-RnSN-nmdz-dDjx-AZlu-WGTO3L
   
  --- Volume group ---
  VG Name               datavg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <119.24 GiB
  PE Size               4.00 MiB
  Total PE              30525
  Alloc PE / Size       0 / 0   
  Free  PE / Size       30525 / <119.24 GiB
  VG UUID               2Vt33P-4wry-4CHm-fzFd-ivoL-6uLe-fPk8fz
   
[root@centos /]# vgdisplay datavbg
  Volume group "datavbg" not found
  Cannot process volume group datavbg
[root@centos /]# vgdisplay datavg
  --- Volume group ---
  VG Name               datavg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <119.24 GiB
  PE Size               4.00 MiB
  Total PE              30525
  Alloc PE / Size       0 / 0   
  Free  PE / Size       30525 / <119.24 GiB
  VG UUID               2Vt33P-4wry-4CHm-fzFd-ivoL-6uLe-fPk8fz
   
[root@centos /]# lvcreate -l 30525 -n lv_data datavg
  Logical volume "lv_data" created.
[root@centos /]# lvs
  LV      VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home    centos -wi-ao----   20.00g                                                    
  root    centos -wi-ao----  <32.24g                                                    
  swap    centos -wi-ao----   16.00g                                                    
  var     centos -wi-ao----   50.00g                                                    
  lv_data datavg -wi-a----- <119.24g                                                    
[root@centos /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   33G  3.3G   30G  10% /
devtmpfs                 7.7G     0  7.7G   0% /dev
tmpfs                    7.7G     0  7.7G   0% /dev/shm
tmpfs                    7.7G  9.0M  7.7G   1% /run
tmpfs                    7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/sdb1               1014M  189M  826M  19% /boot
/dev/mapper/centos-home   20G   33M   20G   1% /home
/dev/mapper/centos-var    50G  307M   50G   1% /var
tmpfs                    1.6G   56K  1.6G   1% /run/user/0
[root@centos /]# mkfs.xfs /dev/mapper/datavg-lv_data
meta-data=/dev/mapper/datavg-lv_data isize=512    agcount=4, agsize=7814400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=31257600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=15262, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   33G  3.3G   30G  10% /
devtmpfs                 7.7G     0  7.7G   0% /dev
tmpfs                    7.7G     0  7.7G   0% /dev/shm
tmpfs                    7.7G  9.0M  7.7G   1% /run
tmpfs                    7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/sdb1               1014M  189M  826M  19% /boot
/dev/mapper/centos-home   20G   33M   20G   1% /home
/dev/mapper/centos-var    50G  307M   50G   1% /var
tmpfs                    1.6G   56K  1.6G   1% /run/user/0
[root@centos /]# lvs
  LV      VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home    centos -wi-ao----   20.00g                                                    
  root    centos -wi-ao----  <32.24g                                                    
  swap    centos -wi-ao----   16.00g                                                    
  var     centos -wi-ao----   50.00g                                                    
  lv_data datavg -wi-a----- <119.24g                                                    
[root@centos /]# mount /dev/mapper/datavg-lv_data /data
[root@centos /]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/centos-root      33G  3.3G   30G  10% /
devtmpfs                    7.7G     0  7.7G   0% /dev
tmpfs                       7.7G     0  7.7G   0% /dev/shm
tmpfs                       7.7G  9.0M  7.7G   1% /run
tmpfs                       7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/sdb1                  1014M  189M  826M  19% /boot
/dev/mapper/centos-home      20G   33M   20G   1% /home
/dev/mapper/centos-var       50G  307M   50G   1% /var
tmpfs                       1.6G   56K  1.6G   1% /run/user/0
/dev/mapper/datavg-lv_data  120G   33M  120G   1% /data
[root@centos /]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Mar  7 22:08:19 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b118055d-ded6-4cf2-9587-31f28414e5f9 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-var  /var                    xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@centos /]# umount /data/
[root@centos /]# df -h 
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   33G  3.3G   30G  10% /
devtmpfs                 7.7G     0  7.7G   0% /dev
tmpfs                    7.7G     0  7.7G   0% /dev/shm
tmpfs                    7.7G  9.0M  7.7G   1% /run
tmpfs                    7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/sdb1               1014M  189M  826M  19% /boot
/dev/mapper/centos-home   20G   33M   20G   1% /home
/dev/mapper/centos-var    50G  307M   50G   1% /var
tmpfs                    1.6G   56K  1.6G   1% /run/user/0
[root@centos /]# lvremove /dev/mapper/
centos-home     centos-root     centos-swap     centos-var      datavg-lv_data  
[root@centos /]# lvremove /dev/mapper/
centos-home     centos-root     centos-swap     centos-var      datavg-lv_data  
[root@centos /]# lvremove /dev/mapper/datavg-lv_data 
Do you really want to remove active logical volume datavg/lv_data? [y/n]: y
  Logical volume "lv_data" successfully removed
[root@centos /]# vgremove /dev/sda
  Volume group "sda" not found
  Cannot process volume group sda
[root@centos /]# vgremove datavg
  Volume group "datavg" successfully removed
[root@centos /]# pvremove /dev/sda
  Labels on physical volume "/dev/sda" successfully wiped.

分区后,修改/etc/fstab文件保存目录挂载信息即可

CentOS 5安装Telnet服务

1.安装telnet服务

[root@localhost ~]# yum install telnet
[root@localhost ~]# yum install xinetd

2.检查是否成功安装

[root@localhost ~]# rpm -qa | grep telnet
telnet-0.17-38.el5
telnet-server-0.17-38.el5                                      #####有显示就是正确的

3.修改文件开启服务

[root@localhost home]# vim /etc/xinetd.d/telnet 
# default: on
# description: The telnet server serves telnet sessions; it uses \
#       unencrypted username/password pairs for authentication.
service telnet
{
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/sbin/in.telnetd
        log_on_failure  += USERID
        disable         = no                              默认的yes是指禁止远方telnet,改为no就是启动
}

[root@localhost xinetd.d]# service  xinetd  restart
停止 xinetd:                                              [确定]
启动 xinetd:                                              [确定]

4.停止iptables、seliunx(可以在iptables中开启telnet的23端口,后面有介绍)

5.测试能否能用root账户telnet(若没配置一般是不行的)

6.修改配置使root登陆

当我们失败后,linux是会记录下失败记录作为日志在/var/log/secure

Oct 26 08:17:57 localhost login: pam_securetty(remote:auth): access denied: tty 'pts/1' is not secure !
Oct 26 08:18:01 localhost login: FAILED LOGIN 1 FROM 192.168.1.101 FOR root, Authentication failure

可以看到没有pts/1所以被拒绝了

我们可以在修改添加一个虚拟线程

[root@localhost xinetd.d]# vi /etc/securetty 

console
vc/1
vc/2
vc/3
vc/4
vc/5
vc/6
vc/7
vc/8
vc/9
vc/10
vc/11
tty1
tty2
tty3
tty4
tty5
tty6
tty7
tty8
tty9
tty10
tty11
pts/1

再次测试

Xshell:\> telnet 192.168.1.100

Connecting to 192.168.1.100:23...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
CentOS release 5 (Final)
Kernel 2.6.18-8.el5 on an i686
login: root
Password: 
Last login: Wed Oct 26 08:13:15 from 192.168.1.101
[root@localhost ~]#

ps:不建议直接用root登陆,因为telnet是明文传输。建议用一个普通用户登录然后su到root用户权限

yum安装中config.py错误解决

报错内容

Traceback (most recent call last):
  File "/usr/bin/yum", line 29, in <module>
    yummain.user_main(sys.argv[1:], exit_code=True)
  File "/usr/share/yum-cli/yummain.py", line 298, in user_main
    errcode = main(args)
  File "/usr/share/yum-cli/yummain.py", line 115, in main
    base.getOptionsConfig(args)
  File "/usr/share/yum-cli/cli.py", line 229, in getOptionsConfig
    self.conf
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 911, in <lambda>
    conf = property(fget=lambda self: self._getConfig(),
  File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 300, in _getConfig
    startupconf = config.readStartupConfig(fn, root)
  File "/usr/lib/python2.6/site-packages/yum/config.py", line 896, in readStartupConfig
    startupconf.releasever = _getsysver(startupconf.installroot, startupconf.distroverpkg)
  File "/usr/lib/python2.6/site-packages/yum/config.py", line 1047, in _getsysver
    hdr = idx.next()
StopIteration

报错Traceback提示为,config.py中的_getsysver方法出现了问题

解决方法为

rpm --rebuilddb

参考:Yum completely broken after deleting something

解决使用Screen出现Cannot open your terminal办法

这个问题出现在我在搭建minecraft时。首先我创建了用户: minecraft

adduser minecraft

然后进行minecraft的安装配置。具体略。

之后,切换到root安装screen

~$ su root
~# apt install screen

切换回minecraft用户,准备开启screen运行服务器。此时报错了

~# su minecraft
~$ screen -S minecraft
Cannot open your terminal '/dev/pts/2' - please check.
  • 解决方法1:

切换到root,然后执行

script /dev/null

之后切换回minecraft用户即可。

  • 解决方法2:

重新开启一个ssh连接,直接使用minecraft用户登录。然后进行screen操作就不会报错了。

参考:http://blog.sina.com.cn/s/blog_704836f401010osn.html

http://serverfault.com/questions/116775/sudo-as-different-user-and-running-screen

使用iptables-persistent持久化iptables规则

正常情况下,我们写入的iptables规则将会在系统重启时消失。即使我们使用iptables-save命令将iptables规则存储到文件,在系统重启后也需要执行iptables-restore操作来恢复原有规则。(当然,你也可以通过在network中的if.post.up.d中配置启动规则来达到开机自动启动iptables的方法)

这里我们有一个更好的iptables持久化方案,让防火墙规则重启后依旧有效。即使用iptables-persistent工具。

首先,安装:

sudo apt-get install iptables-persistent

安装完后即可使用以下命令保存或载入规则:

Ubuntu 14.04

sudo invoke-rc.d iptables-persistent save
sudo invoke-rc.d iptables-persistent reload

或者

sudo /etc/init.d/iptables-persistent save 
sudo /etc/init.d/iptables-persistent reload

Ubuntu 16.04

sudo netfilter-persistent save
sudo netfilter-persistent reload

生成的规则将被存储在以下文件中

/etc/iptables/rules.v4
/etc/iptables/rules.v6

参考:How to save rules of the iptables?

locale警告信息修复

在维护系统后,忽然出现了这样一条和语言相关的系统警告信息:locale: Cannot set LC_ALL to default locale: No such file or directory。在进行各种操作的时候时不时的就跳出来。

于是,我开始查错,首先看看系统的locale配置:

root@localhost:~# locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=en_US:
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=

提示LC_ALL无法找到。看配置中确实这一栏为空。然后查询一下系统已经安装的locale:

root@localhost:~# locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
POSIX

发现了一个明显的问题,配置中的zh_CN.UTF-8并没有安装。于是安装:

sudo locale-gen zh_CN.UTF-8

问题解决。

网上还有一些其他的解决方法和问题原因说明,我没尝试过但是应该有参考价值:

http://www.360doc.com/content/14/0428/16/17044736_372964025.shtml

http://askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue

使用awk命令打印九九乘法表

神TM的awk!

[root@localhost ~]# awk 'BEGIN{for(i=1;i<10;i++) {for(j=1;j<=i;j++) {printf "%d%s%d%s%d\t",j,"*",i,"=",i*j;}printf "\n"}}'
1*1=1
1*2=2   2*2=4
1*3=3   2*3=6   3*3=9
1*4=4   2*4=8   3*4=12  4*4=16
1*5=5   2*5=10  3*5=15  4*5=20  5*5=25
1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
1*9=9   2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81

其实也还好,就是用bash写了个双循环而已