目前大多数的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连接:
[[email protected] ~]# yum -y install yum-plugin-priorities ##安装优先级插件
[[email protected] ~]# sed -i -e "s/\]$/\]\npriority=1/g" /etc/yum.repos.d/CentOS-Base.repo ##设置基本yum源的优先级为1
[[email protected] ~]# yum -y install epel-release ##安装epel源
[[email protected] ~]# sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo ##设置优先级为5
[[email protected] ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo ##禁用epel源
[[email protected] ~]# yum -y install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm ##安装rpmforge的源
[[email protected] ~]# sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/rpmforge.repo ##设置优先级为10
[[email protected] ~]# sed -i -e "s/enabled = 1/enabled = 0/g" /etc/yum.repos.d/rpmforge.repo ##禁用yum源
使用方法:yum --enablerepo=rpmforge install [Package]
6.配置vim:
[[email protected] ~]# yum -y install vim-enhanced
[[email protected] ~]# echo alias vi=‘vim‘ >>/etc/profile ##设置别名
[[email protected] ~]# source /etc/profile ##加载
[[email protected] ~]# alias ##验证
[[email protected] ~]# 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
验证效果:
[[email protected] ~]# su - linuxfan
上一次登录:日 6月 5 23:46:03 CST 2016pts/0 上
[[email protected] ~]$ vim .vimrc
1 set wrap$
2 set showmatch$
3 syntax on$
4 set list $
5 set number $
7.配置sudo:
1)为用户linuxfan添加sudo,除关机外的其他所有操作:
[[email protected] ~]# 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)验证:
[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] ~]$
[[email protected] ~]$ sudo reboot
对不起,用户 linuxfan 无权以 root 的身份在 www.linuxfan.cn 上执行 /sbin/reboot。
[[email protected] ~]$ 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
[[email protected] ~]$
[[email protected] ~]# cat /var/log/sudo.log ##查看日志
Jun 6 00:06:54 : linuxfan : TTY=pts/0 ; PWD=/home/linuxfan ; USER=root ;
COMMAND=list
8.配置sshd:
[[email protected] ~]# sed -i -e ‘49s/^#//g‘ /etc/ssh/sshd_config ##启用49行配置
[[email protected] ~]# sed -i -e ‘49s/yes/no/g‘ /etc/ssh/sshd_config ##禁止root使用ssh登录
[[email protected] ~]# sed -i ‘/^GSS/s/yes/no/g‘ /etc/ssh/sshd_config ##禁用GSSAPI认证加快登录速度
[[email protected] ~]# systemctl restart sshd ##重新启动服务
[[email protected] ~]# systemctl enable sshd ##设置为开机启动
[[email protected] ~]# 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 [email protected] |passwd --stdin root ##将root的密码修改为[email protected]
touch /.autorelabel ##更新系统信息
exit ##退出系统
reboot ##重启
三.安装docker
[[email protected] ~]# yum -y install docker
[[email protected] ~]# 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
[[email protected] ~]# rpm -ql docker
[[email protected] ~]# rpm -ql docker-forward-journald
[[email protected] ~]# rpm -ql docker-common
四.docker的基本使用
[[email protected] ~]# systemctl start docker
[[email protected] ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# vi /etc/sysconfig/docker-storage ##修改存储参数
DOCKER_STORAGE_OPTIONS="--storage-opt dm.no_warn_on_loop_devices=true"
:wq
[[email protected] ~]# systemctl restart docker ##重启docker
[[email protected] ~]# docker run -i -t centos /bin/bash ##登录容器的console控制台
[[email protected] /]# 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
[[email protected] /]# yum -y install iproute
[[email protected] /]# ip a; exit
[[email protected] ~]# docker run -i -t centos /bin/bash ##登录容器,按下ctrl+p,ctrl+q保留容器会话
[[email protected] /]# [[email protected] ~]#
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4b20678656bb centos "/bin/bash" 38secondsago Up 38seconds sick_brattain
[[email protected] ~]# docker attach 4b20678656bb ##连接到容器,然后ctrl+p,ctrl+q退出
[[email protected] /]# [[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# docker kill 4b20678656bb ##杀死容器
4b20678656bb
[[email protected] ~]# docker ps ##验证
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[[email protected] ~]#
五.使用docker部署apache-httpd
[[email protected] ~]# docker images ##查看docker镜像
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
docker.io/centos latest a65193109361 3 days ago 196.7 MB
[[email protected] ~]# 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
[[email protected] ~]# docker commit 5a36bf5d4064 my_image/centos_httpd ##添加镜像
0cfbe60b45cec5311e9da53abbac29bac34d77f0eaa47806bdfaccde0cb8a1c1
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# docker run my_image/centos_httpd ls /usr/sbin/httpd ##在容器的镜像中查看
/usr/sbin/httpd
[[email protected] ~]# ls /usr/sbin/httpd ##在宿主中查看
ls: 无法访问/usr/sbin/httpd: 没有那个文件或目录
[[email protected] ~]# docker run -it -p 8081:80 my_image/centos_httpd /bin/bash
[[email protected] /]# /usr/sbin/httpd &
[1] 13
[[email protected] /]# 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
[[email protected] /]# echo "httpd on Docker Container" > /var/www/html/index.html ##新建首页
[[email protected] /]# [[email protected] ~]# ##ctrl+p, ctrl+q保留镜像进程并返回宿主机
[[email protected] ~]# 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
访问: