一些学习中的随笔小记(四)
文章目录
错误:the input device is not a TTY

【报错】:
the input device is not a TTY
【解决方法】:
docker exec -i (去掉-t)
因为-t是指分配一个伪终端。这里不需要分配伪终端。
Docker容器时间如何与宿主机同步?
如果在启动Docker容器的过程中没有单独配置localtime,很可能造成Docker容器时间与主机时间不一致的情况,比如UTC和CST相差8小时。
# 查看主机时间 [root@localhost ~]# date 2020年07月27日 星期三 22:42:44 CST # 查看容器时间 # docker exec -it <containerid> /bin/sh root@b43340ecf5ef:/# date Wed Jul 27 14:43:31 UTC 2020
可以发现,他们相隔了8小时。CST应该是指(China Shanghai Time,东八区时间) UTC应该是指(Coordinated Universal Time,标准时间) 所以,这2个时间实际上应该相差8个小时
解决方案
docker run 添加时间参数
-v /etc/localtime:/etc/localtime # 实例1 docker run -p 3306:3306 --name mysql -v /etc/localtime:/etc/localtime # 实例2 docker run \ --detach \ --restart always \ --name 'scribe' \ --publish 11315:11315 \ --mount type=bind,source=/data/gop/,destination=/data/gop/,consistency=consistent \ -v /etc/localtime:/etc/localtime \ wsgzao/facebook-scribe
Dockerfile
# 方法1 # 添加时区环境变量,亚洲,上海 ENV TimeZone=Asia/Shanghai # 使用软连接,并且将时区配置覆盖/etc/timezone RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone # 方法2 # CentOS RUN echo "Asia/shanghai" > /etc/timezone # Ubuntu RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
docker-compose
#第一种方式(推荐): environment: TZ: Asia/Shanghai #第二种方式: environment: SET_CONTAINER_TIMEZONE=true CONTAINER_TIMEZONE=Asia/Shanghai #第三种方式: volumes: - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime
宿主机直接执行命令给某个容器同步时间
# 方法1:直接在宿主机操作 docker cp /etc/localtime 【容器ID或者NAME】:/etc/localtime docker cp -L /usr/share/zoneinfo/Asia/Shanghai 【容器ID或者NAME】:/etc/localtime # 方法2:登录容器同步时区timezone ln -sf /usr/share/zoneinfo/Asia/Singapore /etc/localtime
Linux 利用 iptables 屏蔽域名请求
一般 iptables 自带的都有 string 模块,这个模块的作用就是匹配字符串,匹配到泛域名的 URL,然后就把数据包丢弃,就实现了屏蔽泛域名的功能。比如可以限制科学上网访问 youtube.com。
示例:
以下规则是屏蔽以 youtube.com 为主的所有一级 二级 三级等域名。 iptables -A OUTPUT -m string --string "youtube.com" --algo bm --to 65535 -j DROP # 添加屏蔽规则 iptables -D OUTPUT -m string --string "youtube.com" --algo bm --to 65535 -j DROP # 删除屏蔽规则,上面添加的代码是什么样,那么删除的代码就是把 -A 改成 -D
参数解释:
-A # 添加iptables规则; -D # 删除iptables规则(把添加防火墙规则时代码中的 -A 改成 -D 即可删除添加的规则); -m string # 指定模块; --string "youtube.com" # 指定要匹配的字符串(域名、关键词等); --algo bm # 指定匹配字符串模式/算法(还有一种更复杂的算法:kmp); --to 65535 # 指定端口,这里代表所有端口(1-65535); -j DROP # 指匹配到数据包后处理方式,这里是丢弃数据包。
这个模块的作用就是匹配字符串,这个字符串可以是URL、普通文本、文件后缀(后两者时,如果目标网站启用了GZIP类压缩算法,就会无法过滤匹配,毕竟都压缩了)比如: .zip ,就会把包含 .zip 的数据库丢弃,这样就会无法下载 .zip 类型的文件了。
但是这种方法并不靠谱,这种方式是匹配数据包的所有内容,存在关键字都会drop掉。

iptables -D OUTPUT -p tcp -d www.baidu.com –dport 443 -j DROP
这种方式添加的也不靠谱,iptables实际上还是将域名解析为ip再添加规则。

iptables 禁止访问指定 IP段
如果想要禁止访问某个IP,例如:1.1.1.0
这个IP段,使用以下命令:iptables -I INPUT -s 1.1.1.0/24 -j DROP
如果想要禁止访问某个IP,例如:1.1.0.0
这个IP段,使用以下命令:iptables -I INPUT -s 1.1.0.0/16 -j DROP
如果想要禁止访问某个IP,例如:1.0.0.0
这个IP段,使用以下命令:iptables -I INPUT -s 1.0.0.0/8 -j DROP
iptables 只能访问指定IP
1.首先,要添加一个允许访问的IP,如192.168.1.1
,使用以下命令:iptables -I INPUT -s 192.168.1.1 -j ACCEPT
2.然后,设置禁止访问所有IP,使用以下命令:iptables -I INPUT -s 0.0.0.0/8 -j DROP
iptables 也可用于发起dnslog?


微信赞赏
支付宝赞赏
发表评论