文章目录

  • BIOS开启VT支持
  • U盘安装系统(2019-03-11)
  • CentOS DNS配置
  • CentOS网络配置
  • 配置静态IP
  • 多网卡配置
  • 克隆虚拟机网卡名称变更 CentOS6.5
  • 时间配置
  • 安装VMWare-tools
  • 用户管理 (2019-03-15 7.6.1810)
  • 给一般账号 root 权限
  • Samba服务配置
  • 安装必备软件
  • 获取本机公网ip
  • yum源和第三方库源管理
  • 配置本地源(2019-03-12)
  • 配置国内源
  • 使用第三方源
  • 多个gcc版本共存管理(2019-03-15)
  • 多版本系统资源
  • 常用工具安装
  • 单用户模式



U盘安装系统(2019-03-11)

  • 系统镜像:CentOS-7-x86_64-DVD-1810.iso
  • 制作工具:UltraISO

使用UltraISO制作安装U盘的步骤

  1. 【文件】【打开】选择系统镜像
  2. 【启动】【写入硬盘映像】【格式化】选择默认系统格式以及Label改成CENTOS7,快速格式化,完成后写入

常见问题:

  • 在INSTALLATION SOURCE中无法选择本地镜像
    可能安装U盘制作的问题,建议重新制作
  • Warning: /dev/root does not exist, could not boot 此问题是由于无法找到U盘的问题,命令行下ls /dev/或者lsblk查看U盘对应的盘符,比如/dev/sda4. 启一下,上下键选择安装菜单,然后按下Tab键,编辑启动命令,将vmlinuz initrd=initrd.imginst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet 改为:vmlinuz initrd=initrd.img inst.stage2=hd:/dev/sdb4 quite

CentOS DNS配置

Linux 下设置DNS位置有3处,其生效顺序如下:

  • /etc/hosts
  • 网卡配置文件DNS服务器地址
  • /etc/resolv.conf

通过设置主机表地址进行特定主机的解析,优先DNS服务器地址,/etc/hosts:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
123.125.114.144 www.baidu.com #手动指定百度的IP

在/etc/hosts配置www.baidu.com的网址后,可以用ping www.baidu.com命令来观看ping的地址是否是123.125.114.114

Linux中默认的DNS服务器地址配置文件为/etc/resolv.conf:

nameserver 114.114.114.114 #配置DNS服务器地址,可配置多个
nameserver 8.8.8.8

在网卡配置文件中添加DNS服务器地址, /etc/sysconfig/network-scripts/ifcfg-eno16777736:

DNS1=114.114.114.114
DNS2=8.8.8.8

解析域名的方法:

[root@localhost]# nslookup baidu.com
Server:		114.114.114.114
Address:	114.114.114.114#53

Non-authoritative answer:
Name:	baidu.com
Address: 220.181.57.217
Name:	baidu.com
Address: 111.13.101.208
Name:	baidu.com
Address: 123.125.114.144
Name:	baidu.com
Address: 180.149.132.47

[root@localhost]# host baidu.com
baidu.com has address 180.149.132.47
baidu.com has address 220.181.57.217
baidu.com has address 111.13.101.208
baidu.com has address 123.125.114.144
baidu.com mail is handled by 10 mx.n.shifen.com.
baidu.com mail is handled by 20 mx1.baidu.com.
baidu.com mail is handled by 20 jpmx.baidu.com.
baidu.com mail is handled by 20 mx50.baidu.com.
[root@localhost]# dig baidu.com

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 33094
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;baidu.com.			IN	A

;; ANSWER SECTION:
baidu.com.		202	IN	A	111.13.101.208
baidu.com.		202	IN	A	123.125.114.144
baidu.com.		202	IN	A	180.149.132.47
baidu.com.		202	IN	A	220.181.57.217

;; Query time: 28 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Wed Nov 18 13:31:29 2015
;; MSG SIZE  rcvd: 91

