CentOS7開發環境搭建(1)

BIOS開啓VT支持


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

配置多個網卡
配置第二個時,需要自己創建配置文件,在重啓網卡之前,停止NetworkManager
systemctl stop NetworkManager
systemctl 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"
    	```
    
  2. /etc/sysconfig/network-scripts/ifcfg-eth0更改DEVICE與HWADDR
  3. 重啓系統,此處重啓網絡不行

時間配置

配置本地時區:
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
同步網絡時間:
yum install ntpdate
ntpdate 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 restart
service nmb restart

關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 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
    
    baseurl中可以知道,會去/media/cdrom這項去找源
  • 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

配置國內源

查看CentOS源的幫助,根據CentOS版本進行對應的操作

  • http://mirrors.aliyun.com
  • http://mirrors.163.com
  • https://mirrors.huaweicloud.com/
  • https://mirrors.tuna.tsinghua.edu.cn/

使用第三方源

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-4-toolchain
  • 查看scl安裝的軟件列表 scl --list
  • 使用devtoolset-4的環境 scl enable devtoolset-4 bash

參考: Centos7升級gcc版本方法之一使用scl軟件集

多版本系統資源

http://vault.centos.org/

常用工具安裝

  • yum install man-pages libstdc++-docs 安裝開發手冊

單用戶模式

虛擬機長時間不用,最可能發生的事情是忘記root密碼

https://www.cnblogs.com/zhangjianghua/p/6094496.html

參考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章