NTP server 时间同步服务器搭建

#修改时区
 
在/usr/share/zoneinfo/Asia/目录下,ls -l Shanghai查看该文件实际指向哪一个文件,将该文件拷贝为localtime,放入/etc、目录下


#允许ntp通过防火墙

sudo ufw allow ntp


交叉编译ntp步骤:
    1.获取ntp源码:    sudo apt-get source ntp(需要联网,使用该命令,可以获取支持你使用虚拟机的ntp源码包,若自行下载,注意匹配你的虚拟机)
                    dpkg-source -x ***.dsc
    2.进入步骤1生成的ntp源码目录,如果是arm平台,先后执行c和a,PowerPC平台,先后执行d和b,Ubuntu下安装使用sudo apt-get install ntp ntpdate
                    --with-openssl-libdir --with-openssl-incdir分别表示编译出来的openssl的库路径和头文件路径
                    host和CC根据具体使用的编译工具链更改
                    ntp server的ntp.conf文件内容,看本文档的最后。
    3.看完整个文档后,执行完对应的操作在使用ntp服务器
 
a.交叉编译ntp:

./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-xilinx-linux-gnueabi CC=arm-xilinx-linux-gnueabi-gcc --with-yielding-select=yes --with-openssl-libdir=/home/linux/openssl-1.0.1f/install/lib --with-openssl-incdir=/home/linux/openssl-1.0.1f/install/include
make ARCH=arm CROSS_COMPILE=arm-xilinx-linux-gnueabi-


    (这里直接make即可,不必使用上面的那条命令,make遇到问题,keyword-gen不能执行,解决办法,make clean , ./configure, make,mv ./ntpd/keyword-gen ./,然后进行交叉编译即可)
 
b.PowerPC:
    将nxp SDK中的交叉编译工具链提取出来,放到用户目录下,使用source environment-setup-ppce500mc-fsl-linux
    

LD=$LD ./configure --prefix=$PWD/install --exec-prefix=$PWD/install --build=x86_64-linux --host=powerpc-fsl-linux $CONFIGURE_FLAGS --with-openssl-libdir=/home/linux/powerpc_ntf/openssl-1.0.1f --with-openssl-incdir=/home/linux/powerpc_ntf/openssl-1.0.1f/include
 
make ARCH=powerpc CROSS_COMPILE=$CC


    在PowerPC下编译ntp时,交叉编译openssl没有使用make install(openssl的库路径和头文件路径一定要对)
 
c.交叉编译openssl:
    

./Configure linux-elf-arm -DB_ENDIAN arm:'arm-xilinx-linux-gnueabi-gcc' no-shared --prefix=/home/linux/openssl-1.0.1f/install
make && make install (--prefix make install 的路径)


    如果make install 失败,只要编译成功即可,编译ntp时,指定openssl的库路径和头文件路径即可(一定是编译出来的库)
d.PowerPC:
    

/Configure linux-elf-powerpc -DB_ENDIAN powerpc:'powerpc-fsl-linux-gcc' no-shared --prefix=/home/linux/powerpc_ntf/openssl-1.0.1f/install
更改Makefile的CC,去掉前边的$(CROSS_COMPILE)
make && make install (--prefix make install de 路径)


    如果make install 失败,只要编译成功即可,编译ntp时,指定openssl的库路径和头文件路径即可(一定是编译出来的库)
ubuntu获取软件包源码:
    #获取源码包
    sudo apt-get source pakege_name
    #解压所有的包,生成源码目录
    dpkg-source -x ***.dsc
    #获取依赖
    sudo apt-get build-dep xxx
 
 
定时任务:
    板子下输入crontab -e后输入
 
    * * * * * ntpdate <ntp_server ip> >&1
 
    mkdir -p /var/spool/cron/crontabs
 
    每两分钟同步一次
    */2 * * * * ntpdate <ntp_server ip> >&1
 
设置时间:
    date -s 2019.10.23-10:10:10
 
 
Task:
 
    board:                    ubuntu:
        server(ntpd)            client(ntpdate)        (完成)
        server(ntpd)            client(ntpd)        (完成)
 
        client(ntpdate)            server(ntpd)        (完成)
        client(ntpd)            server(ntpd)        (完成,但板子的时间与ntp server时间差过大时,无法使用ntpd无法同步自身时间)
        client(ntpdate + crond)    server(ntpd)        (完成)
 
获取rtc时间:
    hwclock -w
    
 