CentOS网络配置

配置静态IP

根据centos7.0安装教程安装虚拟机。

su root切换到root用户,编辑配置文件vim /etc/sysconfig/network-scripts/ifcfg-eno16777736

HWADDR=00:0C:29:23:C4:B1 #ip link show查看
TYPE=Ethernet
#BOOTPROTO=dhcp
BOOTPROTO=static
IPADDR=192.168.0.122
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=114.114.114.114
DNS2=8.8.8.8
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
NM_CONTROLLED=no
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=ebd7eace-f0b7-4d48-b4d6-fd6ee3f7e21d #nmcli con show
# ONBOOT=no
ONBOOT=yes

注意配置能够使用的DNS,否则无法解析域名,可参考常用公共DNS服务器地址

service network restart重启网络

ping baidu.com是否能够ping通百度

自动生成配置文件:nmcli con add con-name ens3f0 type ethernet ifname ens3f0

多网卡配置

在条件允许的情况下,很多设备时可以同时连接内外网ip,在这种情况下配置交复杂。

  1. 分别给内网网卡与外网网卡配置好ip信息,这里配置内网网卡的时候,不要配置GATEWAY,这个时候网络重启生成的默认路由走外网网关地址,内网同网段通信走内网网卡;
  2. 给与内网其他网段网络通信配置静态路由,如内网网段172.168.20.0/24,内网网关地址172.168.10.254,设备内网网卡名称eno4
  • 临时生效
  • ip route add 172.168.20.0/24 via 172.168.10.254 dev eno4
  • 永久生效,配置文件文件名格式 route-网卡名称
# cat /etc/sysconfig/network-scripts/route-eno4 
172.168.20.0/24 via 172.168.10.254 dev eno4

对于虚拟机通常情况下我都是配置两个网卡,一个桥接模式一个NAT模式,VSCode都是通过NAT网卡进行登录,即使公司网络不稳定也不会受到影响,桥接网卡只是在其他设备访问主机的时候使用

配置多个网卡
配置第二个时,需要自己创建配置文件,在重启网卡之前,停止NetworkManager
systemctl stop NetworkManagersystemctl disable NetworkManager

克隆虚拟机网卡名称变更 CentOS6.5

