玩轉centos7和docker

目前大多數的linux服務器使用的都是centos5,6;但是在不久的將來7一定會成爲主流,接下來我將爲大家詳細的介紹centos7的一些常用的技術。

掌握以下技術,可以輕鬆玩轉centos7。

注意:

centos7和centos6.5的不同:

1.網絡管理方式不同

2.服務管理方式不同

3.防火牆管理方式不同

4.文件系統不同,centos7-xfs;centos6.5-ext4

5.其他

 

一.安裝系統

1.新建虛擬機:

1)新建虛擬機centos7.2:

wps714E.tmp[11]

wps714F.tmp[8]

2)編輯虛擬機:

wps7160.tmp[8]

wps7161.tmp[8]

2.下載並安裝centos7.2系統

1)下載地址:

http://mirrors.aliyun.com/centos/7.2.1511/isos/x86_64/CentOS-7-x86_64-DVD-1511.iso

2)安裝centos7.2:

(1)回車進行安裝:

wps7162.tmp[8]

wps7163.tmp[8]

(2)設置安裝過程中的語言:

wps7164.tmp[8]

(3)設置鍵盤佈局:

wps7165.tmp[8]

wps7176.tmp[8]

wps7177.tmp[8]

wps7178.tmp[8]

(4)軟件選擇:

wps7179.tmp[8]

wps717A.tmp[8]

(5)配置安裝位置:

wps717B.tmp[8]

wps717C.tmp[8]

(6)配置網絡:

wps718C.tmp[8]

wps718D.tmp[8]

(7)開始安裝:

wps718E.tmp[8]

(8)設置root密碼:

wps718F.tmp[8]

wps7190.tmp[8]

等待安裝完成。

wps7191.tmp[8]

二.配置centos7系統

1.添加用戶:

wps7192.tmp[8]

wps71A3.tmp[8]

添加su控制:允許linuxfan用戶能使用su命令。

wps71A4.tmp[8]

驗證:

wps71A5.tmp[8]

2.關閉防火牆和selinux:

查看防火牆狀態,並設置爲開機不啓動:語法systemctl start|restart|stop|reload 服務名

wps71A6.tmp[8]

查看selinux 狀態並設置爲禁用:

wps71A7.tmp[8]

3.設置network和主機名:

1)設置永久的主機名:永久修改主機名的語法:hostnamectl set-hostname 名

wps71A8.tmp[8]

2)修改網絡的顯示名稱:network-scripts|NetworkManager(nmcli)

wps71B9.tmp[8]

wps71BA.tmp[8]

reboot後進行驗證:

wps71BB.tmp[8]

3)修改文件設置ip地址:

wps71BC.tmp[8]

wps71BD.tmp[8]

:wq

重啓服務並驗證:

wps71BE.tmp[8]

4)使用nmcli命令設置網絡:nmcli help|man nmcli-->EXAMPLE

Red Hat Enterprise Linux 7和CentOS 7的一個新特性是默認的網絡服務由NetworkManager提供,這是一個動態的網絡控制和配置守護進程,它在網絡設備和連接可用時保持鏈接正常,同時也提供了典型的ifcfg類型的配置文件。NetworkManager可以用於下面這些連接:Ethernet、 VLANs、橋接、Bonds、Teams、 Wi-Fi、 移動寬帶 (比如 3G)和IP-over-InfiniBand(IPoIB)。NetworkManager可以由命令行工具nmcli控制。

wps71BF.tmp[8]

4.服務管理:語法systemctl 控制類型  服務名稱

1)查看服務狀態:

查看所有服務:

wps71CF.tmp[8]

查看所有服務名稱及狀態:

wps71D0.tmp[8]

2)停止服務:stop停止、disable開機不啓動

wps71D1.tmp[8]

3)啓動服務:start啓動、enable設置爲開機啓動

wps71D2.tmp[8]

5.配置yum源並update系統:yum的使用基本和centos6一致

1)測試網絡連通性:

wps71D3.tmp[8]

2)更新系統:

wps71D4.tmp[8]

wps71D5.tmp[8]

3)添加yum源:

使用xshell連接:

wps71E6.tmp[8]

[root@www ~]# yum -y install yum-plugin-priorities   ##安裝優先級插件

[root@www ~]# sed -i -e "s/\]$/\]\npriority=1/g" /etc/yum.repos.d/CentOS-Base.repo  ##設置基本yum源的優先級爲1

[root@www ~]# yum -y install epel-release   ##安裝epel源

[root@www ~]# sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo ##設置優先級爲5

