目前大多數的linux伺服器使用的都是centos5,6;但是在不久的将來7一定會成為主流,接下來我将為大家詳細的介紹centos7的一些常用的技術。
掌握以下技術,可以輕松玩轉centos7。
注意:
centos7和centos6.5的不同:
1.網絡管理方式不同
2.服務管理方式不同
3.防火牆管理方式不同
4.檔案系統不同,centos7-xfs;centos6.5-ext4
5.其他
一.安裝系統
1.建立虛拟機:
1)建立虛拟機centos7.2:
2)編輯虛拟機:
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)回車進行安裝:
(2)設定安裝過程中的語言:
(3)設定鍵盤布局:
(4)軟體選擇:
(5)配置安裝位置:
(6)配置網絡:
(7)開始安裝:
(8)設定root密碼:
等待安裝完成。
二.配置centos7系統
1.添加使用者:
添加su控制:允許linuxfan使用者能使用su指令。
驗證:
2.關閉防火牆和selinux:
檢視防火牆狀态,并設定為開機不啟動:文法systemctl start|restart|stop|reload 服務名
檢視selinux 狀态并設定為禁用:
3.設定network和主機名:
1)設定永久的主機名:永久修改主機名的文法:hostnamectl set-hostname 名
2)修改網絡的顯示名稱:network-scripts|NetworkManager(nmcli)
reboot後進行驗證:
3)修改檔案設定ip位址:
:wq
重新開機服務并驗證:
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控制。
4.服務管理:文法systemctl 控制類型 服務名稱
1)檢視服務狀态:
檢視所有服務:
檢視所有服務名稱及狀态:
2)停止服務:stop停止、disable開機不啟動
3)啟動服務:start啟動、enable設定為開機啟動
5.配置yum源并update系統:yum的使用基本和centos6一緻
1)測試網絡連通性:
2)更新系統:
3)添加yum源:
使用xshell連接配接:
[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個空格長度
驗證效果:
[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
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
[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
[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鍵
2)定位到linux16行,将核心參數ro改為rw init=/sysroot/bin/sh然後按下ctrl+x進入單使用者模式。
3)在單使用者模式下使用如下指令完成修改密碼:
chroot /sysroot ##通路系統
LANG=C ##臨時設定環境語言為C
echo pwd@123 |passwd --stdin root ##将root的密碼修改為pwd@123
touch /.autorelabel ##更新系統資訊
exit ##退出系統
reboot ##重新開機
三.安裝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 ##自動下載下傳官方的p_w_picpath鏡像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 p_w_picpath 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"
[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@www ~]#
[root@www ~]# docker kill 4b20678656bb ##殺死容器
4b20678656bb
[root@www ~]# docker ps ##驗證
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
五.使用docker部署apache-httpd
[root@www ~]# docker p_w_picpaths ##檢視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_p_w_picpath/centos_httpd ##添加鏡像
0cfbe60b45cec5311e9da53abbac29bac34d77f0eaa47806bdfaccde0cb8a1c1
[root@www ~]# docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
my_p_w_picpath/centos_httpd latest 0cfbe60b45ce 42 seconds ago 310.9 MB
docker.io/centos latest a65193109361 3 days ago 196.7 MB
[root@www ~]# docker run my_p_w_picpath/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_p_w_picpath/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保留鏡像程序并傳回主控端
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES