服务管理
特点:提供可靠的连续不间断的服务 并发特点
通过交互的方式提供服务
socket交互:应用程序通过“套接字”向网络发出请求或者应答网络请求。介于应用层和传输层之间。
socket类型:
流式scoket(sock_stream):是一种面向连接的socket,针对面向链接的tcp服务应用。
数据包scoket(sock_dgram):是一种面向无连接的socket,针对面向无连接的udp服务应用。
LINUX下的两大服务:
依赖服务:
特点:依赖于xinetd服务,没有独立的启动脚本
启动:先启动xinetd服务
eg:telnet、tftp
xinetd服务是什么?
xinetd是一个网络守护进程服务程序,又叫 超级intrnet服务,常被用来管理一些轻量级别的服务
作用:唤醒正在睡觉的服务,由xinetd服务管理的服务只有在用的时候才被唤醒,平时不占用系统资源。
独立服务:
特点:有自己的启动脚本和进程
启动:service xxx start 或者
/etc/init.d/xxx start
或者 /etc/rc.d/init.d/xxx start
区别:
[root@node1 Desktop]# file /etc/init.d/httpd
/etc/init.d/httpd: Bourne-Again shell script text executable
[root@node1 Desktop]# file /etc/rc.d/init.d/httpd
/etc/rc.d/init.d/httpd: Bourne-Again shell script text executable
[root@node1 Desktop]# ll -id /etc/init.d/httpd
655420 -rwxr-xr-x. 1 root root 3371 Aug 2 2013 /etc/init.d/httpd
[root@node1 Desktop]# ll -id /etc/rc.d/init.d/httpd
655420 -rwxr-xr-x. 1 root root 3371 Aug 2 2013 /etc/rc.d/init.d/httpd
service 调用/etc/rc.d/init.d/xxx
client ——> server
IP地址(找到服务所在的服务器)+ 端口号(访问相应的服务)
端口号的设定:
端口都是整数,范围0~65535
1~255:知名的端口号,eg:ftp 21 ssh 22 web 80 telnet 23
256~1023:通常是有系统占用的
1024~5000:客户端临时分配的端口,随机分配
大于5000:互联网中预留的端口
/etc/service 定义了系统默认注册的端口号
搭建服务流程和思路:
1、配置yum源 # ls /etc/yum.repos.d/*.repo
2、查询软件包 yum list |grep xxx
3、安装软件包 yum install xxx
4、确认软件是否安装成功 rpm -aq |grep xxx
5、查询软件列表 rpm -ql xxx
【服务端|客户端|工具包|开发包|库文件|帮助手册等】
6、了解配置文件
7、根据需求通过修改配置文件来完成服务的搭建
8、启动服务,设置开机自动启动
9、测试验证
netstat -tunlp
-t:tcp协议
-u:udp协议
-l:监听状态(listening 处于监听状态|established 已经建立连接)
-n:名称显示数字
-p:进程号和进程名
搭建依赖服务:
telnet服务:远程管理服务 linux是不允许开放
通过明文传输帐号密码 tcp 协议 23号
用于远程管理,不适合用于类unix操作系统
环境准备:
实验要求:虚拟机3台,2台linux,1台windows
固定的IP地址,网关是192.168.0.254,DNS:192.168.0.254
每个虚拟机定义快照
搭建telnet步骤:
说明:server 192.168.0.1
client 192.168.0.2
1、yum配置
2、查看软件包
# yum list|grep telnet telnet.x86_64 客户端工具 telnet-server.x86_64 服务端
3、安装服务端
#yum -y install telnet-server
4、确认|查看软件列表
/etc/xinetd.conf 主配置文件
/usr/sbin/xinetd 启动脚本
/etc/xinetd.d/telnet 子配置文件
5、了解相关配置文件
主配置文件:
# cat /etc/xinetd.conf |grep -v ^#
defaults
{
log_type = SYSLOG daemon info 日志类型,表示使用syslog来进行服务登记
log_on_failure = HOST 失败日志,失败后记录客户端的IP地址
log_on_success = PID HOST DURATION EXIT 成功日志,记录客户端的PID和ip地址
cps = 50 10 每秒50个连接,如果超出限制则需要等待10秒钟,主用于ddos攻击
instances = 50 最大连接数
per_source = 10 每个IP最大的连接数
v6only = no 不是用IPv6
groups = yes
umask = 002
}
includedir /etc/xinetd.d 外部调用目录
子配置文件:
service telnet 服务名
{
flags = REUSE 标记
socket_type = stream tcp协议
wait = no 不需要等待,服务将以多线程方式去运行
user = root 以root身份来启动服务
server = /usr/sbin/in.telnetd 启动脚本
log_on_failure += USERID
disable = yes 表示关闭服务;no表示开启服务
}
6、启动服务
vim /etc/xinetd.d/telnet
…
disable = no
# service xinetd start # chkconfig --list|grep xinetd xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
7、测试验证
server:
#netstat -tnpl |grep 23 tcp 0 0 :::23 :::* LISTEN 17060/xinetd
client:
yum -y install telnet
# telnet 192.168.0.1 23
login:user1 (普通用户,默认不允许root登录)
passwd:密码
注意:
搭建服务前:关闭防火墙和selinux!!!!
# vim /etc/selinux/config
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=enforcing ——> disabled
重启机器生效
setenforce 0 临时切换到permissive
getenforce 查看
service iptables stop
iptables -F
chkconfig iptables off 开机不自动启动
需求:不允许192.168.0.0/24网段来telnet
或者只允许192.168.0.2客户端来访问
man 5 xinetd.conf
only_from …
vim /etc/xinetd.d/telnet
….
only_from = 192.168.0.2
service xinetd restart
练习1:
只允许工作时间访问
9:30~12:00
2:00~5:30
vim /etc/xinetd.d/telnet
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
only_from = 192.168.0.2 192.168.0.3
access_times = 9:30-12:00 14:00-17:30
}
练习2:
根据telent的安装步骤,搭建tftp服务端
独立服务nfs搭建:
NFS :网络文件共享服务
在linux系统下的一种文件传输的协议,缺点该协议没有用户认证机制,并且数据在网络中明文传输的,所以该服务一般多用于局域网中。
包含的组件:
RPC:远程过程调用协议。
rpcbind: 负责nfs的数据传输,远程过程的调用
nfs: 负责共享哪些文件及权限控制
步骤:
1、关闭防火墙和selinux
2、配置yum源
3、查看软件|安装|确认
rpcbind.x86_64
nfs-utils.x86_64 nfs服务的工具包
nfs-utils-lib.x86_64 库文件包
nfs4-acl-tools.x86_64 工具包
# rpm -ql rpcbind
/etc/rc.d/init.d/rpcbind 启动脚本
/sbin/rpcbind 二进制命令
/etc/rc.d/init.d/nfs
/sbin/mount.nfs 挂载类型 mount -t nfs
/sbin/mount.nfs4
/sbin/umount.nfs 卸载
/usr/sbin/showmount 发现远端发布的共享目录
/usr/sbin/exportfs 重新加载共享目录
/etc/exports 发布共享目录的文件
# rpm -qf /etc/exports
setup-2.8.14-20.el6_4.1.noarch
4、了解配置文件
man 5 exports
EXAMPLE
# sample /etc/exports file
/ master(rw) trusty(rw,no_root_squash)
/projects proj*.local.domain(rw)
/usr *.local.domain(ro) @trusted(rw)
/home/joe pc001(rw,all_squash,anonuid=150,anongid=100)
/pub *(ro,insecure,all_squash)
/srv/www -sync,rw server @trusted @external(ro)
/foo 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build buildhost[0-9].local.domain(rw)
5、发布一个共享目录
mkdir /nfs/share -p
cp /etc/hosts /nfs/share
chmod 1777 /nfs/share
# vim /etc/exports
/nfs/share *(ro)
/nfs/share 192.168.0.0/24(rw,sync)
root_squash:如果文件的拥有者是root,默认将文件的属主和属组变成nfsnobody
no_root_squash:不管是哪个用户创建的,文件的属主和属组都是自己
all_squash:不管是哪个用户,创建的文件的属组和属主都是nfsnodody
说明:
如果再次修改/etc/exports文件可以不用重启服务,通过以下命令重新发布:
exportfs -rv
6、启动服务
# service rpcbind restart
# service nfs start
# chkconfig –level 5 nfs on
注意:先启动rpcbind服务再启动nfs服务,要不然会报错。
7、测试验证
client:showmount -e 192.168.0.1 查看远端服务器所发布的共享目录
手动挂载
mount -t nfs 192.168.0.1:/nfs/share /u01
开机自动挂载
vim /etc/fstab
192.168.0.1:/nfs/share /u01 nfs defaults,ro 0 0
触发式挂载:
auto.master
/u01 /etc/auto.share
vim /etc/auto.share
nfs -ro,soft,intr 192.168.0.1:/nfs/share
service autofs restart
示例:自动挂载家目录
server:192.168.0.1
client:192.168.0.2
步骤:
server端:
1、创建本地共享目录
mkdir -p /rhome/{u01,u02,u03}
2、授权
chmod 777 -R /rhome/u*
3、发布共享目录
vim /etc/exports
/rhome 192.168.0.0/24(rw,sync,no_root_squash)
4、更改用户环境变量:
cp -a /etc/skel/. /rhome/u01
cp -a /etc/skel/. /rhome/u02
cp -a /etc/skel/. /rhome/u03
5、重启服务
service nfs restart
client:
1、创建目录
mkdir /nfs
2、创建三个用户
useradd -M -d /nfs/u01 u01
useradd -M -d /nfs/u02 u02
useradd -M -d /nfs/u03 u03
-M:不创建用户家目录
3、挂载远端家目录
一键式触发挂载
vim /etc/auto.master
/nfs /etc/auto.rhome
vim /etc/auto.rhome
#u01 -rw,nfs 192.168.0.1:/rhome/u01
* -rw,nfs 192.168.0.1:/rhome/&
测试验证:
client:su – u01
练习:
1、新建5个用户user01~user05,密码为uplooking,第一次登录强制修改密码,每隔30天更新一次密码。
2、搭建nfs服务,在服务端发布共享目录/uplooking/{user01~user05},将/uplooking目录定义为逻辑卷,方便后期扩展,上传的文件属性属于自己。
3、将远程家目录挂载到/nfs/{user01~user05},定义超时时间为300秒
[给力]