FAQ:
    
    ntp客户端的时间大于ntp服务器的时间,客户端无法同步时间(板子做客户端)
 
    板子无法使用ntpd同步本身时间
 
NOTE:
 
    无论是Ubuntu运行ntpd还是板子运行ntpd,若运行的一端作为被同步的一方,则其时间大于服务器时间时,无法使用ntpd进行同步
    
 
    板子运行ntpdate 出现 sername not supported ai_socketype问题时,请确保/etc/目录下存在services文件,且加入了相应的内容,详细内容请往下看
    
    交叉编译命令(依赖openssl,需要先交叉编译openssl)

./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-xilinx-linux-gnueabi CC=arm-xilinx-linux-gnueabi-gcc --with-yielding-select=yes --with-openssl-libdir=/home/linux/openssl-1.0.1f/install/lib --with-openssl-incdir=/home/linux/openssl-1.0.1f/install/include
 
 make ARCH=arm CROSS_COMPILE=arm-xilinx-linux-gnueabi-


    板子上运行:
                运行ntp server时,当客户端与服务器时间相差太大时,可能会导致失败,应先使用ntpdate 同步本机时间后开启ntp server
                另外需要ntp.conf文件,在/etc目录下,同时在/etc目录下的services文件下新增一下两行内容,若没有services文件,则新建该文件
                            ntp    123/tcp
                            ntp    123/udp
                
                客户端时间大于服务器时间时,使用ntpdate -b 选项同步服务器时间
 
                客户端运行ntpdate -b ip 或者ntpdate ip
 
                板子使用ntpd同步时间时,配置文件下面的内容会导致失败,应该去掉,原因不明,目前Ubuntu没问题
                    server  127.127.1.0
                    fudge   127.127.1.0 stratum 8
 
    板子的文件系统:
                在/etc目录下,需要services和ntp.conf文件
                在/bin目录下放入编译好的ntpd和ntpdate
 
    板子上定时同步ntp服务器的时间:
                文件系统需要有/var/spool/cron/crontabs目录
                在该目录下新建root文件,文件内容如下:
                #每分钟执行一次
                * * * * * ntpdate 192.168.11.165 >&1
                

linux@ubuntu:~$ ntpq -p
     remote           refid      st                 t             when                             poll                     reach   delay   offset      jitter
============================================================================================================================================================
 192.168.11.165  LOCAL(0)        11                 u           11                               64                        3        0.583      -691911       0.616
 
服务器的ip地址    服务器的上层    服务器的上层层数    u:单播        上一次校正时间与现在时间的差    查询服务器的时间间隔            延时    时间偏移    
                                                    b:广播
                                                    i:本地


ntp server 配置文件:
        

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
 
        driftfile /var/lib/ntp/ntp.drift
 
        # Enable this if you want statistics to be logged.
        #statsdir /var/log/ntpstats/
 
        statistics loopstats peerstats clockstats
        filegen loopstats file loopstats type day enable
        filegen peerstats file peerstats type day enable
        filegen clockstats file clockstats type day enable
 
        #上层ntp server
        server  192.168.11.113 perfer
        #本地时间提供给客户端,如果上层server无法提供时间
        server  127.127.1.0
        fudge   127.127.1.0 stratum 8
 
        #不允许公网ipv4 和ipv6客户端访问
        # By default, exchange time with everybody, but don't allow configuration.
        restrict -4 default kod notrap nomodify nopeer noquery
        restrict -6 default kod notrap nomodify nopeer noquery
 
        #add 允许11网段同步本服务器的时间,可指定只允许某个ip
        restrict 192.168.11.0 mask 255.255.255.0
        #允许上层 server所有权限
        restrict 192.168.11.113
        # Local users may interrogate the ntp server more closely.
        restrict 127.0.0.1
        restrict ::1


//忽略

if [ -f /mnt/mmcblk0p2/init.sh ]
then
        echo "++ Running user script init.sh from /mnt/mmcblk0p2/init.sh"
        source /mnt/mmcblk0p2/init.sh
fi

设置时间获取时间:

time.c:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

void gettime(){
	struct tm *get_tm;
    struct timeval get_tv;
 
    gettimeofday(&get_tv, NULL);
    get_tm = localtime(&get_tv.tv_sec);
 
    printf("%s \n", asctime(get_tm));
    printf("year %d mon %d mday %d hour %d min %d sec  %d \n",  get_tm->tm_year + 1900, get_tm->tm_mon + 1,
                                                                get_tm->tm_mday, get_tm->tm_hour,
                                                                get_tm->tm_min , get_tm->tm_sec);
	
}

