docker ntpdate同步时间执行失败

当容器以非特权模式运行时,很多操作都没有权限执行,

[root@d3803eb23a14 /]# ntpdate hshh.org asia.pool.ntp.org
22 Nov 07:12:43 ntpdate[78]: Can't adjust the time of day: Operation not permitted

解决办法之一就是开启特权模式--privileged,但是这样一来容器里权限就过大,安全隐患也大,因此我们可以通过--cap-add来增加一些比较细致的权限,比如对于时间修改,就可以增加SYS_TIME权限,

[root@CentOS-7-2 /home]# docker run -it --cap-add=SYS_TIME --name test 165931a63fea /bin/bash
[root@6659a1445222 /]# ntpdate hshh.org asia.pool.ntp.org
22 Nov 07:15:03 ntpdate[64]: adjust time server 211.19.59.28 offset 0.032864 sec

之前遇到过ssh连接容器后闪退的问题(Docker容器内运行sshd进程,远程登录闪退(Exit status 254)),也是因为权限问题,问题根因是因为pam模块里设置ulimit时权限不够,导致鉴权失败。这个问题除了特权模式外,同样可以通过添加--cap-add添加CAP_SYS_RESOURCE权限得到解决。

更多权限可以通过man capabilities,查看用户手册,

......
       CAP_SYS_PTRACE
              Trace  arbitrary  processes  using  ptrace(2);  apply get_robust_list(2) to
              arbitrary processes; inspect processes using kcmp(2).

       CAP_SYS_RAWIO
              * Perform I/O port operations (iopl(2) and ioperm(2));
              * access /proc/kcore;
              * employ the FIBMAP ioctl(2) operation;
              * open devices  for  accessing  x86  model-specific  registers  (MSRs,  see
                msr(4))
              * update /proc/sys/vm/mmap_min_addr;
              * create  memory  mappings  at  addresses  below  the  value  specified  by
                /proc/sys/vm/mmap_min_addr;
              * map files in /proc/bus/pci;
              * open /dev/mem and /dev/kmem;
              * perform various SCSI device commands;
              * perform certain operations on hpsa(4) and cciss(4) devices;
              * perform a range of device-specific operations on other devices.

       CAP_SYS_RESOURCE
              * Use reserved space on ext2 file systems;
              * make ioctl(2) calls controlling ext3 journaling;
              * override disk quota limits;
              * increase resource limits (see setrlimit(2));
              * override RLIMIT_NPROC resource limit;
              * override maximum number of consoles on console allocation;
              * override maximum number of keymaps;
              * allow more than 64hz interrupts from the real-time clock;
              * raise msg_qbytes limit for a System V message queue above  the  limit  in
                /proc/sys/kernel/msgmnb (see msgop(2) and msgctl(2));
              * override  the  /proc/sys/fs/pipe-size-max limit when setting the capacity
                of a pipe using the F_SETPIPE_SZ fcntl(2) command.
              * use F_SETPIPE_SZ to increase the capacity of a pipe above the limit spec‐
                ified by /proc/sys/fs/pipe-max-size;
              * override /proc/sys/fs/mqueue/queues_max limit when creating POSIX message
                queues (see mq_overview(7));
              * employ prctl(2) PR_SET_MM operation;
              * set /proc/PID/oom_score_adj to a value lower than the value last set by a
                process with CAP_SYS_RESOURCE.

       CAP_SYS_TIME
              Set  system  clock  (settimeofday(2), stime(2), adjtimex(2)); set real-time
              (hardware) clock.

同步Docker容器的时间和宿主机的时间,可以采取以下步骤: 1. 首先,查看宿主机的时间和容器的时间是否一致。可以使用命令`docker exec -ti <container_id> date`来查看容器的时间,使用命令`date`来查看宿主机的时间。如果它们不一致,需要进行时间同步。 2. 一种方法是在运行容器时指定时区,以使容器的时间与宿主机的时间保持一致。例如,可以使用`-v /etc/localtime:/etc/localtime:ro`选项来将宿主机的时区文件(通常是/etc/localtime)挂载到容器中。这样容器就可以共享宿主机的时区设置。 3. 另一种方法是将宿主机的时区文件复制到容器中。可以使用命令`docker cp /etc/localtime <container_id>:/etc/localtime`将宿主机的时区文件复制到容器中。然后,再次执行`docker exec -ti <container_id> date`命令来确认容器的时间是否同步。 需要注意的是,以上方法适用于Linux环境下的Docker容器。对于其他操作系统或虚拟化平台,请参考相应的文档或指南来进行时间同步操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【Docker】容器与系统时间同步](https://blog.csdn.net/qq_22227087/article/details/96159034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Docker容器与宿主机设置时间同步](https://blog.csdn.net/IT_ZRS/article/details/112969590)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值