[root@www ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo ##禁用epel源

[root@www ~]# yum -y install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm ##安裝rpmforge的源

[root@www ~]# sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/rpmforge.repo  ##設置優先級爲10

[root@www ~]# sed -i -e "s/enabled = 1/enabled = 0/g" /etc/yum.repos.d/rpmforge.repo ##禁用yum源

使用方法:yum --enablerepo=rpmforge install [Package]

6.配置vim:

[root@www ~]# yum -y install vim-enhanced

[root@www ~]# echo alias vi='vim' >>/etc/profile  ##設置別名

[root@www ~]# source /etc/profile  ##加載

[root@www ~]# alias  ##驗證

[root@www ~]# vi  ~linuxfan/.vimrc  ##給linuxfan用戶設置vim

set wrap  ##不換行

highlight Comment ctermfg=LightCyan   ##低亮度顯示

set showmatch  ##顯示匹配

syntax on  ##開啓語法提示

set list  

set number   #顯示行號

set incsearch 

set hlsearch

set history=50

set fileformats=unix,dos  ##設置文件的格式

set fileencodings=cp936,gb18030,gbk,gb2312,utf-8,ucs-bom,latin-1 ##避免亂碼

set ts=4  ##將tab的長度設置爲4個空格長度

:wq

驗證效果:

[root@www ~]# su - linuxfan

上一次登錄:日 6月  5 23:46:03 CST 2016pts/0 上

[linuxfan@www ~]$ vim .vimrc

  1 set wrap$

  2 set showmatch$

  3 syntax on$

  4 set list $

  5 set number $

7.配置sudo:

1)爲用戶linuxfan添加sudo,除關機外的其他所有操作:

[root@www ~]# visudo

Cmnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, /sbin/poweroff, /sbin/reboot, /sbin/init

linuxfan        ALL=(ALL) ALL,!SHUTDOWN

Defaults logfile=/var/log/sudo.log  

:wq

2)驗證:

[linuxfan@www ~]$ sudo cat /etc/passwd

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

[sudo] password for linuxfan: 輸入自己的密碼

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

[linuxfan@www ~]$ sudo ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:de:3d:a0 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.150/24 brd 192.168.1.255 scope global eth0

       valid_lft forever preferred_lft forever

[linuxfan@www ~]$

[linuxfan@www ~]$ sudo reboot

對不起,用戶 linuxfan 無權以 root 的身份在 www.linuxfan.cn 上執行 /sbin/reboot。

[linuxfan@www ~]$ sudo -l

匹配此主機上 linuxfan 的默認條目:

    requiretty, !visiblepw, always_set_home, env_reset,

    env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR

    LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG

    LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE

    LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES",

    env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER

    LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS

    _XKB_CHARSET XAUTHORITY",

    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

用戶 linuxfan 可以在該主機上運行以下命令:

    (ALL) ALL

    (ALL) ALL, (ALL) !/sbin/halt, !/sbin/shutdown,

    !/sbin/poweroff, !/sbin/reboot, !/sbin/init

[linuxfan@www ~]$

[root@www ~]# cat /var/log/sudo.log   ##查看日誌

Jun  6 00:06:54 : linuxfan : TTY=pts/0 ; PWD=/home/linuxfan ; USER=root ;

    COMMAND=list

8.配置sshd:

[root@www ~]# sed -i -e '49s/^#//g' /etc/ssh/sshd_config  ##啓用49行配置

[root@www ~]# sed -i -e '49s/yes/no/g' /etc/ssh/sshd_config  ##禁止root使用ssh登錄

[root@www ~]# sed -i '/^GSS/s/yes/no/g' /etc/ssh/sshd_config   ##禁用GSSAPI認證加快登錄速度

[root@www ~]# systemctl restart sshd   ##重新啓動服務

[root@www ~]# systemctl enable sshd  ##設置爲開機啓動

[root@www ~]# systemctl status sshd  ##查看狀態

● sshd.service - OpenSSH server daemon

   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)

   Active: active (running) since 一 2016-06-06 00:16:26 CST; 1min 3s ago

9.破解root密碼:

1)在grub菜單中選擇系統並安裝E鍵

wps71E7.tmp[8]

2)定位到linux16行,將內核參數ro改爲rw init=/sysroot/bin/sh然後按下ctrl+x進入單用戶模式。

wps71E8.tmp[8]

3)在單用戶模式下使用如下命令完成修改密碼:

chroot /sysroot  ##訪問系統

LANG=C  ##臨時設置環境語言爲C

echo pwd@123 |passwd --stdin root  ##將root的密碼修改爲pwd@123

touch /.autorelabel  ##更新系統信息

exit  ##退出系統

reboot  ##重啓

wps71E9.tmp[8]

三.安裝docker

[root@www ~]# yum -y install docker

[root@www ~]# rpm -qa |grep docker

docker-forward-journald-1.9.1-40.el7.centos.x86_64

docker-common-1.9.1-40.el7.centos.x86_64

docker-selinux-1.9.1-40.el7.centos.x86_64

docker-1.9.1-40.el7.centos.x86_64

[root@www ~]# rpm -ql docker

[root@www ~]# rpm -ql docker-forward-journald

[root@www ~]# rpm -ql docker-common

四.docker的基本使用