int settime(){
    struct tm set_tm;
    struct timeval set_tv;
    time_t timep;

    set_tm.tm_year = 2018 - 1900;
    set_tm.tm_mon = 6 - 1;
    set_tm.tm_mday = 10; 
    set_tm.tm_hour = 15; 
    set_tm.tm_min = 55; 
    set_tm.tm_sec = 5;

    timep = mktime(&set_tm);

    set_tv.tv_sec = timep;
    set_tv.tv_usec = 0;

    if (settimeofday(&set_tv, (struct timezone *)0) < 0) {
        printf("set time error !\n");
        return -1; 
    }   
}

int main(void)
{
	pid_t status;
    gettime();
	settime();
	
	status = system("la");
	if(status ==-1){
		printf("system error\n");
	}else{
		printf("exit status:0x%x\n",status);
		if(WIFEXITED(status)){
			if(WEXITSTATUS(status) == 0){
				printf("run shell ok\n");
			}
			else{
				printf("run shell error:%d\n",WEXITSTATUS(status));
			}
		}else{
			printf("exit status:%d\n",status);
		}
		
	}
	
    return 0;
}


services:(ntp时间同步服务器需要使用的文件,一般在/etc/目录下,如果没有该文件,应该新建并添加一下内容)

ntp	123/tcp
ntp	123/udp

 server_start_ntpd.sh:(用于定时任务crond,作用是监测ntpd程序是否在运行,如果程序挂死,则重新拉起该程序)

if [ -z "$(pgrep ntpdate)" ]
then
	if [ -z "$(pgrep ntp)" ] 
	then
       		echo "ntpd is not start,will start it..." >&1
        	ntpd
	else
		echo "ntpd is runing..." >&1
	fi
else
	echo "kill ntpdate" >&1
	kill $(pgrep ntpdate)
	ntpd	
fi

 server_ntp_init.sh:(改脚本应该在板卡的init.sh里使用,目的是为运行ntp服务器做前期准备)

mkdir -p /var/spool/cron/crontabs                                                 
rm -rf /bin/ntpd /usr/sbin/ntpd

cp -rf /mnt/mmcblk0p2/test/server_root /var/spool/cron/crontabs/root                         
cp -rf /mnt/mmcblk0p2/test/server_start_ntpd.sh /bin/ && chmod 777 /bin/server_start_ntpd.sh    
cp -rf /mnt/mmcblk0p2/test/kill_ntp.sh /bin/ && chmod 777 /bin/kill_ntp.sh
cp -rf /mnt/mmcblk0p2/test/ntp_server.conf /etc/ntp.conf                                         
cp -rf /mnt/mmcblk0p2/test/localtime /etc/                                        
cp -rf /mnt/mmcblk0p2/test/services /etc/                                         
cp -rf /mnt/mmcblk0p2/test/ntpd /bin/ && chmod 777 /bin/ntpd 
               
cp -rf /mnt/mmcblk0p2/test/ntpdate /bin/ && chmod 777 /bin/ntpdate                
crond    

ntpd

#time

ntp_server.conf:(该文件是ntp server的配置文件,在板卡上实际应为/etc/ntp.conf,解释一下,这里的server server_ip prefer,是指本ntp服务器的上次服务器)

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

server server_ip prefer

#local clock
server  127.127.1.0    
fudge   127.127.1.0 stratum 8

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

#add
restrict 192.168.11.0 mask 255.255.255.0
restrict server_ip 
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

server_root:(该文件用于crond程序,用于启动定时任务,每分钟执行一次server_start_ntpd.sh)

* * * * * echo "runing" >&1
* * * * * server_start_ntpd.sh >&1

client_ntp_init.sh:(ntp 客户端,暂称为ntp客户端,该文件也是用于板卡上的init.sh,ntp的前期准备工作)
 

mkdir -p /var/spool/cron/crontabs                                                 
rm -rf /bin/ntpd /usr/sbin/ntpd