虚拟机克隆后发现网卡名称从eth0改成了eth1,如何改回呢:

  1. /etc/udev/rules.d/70-persistent-net.rules查看到两块网卡eth0与eth1,eth0为克隆系统的,eth1为重新生成,注释掉第一个eth0网卡信息。将eth1更改为eth0并记录下mac地址;
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x8086:0x100f (e1000)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:94:65:9c", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:aa:e3:ea", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
	```
  1. /etc/sysconfig/network-scripts/ifcfg-eth0更改DEVICE与HWADDR
  2. 重启系统,此处重启网络不行

时间配置

配置本地时区:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 同步网络时间:
yum install ntpdatentpdate pool.ntp.org 手动配置时间:

date –s '2016-12-09 15:50:59'

clock -r //读CMOS日期
clock –w //将日期写入CMOS

hwclock --show                          //查看硬件时钟
hwclock --set --date="11/23/2016 22:16:59" //设置硬件时钟

或者使用RHEL7及CentOS7中新增的systemd的timedatectl命令,具体参考Linux(RHEL7及CentOS7)的时间设置篇(timedatectl,date,hwclock)-系统管理(2)

# timedatectl
      Local time: Thu 2019-03-07 09:10:03 CST
  Universal time: Thu 2019-03-07 01:10:03 UTC
        RTC time: Thu 2019-03-07 01:10:03
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
# timedatectl -h
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled

安装VMWare-tools

# 【虚拟机】-【安装VMWare-tools】
mount /dev/cdrom /mnt
cd /mnt
tar xvf /mnt/VMwareTools-9.9.0-2304977.tar.gz -C /tmp
cd /tmp/vmware-tools-distrib
./vmware-install.pl  # 一路回车即可

【问题1】安装过程中报错:vmhgfs-only/page.c:1625:23: 错误:提供给函数‘wait_on_bit’的实参太多

1、在解压后的 vmware-tools-distrib/ 目录中,进入到 lib/modules/source/,解压 vmhgfs.tar,得到 vmhgfs-only 目录。

2、修改文件 vmhgfs-only/page.c :把第1622行

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 17, 0)

改成

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0) 因为 CentOS 7 的内核版本是 3.10.0。

3、重新把 vmhgfs-only 目录打包为 vmhgfs.tar。

4、重新执行安装脚本 vmware-install.pl。

【问题2】vmvare安装后,共享的文件夹在/mnt/hgfs目录并没有显示出来

1、运行/usr/bin/vmware-config-tools.pl会出现错误
2、/usr/lib/vmware-tools/modules/source目录,同问题的解决步骤进行修改
3、在运行/usr/bin/vmware-config-tools.pl

用户管理 (2019-03-15 7.6.1810)

给一般账号 root 权限

默认情况下,一般账号是没有 root 权限的。为什么不直接用 root 账号,而是要给一般账号 root 权限呢?

  • 日常工作中使用 root 账号,是非常糟糕且危险的习惯!无论是服务器还是个人电脑都是如此。
  • 安装软件时需要 root 权限,只能 su 切换到 root 账户再安装。对于习惯使用 sudo 的人来说,还是有些麻烦。

假设账户名为 seisman,要授予他 root 权限,则要修改配置文件 /etc/sudoers :

$ su
# echo 'seisman ALL=(ALL) ALL'>> /etc/sudoers # 向配置文件中加入语句
# tail -1 /etc/sudoers  # 检查一下是否正确
seisman ALL=(ALL) ALL

其中 seisman 为当前用户名。

Samba服务配置

yum install samba samba-client samba-common安装相关软件

# rpm -qa | grep samba
samba-4.1.12-23.el7_1.x86_64
samba-libs-4.1.12-23.el7_1.x86_64
samba-common-4.1.12-23.el7_1.x86_64
samba-client-4.1.12-23.el7_1.x86_64

这里顺便说一下搭建samba服务器所需要的基本软件包:

  1. samba 这个软件包包含了主要的daemon文件(smbd和nmbd)
  2. samba-common 提供samba的主要配置文件(smb.conf)、smb.conf语法检测程序(testparm)等
  3. samba-client 当linux作为samba 客户端的时候,提供了一套所需的工具和指令。

备份配置文件cp /etc/samba/smb.conf /etc/samba/smb.conf.$(date +%F) 编辑vim /etc/samba/smb.conf

[global]
	workgroup = MYGROUP
	server string = Samba Server Version %v
	log file = /var/log/samba/log.%m
	max log size = 50
	security = user
	passdb backend = tdbsam
	load printers = yes
	cups options = raw

[samba]
	comment = my share dir
	path = /home/user
	browseable = yes
	guest ok = yes
	writable = yes
	public = yes

添加用户smbpasswd -a samba_user,设置密码
重启服务
service smb restartservice nmb restart

关闭防火墙
systemctl stop firewalld.servicesystemctl disable firewalld.servicesetenforce 0

安装iptables服务
yum install iptables-services

Linux下客户端测试
smbclient -L //192.168.0.122/samba -U samba_user

设置开机启动
systemctl enable smb.service

Win8下添加网络位置
进入我的电脑,【右击】-【添加网络位置】根据向导一直点击【下一步】,在Internet地址或网络位置下填写\\192.168.0.122\samba,【下一步】根据提示填写用户与密码。

安装必备软件

  • 安装gcc: yum -y install gcc
  • 安装g++: yum -y install gcc-c++
  • 安装gccgo: yum -y install gcc-go
  • 安装小工具:
  • yum -y install lrzsz screen
  • yum -y install socat nc nmap
  • yum -y install tree

获取本机公网ip

  • curl http://members.3322.org/dyndns/getip
  • curl ipinfo.io
{
  "ip": "36.110.42.197",
  "hostname": "No Hostname",
  "city": "Beijing",
  "region": "Beijing Shi",
  "country": "CN",
  "loc": "39.9289,116.3883",
  "org": "AS4847 China Networks Inter-Exchange"
}

yum源和第三方库源管理

安装完系统,我们常常意愿使用国内的yum源或本地源以提高下载速度,使用第三方库源来获取不在标准库中的资源。常用管理总结如下

yum工具配置文件/etc/yum.conf

[main]
cachedir=/var/cache/yum/$basearch/$releasever # basearch就是架构,如x86_64,releasever版本号,如7
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

配置本地源(2019-03-12)

系统镜像:CentOS-7-x86_64-DVD-1810.iso

  • 【虚拟机】【设置】【硬件】【CD/DVD(IDE)】【设备状态】选择启动时连接,【连接】使用ISO镜像文件,选择ISO文件;或者在实体机中连接镜像光驱
  • 查看配置文件,系统安装后自带
# cat /etc/yum.repos.d/CentOS-Media.repo 
# CentOS-Media.repo
#
#  This repo can be used with mounted DVD media, verify the mount point for
#  CentOS-7.  You can use this repo and yum to install items directly off the
#  DVD ISO that we release.
#
# To use this repo, put in your DVD and use it with the other repos too:
#  yum --enablerepo=c7-media [command]
#  
# or for ONLY the media repo, do this:
#
#  yum --disablerepo=\* --enablerepo=c7-media [command]

[c7-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
        file:///media/cdrom/
        file:///media/cdrecorder/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • sudo mkdir /media/cdrom;sudo mount /dev/cdrom /media/cdrom 挂载ISO
  • yum repolist all查看源的状态
c7-media          CentOS-7 - Media         disabled
  • yum-config-manager --enable c7-media

使用第三方源

CentOS7

yum localinstall http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
# ffmpeg 2.8.15

yum localinstall --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm https://download1.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-7.noarch.rpm
# ffmpeg 3.4.7

CentOS6.7为例

  1. 安装yum优先级插件

yum install yum-priorities

  1. epel简介: https://fedoraproject.org/wiki/EPEL/zh-cn
rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

以上URL请按实际情况修改

  1. 查看是否安装成功

rpm -q epel-release

  1. 导入key:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

  1. 修改/etc/yum.repos.d/epel.repo文件

在[epel]最后添加一条属性 priority=11

vi /etc/yum.repos.d/epel.repo
意思是yum先去官方源查,官方没有再去epel的源找

  1. 重建缓存

yum makecache

多个gcc版本共存管理(2019-03-15)

在一些软件编译时,对编译器的版本有要求,比如gcc不低于某某或者不高于某某。

Centos7 gcc版本默认4.8.3,为了兼容CentOS6的包,提供了compat-gcc安装包

# yum search compat-gcc
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
====================================== N/S matched: compat-gcc ======================================
compat-gcc-44.x86_64 : Compatibility GNU Compiler Collection
compat-gcc-44-c++.x86_64 : C++ support for compatibility compiler
compat-gcc-44-gfortran.x86_64 : Fortran support for compatibility compiler

Red Hat 为了软件的稳定和版本支持,yum 上gcc版本也是4.8.3,所以无法使用yum进行软件更新。要升级更高版本的gcc,我们需要scl源

  • 安装scl源 yum install -y centos-release-scl
  • 要启用和运行 SCL 中的应用,你还需要安装下列包: yum install -y scl-utils-build
  • 安装高版本的gcc yum install -y devtoolset-8-toolchain
  • 查看scl安装的软件列表 scl --list
  • 使用devtoolset-4的环境 scl enable devtoolset-4 bash