[root@www ~]# systemctl start docker

[root@www ~]# systemctl enable docker

Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

[root@www ~]# systemctl status docker

● docker.service - Docker Application Container Engine

   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)

   Active: active (running) since 一 2016-06-06 00:24:14 CST; 21s ago

     Docs: http://docs.docker.com

Main PID: 40454 (sh)

   CGroup: /system.slice/docker.service

           ├─40454 /bin/sh -c /usr/bin/docker-current daemon...

           ├─40456 /usr/bin/docker-current daemon --selinux-...

           └─40457 /usr/bin/forward-journald -tag docker

[root@www ~]# docker pull centos  ##自動下載官方的image鏡像centos的last版

Using default tag: latest

Trying to pull repository docker.io/library/centos ... latest: Pulling from library/centos

1544084fad81: Pull complete

df0fc3863fbc: Pull complete

a3d54b467fad: Pull complete

a65193109361: Pull complete

Digest: sha256:1a62cd7c773dd5c6cf08e2e28596f6fcc99bd97e38c9b324163e0da90ed27562

Status: Downloaded newer image for docker.io/centos:latest

[root@www ~]# docker run centos /bin/echo "welcome to the docker world"  ##在容器中執行命令

Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.

welcome to the docker world

[root@www ~]# vi /etc/sysconfig/docker-storage  ##修改存儲參數

DOCKER_STORAGE_OPTIONS="--storage-opt dm.no_warn_on_loop_devices=true"

:wq

[root@www ~]# systemctl restart docker  ##重啓docker

[root@www ~]# docker run -i -t centos /bin/bash  ##登錄容器的console控制檯

[root@0faf7bf4e1b4 /]# uname -a

Linux 0faf7bf4e1b4 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

[root@0faf7bf4e1b4 /]# yum -y install iproute

[root@0faf7bf4e1b4 /]# ip a; exit

[root@www ~]# docker run -i -t centos /bin/bash   ##登錄容器,按下ctrl+p,ctrl+q保留容器會話

[root@4b20678656bb /]# [root@www ~]#

[root@www ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED    STATUS     PORTS       NAMES

4b20678656bb  centos   "/bin/bash"    38secondsago   Up     38seconds  sick_brattain

[root@www ~]# docker attach 4b20678656bb  ##連接到容器,然後ctrl+p,ctrl+q退出

[root@4b20678656bb /]# [root@www ~]#

[root@www ~]#

[root@www ~]# docker kill 4b20678656bb  ##殺死容器

4b20678656bb

[root@www ~]# docker ps  ##驗證

CONTAINER ID  IMAGE      COMMAND    CREATED   STATUS       PORTS   NAMES

[root@www ~]#

五.使用docker部署apache-httpd

[root@www ~]# docker images  ##查看docker鏡像

REPOSITORY     TAG       IMAGE ID            CREATED             VIRTUAL SIZE

docker.io/centos  latest     a65193109361        3 days ago             196.7 MB

[root@www ~]# docker ps -a |head -2

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES

5a36bf5d4064        centos              "/bin/bash -c 'yum -y"   2 minutes ago       Exited (0) 38 seconds ago                         clever_mcnulty

[root@www ~]# docker commit 5a36bf5d4064 my_image/centos_httpd  ##添加鏡像

0cfbe60b45cec5311e9da53abbac29bac34d77f0eaa47806bdfaccde0cb8a1c1

[root@www ~]# docker images

REPOSITORY             TAG     IMAGE ID       CREATED         VIRTUAL SIZE

my_image/centos_httpd   latest    0cfbe60b45ce     42 seconds ago      310.9 MB

docker.io/centos         latest    a65193109361     3 days ago          196.7 MB

[root@www ~]#

[root@www ~]# docker run my_image/centos_httpd ls /usr/sbin/httpd ##在容器的鏡像中查看

/usr/sbin/httpd

[root@www ~]# ls /usr/sbin/httpd  ##在宿主中查看

ls: 無法訪問/usr/sbin/httpd: 沒有那個文件或目錄

[root@www ~]# docker run -it -p 8081:80 my_image/centos_httpd /bin/bash

[root@18f49a674173 /]# /usr/sbin/httpd &

[1] 13

[root@18f49a674173 /]# AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

[1]+  Done                    /usr/sbin/httpd

[root@18f49a674173 /]# echo "httpd on Docker Container" > /var/www/html/index.html ##新建首頁

[root@18f49a674173 /]# [root@www ~]#   ##ctrl+p, ctrl+q保留鏡像進程並返回宿主機

[root@www ~]# docker ps

CONTAINER ID      IMAGE    COMMAND    CREATED   STATUS    PORTS     NAMES

18f49a674173 my_image/centos_httpd  "/bin/bash" 4 minutes ago   Up 4 minutes        0.0.0.0:8081->80/tcp   adoring_heisenberg

訪問:

wps71F9.tmp[9]

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