cp -rf /mnt/mmcblk0p2/test/client_root /var/spool/cron/crontabs/root                         
cp -rf /mnt/mmcblk0p2/test/client_start_ntpd.sh /bin/ && chmod 777 /bin/client_start_ntpd.sh    
cp -rf /mnt/mmcblk0p2/test/restart_ntpd.sh /bin/ && chmod 777 /bin/restart_ntpd.sh    
cp -rf /mnt/mmcblk0p2/test/kill_ntp.sh /bin/ && chmod 777 /bin/kill_ntp.sh    
cp -rf /mnt/mmcblk0p2/test/ntp_client.conf /etc/ntp.conf                                         
cp -rf /mnt/mmcblk0p2/test/localtime /etc/                                        
cp -rf /mnt/mmcblk0p2/test/services /etc/                                         
cp -rf /mnt/mmcblk0p2/test/ntpd /bin/ && chmod 777 /bin/ntpd
                
cp -rf /mnt/mmcblk0p2/test/ntpdate /bin/ && chmod 777 /bin/ntpdate                
ntpdate -b server_ip

crond 

ntpd   

client_start_ntpd.sh:(同上)

if [ -z "$(pgrep ntpdate)" ]
then
	if [ -z "$(pgrep ntp)" ]
	then
        	echo "ntpd is not start,will start it..." >&1
		ntpdate -b server_ip
	        ntpd
	else
		echo "ntpd is runing..." >&1
	fi
else
	kill $(pgrep ntpdate)
        echo "ntpd is not start,will start it..." >&1
	ntpdate -b server_ip
	ntpd
fi

ntp_client.conf:(同上,ntp客户端的ntp服务配置,192.168.11.0为板子所使用的网段,本文档是局域网)

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

server server_ip prefer

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

#add
restrict 192.168.11.0 mask 255.255.255.0

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

restart_ntpd.sh:(用于客户端)

kill $(pgrep ntpdate)
kill $(pgrep ntp)
echo "restart.sh"
ntpdate -b server_ip
cd /mnt/mmcblk0p2/test
./ntpd

client_root:(同上,crond使用)

* * * * * echo "runing" >&1
* * * * * client_start_ntpd.sh >&1
*/10 * * * * restart_ntpd.sh >&1

这里总共有三个定时任务,第一个打印,实际上crond后台运行的话并不会打印,第二个监测ntpd程序是否在运行,挂掉则重新拉起,第三个是为了防止ntp客户端的时间大于ntp服务器的时间导致ntp客户端无法使用ntpd服务区同步时间,关于实现:该任务在一个小时60分钟内的为10的整数分钟时运行该任务,restart_ntpd.sh会先杀死ntpdate和ntpd,然后使用ntpdate -b server_ip 同步一次时间,然后重新启动ntpd服务。做第三个任务的原因很简单,ntp客户端的时间和ntp服务端的时间差距过大时,ntpd不能再同步其时间。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Server 2016可以方便地搭建NTP(Network Time Protocol)服务器,提供网络时间同步服务。下面是搭建NTP服务器的步骤: 1. 打开服务器管理器,选择角色和功能,点击下一步。 2. 选择“安装基于角色或基于功能的安装”,点击下一步。 3. 在服务器角色页面中,选择“网络时间协议(NTP)服务器”,点击下一步。 4. 点击安装,等待安装完成。 5. 安装完成后,回到服务器管理器,选择“工具”-“服务”。 6. 在服务管理窗口,找到“Windows Time”服务,鼠标右键点击,选择“属性”。 7. 在属性窗口的“常规”选项卡中,将“启动类型”设置为“自动”,点击“应用”和“确定”按钮。 8. 在“登录”选项卡中,选择“本地系统账户”,点击“应用”和“确定”按钮。 9. 回到服务管理窗口,右键点击“Windows Time”服务,选择“重启”。 完成上述步骤后,您的Windows Server 2016就会作为NTP服务器运行。其他设备可以连接到该服务器,通过NTP协议同步时间。请注意,您还需要在防火墙中打开UDP端口123,以允许其他设备访问NTP服务器。 为了确保NTP服务器提供准确的时间同步服务,您可以在服务器管理器中的“工具”-“注册表编辑器”中进行进一步配置。具体步骤是: 1. 打开注册表编辑器,导航到以下路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters。 2. 在参数文件夹中,找到“Type”键,鼠标右键点击选择“修改”。 3. 修改数值数据为“NTP”。 4. 找到“NtpServer”键,鼠标右键点击选择“修改”。在数值数据中,填写您的服务器名称或IP地址,多个服务器可用空格分隔。 5. 重启“Windows Time”服务以使更改生效。 经过以上配置,您的Windows Server 2016将提供NTP服务器服务,并与其他设备同步时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值