分类目录归档:安全运维

MariaDB创建用户、数据库

目标:

用户名:sampleuser
数据库名:sampledb
密码:samplepass

命令:

# Step1: Install MariaDB

yum install mariadb
systemctl start mariadb
systemctl enable mariadb


# Step2: Initialize MariaDB

mysql_secure_installation
Set root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y


# Step3: Create user and database

mysql -u root -p
MariaDB [(none)]>CREATE DATABASE sampledb;
MariaDB [(none)]>CREATE USER 'sampleuser'@'%' IDENTIFIED BY 'samplepass';
MariaDB [(none)]>GRANT ALL on sampledb.* to 'sampleuser'@'%';
MariaDB [(none)]>FLUSH PRIVILEGES;
MariaDB [(none)]>exit

 

编译安装python3

从官网下载源码包

wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

解压

tar xvzf Python-3.6.4.tgz

准备编译需要的依赖

yum install -y gcc zlib zlib-devel openssl openssl-devel

配置并编译

./configure --prefix=/usr/local/python3.6    # 设置安装位置
make
make install

添加环境变量。在/etc/profile文件末尾追加

PATH=$PATH:/usr/local/python3.6/bin
export PATH

使环境变量生效

source /etc/profile

测试,执行python3命令,查看输出

 

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使用shadowsocks代理

本文主要用于国内服务器访问被GFW屏蔽的外网时的代理设置。由于shadowsocks本身为socket5服务器,所以还需要借助代理工具将流量进行转发,本文中使用的为广泛应用的Privoxy。

ShadowSocks安装配置

首先安装shadowsocks服务器并配置。

yum install epel-release python-pip m2crypto
pip install shadowsocks

创建shadowsocks客户端配置文件

vim /etc/ss_client.json

配置如下

{
    "server":"ss_server_ip",
    "server_port":8388,
    "local_address": "127.0.0.1",
    "local_port":1080,
    "password":"your_password",
    "timeout":300,
    "method":"aes-256-cfb",
    "fast_open": false
}

启动shadowsocks服务器

sslocal -c /etc/ss_client.json -d start

Privoxy代理安装配置

之后需要使用Privoxy将流量转发,首先安装Privoxy

yum -y install privoxy

添加privoxy配置

echo 'listen-address localhost:8118' >> /etc/privoxy/config  # 8118为Privoxy默认端口
echo 'forward-socks5 / 127.0.0.1:1080 .' >> /etc/privoxy/config # 全局代理,所有流量都转发到socket5

启动privoxy服务器

systemctl start privoxy

最后,配置环境变量http_proxy/https_proxy

vim /etc/profile

在最后添加

export http_proxy=http://127.0.0.1:8118
export https_proxy=http://127.0.0.1:8118
export no_proxy=localhost

使配置生效

source /etc/profile

测试结果

此时,所有的配置已经完成。下面使用curl访问google.com,如果能够获取到内容即成功

curl www.google.com

参考

使用xrdp远程连接CentOS桌面

在最小安装的情况下,CentOS是没有桌面的,所以需要先安装GNOME桌面

yum -y groupinstall "GNOME Desktop"

启动桌面环境

startx

安装epel源

yum -y install epel-release

安装xrdp并启动

yum -y install xrdp
systemctl start xrdp
systemctl enable xrdp

如果有防火墙,配置规则允许3389端口或直接关闭防火墙

systemctl stop firewalld

完成后,直接使用windows远程桌面(mstsc)连接即可

设置为默认登录GUI

systemctl set-default graphical.target

设置为默认登录命令行

systemctl set-default multi-user.target

 

参考:CentOS 7安装Gnome GUI 图形界面

搭建Windows FTP服务器及内网穿透相关

这里使用FileZilla Server作为FTP服务器。本文将分两个部分进行说明,第一部分为FTP服务器的搭建和配置,第二部分为FTP服务器内网穿透(端口转发)相关配置。

FTP服务器搭建

首先在FileZilla官网 下载FileZilla Server端,安装完成后直接运行。运行后会提示设置管理端口(默认14147)及admin用户密码。这些都不是重点,键入密码后继续即可。完成后,我们就看到了FileZilla Server的主界面。

