1. 安装xinetd
cat /etc/services | grep "daytime"
# for ubuntu
sudo apt-get install xinetd
如果是centos/red hat
sudo yum install xinetd.x86_64
centos默认普通用户不能用sudo: https://blog.csdn.net/fareast_mzh/article/details/81908198
2. 改配置文件
sudo emacs /etc/inetd.conf
# /etc/inetd.conf: see inetd(8) for further informations.
#
# Internet superserver configuration database
#
#
# Lines starting with "#:LABEL:" or "#<off>#" should not
# be changed unless you know what you are doing!
#
# If you want to disable an entry so it isn't touched during
# package updates just comment it out with a single '#' character.
#
# Packages should modify this file by using update-inetd(8)
#
# <service_name> <sock_type> <proto> <flags> <user> <server_path> <args>
#
#:INTERNAL: Internal services
#discard stream tcp nowait root internal
#discard dgram udp wait root internal
daytime stream tcp nowait root internal
#time stream tcp nowait root internal
#:STANDARD: These are standard services.
#:BSD: Shell, login, exec and talk are BSD protocols.
#:MAIL: Mail, news and uucp services.
#:INFO: Info services
#:BOOT: TFTP service is provided primarily for booting. Most sites
# run this only on machines acting as "boot servers."
#:RPC: RPC based services
#:HAM-RADIO: amateur-radio services
#:OTHER: Other services
#<off># bbs stream tcp wait root /usr/sbin/ax25-node ax25-node
去掉daytime开头的这行前面的#注释
sudo emacs /etc/xinetd.d/daytime
# default: off
# description: An internal xinetd service which gets the current system time
# then prints it out in a format like this: "Wed Nov 13 22:30:27 EST 2002".
# This is the tcp version.
service daytime
{
disable = no
type = INTERNAL
id = daytime-stream
socket_type = stream
protocol = tcp
user = root
wait = no
}
# This is the udp version.
service daytime
{
disable = no
type = INTERNAL
id = daytime-dgram
socket_type = dgram
protocol = udp
user = root
wait = yes
}
disable=yes改为disable=no
对于centos/red hat
修改 /etc/xinetd.d/daytime-stream和 /etc/xinetd.d/ daytime-dgram 中的disable为no;
3. 启动服务
sudo /etc/init.d/xinetd restart
centos:
sudo /bin/systemctl restart xinetd.service
4. 测试
nc localhost daytime
12 MAR 2019 01:09:55 UTC
5. udp.php
<?php
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) {
printf("ERROR: %d - %s\n", $errno, $errstr);
exit($errno);
}
fwrite($fp, "\n");
echo fread($fp, 26);
fclose($fp);
$ php udp.php
12 MAR 2019 01:11:04 UTC