这里看见了一些红字提示:

You appear to be behind a NAT router. Please configure the passive mode settings and forward a range of ports in your router.
Warning: FTP over TLS is not enabled, users cannot securely log in.

主要说明了两方面的内容:1、软件识别到当前环境是部署在路由器NAT之后的,所以需要使用被动模式(passive mode)运行并在路由器上设置端口转发;2、FTP服务没有启用TLS模式,所以用户登录信息是不安全的。

我们先无视这两条警告信息,继续完成设置。首先添加一个FTP用户

进入到用户面板,添加用户信息

完成后,进入共享目录设置(Shared folders),添加需要共享的文件夹/磁盘

完成上述配置后,直接在资源管理器的地址栏中输入

ftp://本机IP/

然后使用刚才配置的用户名和密码即可登陆到FTP共享的文件目录,例如我的本机IP为192.168.50.100

内网穿透(端口转发)

通过以上配置,我们已经可以在本地使用FTP访问到共享的目录,但是,由于服务器配置在路由器后,所以如果想从外网访问到FTP服务器,必须配置端口转发。刚才在进入到FileZilla Server主界面的时候,有一行警告信息:You appear to be behind a NAT router. Please configure the passive mode settings and forward a range of ports in your router. 这一句话已经把所需要做的给说明了,那下面就具体操作一下。

首先,配置两个端口转发记录。之所以是两条,这是由于FTP协议的原因:

默认情况下FTP协议使用TCP端口中的 20和21这两个端口,其中20用于传输数据,21用于传输控制信息。但是,是否使用20作为传输数据的端口与FTP使用的传输模式有关,如果采用主动模式,那么数据传输端口就是20;如果采用被动模式,则具体最终使用哪个端口要服务器端和客户端协商决定。

这里配置了两条记录,外网3021端口转发到服务器21端口,也就是FTP服务器的控制端口;3020端口转发到服务器的3020端口,这个端口用于之后的被动模式数据传输。

接下来,修改FileZilla的被动模式设置。打开设置面板

启用被动模式(Passive Mode),并将端口设置为上面步骤中用于FTP数据传输设置的端口(3020)

完成后,我们就可以通过FTP客户端访问到FTP服务器了。不过在连接的时候,需要选择模式为被动模式。以FileZilla Client连接为例

新建站点后,填写域名/IP信息,端口选择之前配置端口转发时设置的控制端口(3021),并填好用户信息

在传输设置标签中,选择被动模式

常见问题

问题1:以上配置完成后,可以从本地访问共享目录,但无法从外网访问。

可能原因:防火墙阻止了相关端口的访问。需要关闭防火墙或配置相应端口的白名单规则。

问题2:添加多个文件夹时出现下面两个错误

  • you have shared multiple unrelated directories
  • you need to set a home directory

原因:这两个问题都是由于目录设置不规范导致的,FileZilla的规则为

  • 需要选择一个目录为主目录
  • 其他目录/磁盘需要以别名的方式挂载到主目录下

解决:典型的示例可以像下面一样,主目录为用户共享目录,然后三个磁盘以别名的方式挂在在主目录下

参考:

Zabbix错误”zbx_mem_malloc(): out of memory”

Zabbix Server突然挂了,查看log报错如下:

using configuration file: /etc/zabbix/zabbix_server.conf
...
[file:dbconfig.c,line:545] zbx_mem_malloc(): out of memory (requested 16 bytes)
[file:dbconfig.c,line:545] zbx_mem_malloc(): please increase CacheSize configuration parameter

报错里已经很明确的提示了修复办法:please increase CacheSize configuration parameter

所以,我们就去zabbix_server.conf中找到CacheSize字段

### Option: CacheSize
#   Size of configuration cache, in bytes.
#   Shared memory size for storing host, item and trigger data.
#
# Mandatory: no
# Range: 128K-8G
# Default:
# CacheSize=8M

根据服务器配置情况,修改CacheSize

### Option: CacheSize
#   Size of configuration cache, in bytes.
#   Shared memory size for storing host, item and trigger data.
#
# Mandatory: no
# Range: 128K-8G
# Default:
CacheSize=2048M

重启Zabbix Server即可

systemctl start zabbix-server