跳转到内容

Shell

5 posts with the tag “Shell”

让我们用 Certbot 加密

一、简介这篇文章将介绍如何使用 HTTPS 和 [Certbot][certbot] 来加密您的网站。

Section titled “一、简介这篇文章将介绍如何使用 HTTPS 和 [Certbot][certbot] 来加密您的网站。”

所有步骤基于CentOS/RHEL(Red Hat Enterprise Linux)。

2. 环境要求- 操作系统:CentOS/RHEL- 软件包:certbot(可选:certbot-dns-cloudflare)- 主机名:example.com

Section titled “2. 环境要求- 操作系统:CentOS/RHEL- 软件包:certbot(可选:certbot-dns-cloudflare)- 主机名:example.com”

3.安装Certbot使用 dnf 包管理器在您的系统上安装 certbot:

Section titled “3.安装Certbot使用 dnf 包管理器在您的系统上安装 certbot:”
sudo dnf install python3-certbot

如果您想使用 dns-01 挑战,我们以cloudflare为例,您应该安装certbot-dns-cloudflare插件:

sudo dnf install python3-certbot-dns-cloudflare

确保您已将主机名设置为主机 IP。

(2)。配置防火墙允许主机上的端口80:

Section titled “(2)。配置防火墙允许主机上的端口80:”
sudo firewall-cmd --add-port=80/tcp

(3)。使用 Certbot 颁发 SSL 证书使用以下命令从[Let’s Encrypt][letEnc]颁发SSL证书

Section titled “(3)。使用 Certbot 颁发 SSL 证书使用以下命令从[Let’s Encrypt][letEnc]颁发SSL证书”
sudo certbot certonly -d example.com

私钥和公钥将存储在文件夹/etc/letsencrypt/live/example.com/中。

(4)。使用 SSL 证书配置您的站点这不是本文的重点,配置应根据所使用的 Web 服务器而有所不同。请参阅在线其他资源。

Section titled “(4)。使用 SSL 证书配置您的站点这不是本文的重点,配置应根据所使用的 Web 服务器而有所不同。请参阅在线其他资源。”

可选-1。自动使用 Certbot 配置 Nginx如果您使用[Nginx][nginx],使用--nginx参数会很方便,替换*step(3)*中的命令:

Section titled “可选-1。自动使用 Certbot 配置 Nginx如果您使用[Nginx][nginx],使用--nginx参数会很方便,替换*step(3)*中的命令:”
sudo certbot certonly -d example.com --nginx

然后所有配置将在位于/etc/nginx/conf.d/example.com.conf的 nginx 文件中完成

可选-2。dns-01的问题dns_cloudflare插件通过使用 Cloudflare API 创建并随后删除 TXT 记录,自动完成dns-01挑战 (DNS01) 的过程。

Section titled “可选-2。dns-01的问题dns_cloudflare插件通过使用 Cloudflare API 创建并随后删除 TXT 记录,自动完成dns-01挑战 (DNS01) 的过程。”

以下命令将执行此操作:

sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/PATH_TO_CF_KEY/cf_key.txt \

在本节中,cf_key.txt包含来自 Cloudflare 的 [API TOKEN][API] 或 [Global API Key][APIKEY],以下是这两个结构的示例:

# Cloudflare API token used by Certbot
dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567
# Cloudflare API credentials used by Certbot
dns_cloudflare_email = cloudflare@example.com
dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234

[2].https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ [3].https://developers.cloudflare.com/fundamentals/api/get-started/keys/ [certbot]:https://certbot.eff.org [letEnc]:https://letsencrypt.org/ [nginx]:https://www.nginx.com [API]:https://developers.cloudflare.com/fundamentals/api/get-started/create-token/ [APIKEY]:https://developers.cloudflare.com/fundamentals/api/get-started/keys/ [4]. 使用 RHEL 上的 Nginx 进行反向代理

Mount Disk in Linux on Boot

开机自动挂载硬盘

在挂载磁盘前,需要知道每个磁盘的路径或UUID,此时主要依靠fdisk和blkid两个指令,前一个指令获取分区信息,后一个指令获取Block device块设备的属性。

$ sudo blkid
/dev/sda1: LABEL="Win10" UUID="C4A0E65EA0E65708" TYPE="ntfs" PARTUUID="6190c592-01"
/dev/sda2: UUID="AE3C137D3C133FAF" TYPE="ntfs" PARTUUID="6190c592-02"
/dev/sdb1: LABEL="Apps" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="45ed07d2-c9e3-4167-8868-3e33f62784e1"
/dev/sdb2: LABEL="Data" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="2a8b4bf1-8357-4d65-bed4-f73f01b96431"
/dev/sdb3: LABEL="Backup" UUID="0000678400004823" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="a7187083-2820-4eef-83ff-05d8899879a2"

从上面的输出结果就可以将磁盘对应到具体的块设备路径/dev/sdax,因为电脑含两块磁盘,所以包含sda及sdb。这里的块设备路径和UUID会将下面的配置参数中用到。

对于临时解决方案,可以使用以下指令将/dev/sda1挂载到/mnt/c目录

sudo mkdir /mnt/c
sudo mount -t ntfs /dev/sda1 /mnt/c

开机启动后自动挂载的方案就需要修改配置文件/etc/fstab,使用编辑器打开后,按以下格式添加新的挂载项

$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sdb5 during installation
UUID=4d2e5a83-d9fe-47bb-aa02-fee9ac8535e3 / ext4 errors=remount-ro 0 1
# /boot was on /dev/sdb6 during installation
UUID=9b521dc2-ad85-4ca6-bb8b-f285b312aa49 /boot ext4 defaults 0 2
# /home was on /dev/sdb7 during installation
UUID=f6b796dd-9e59-4f79-b70a-c2c8e5977163 /home ext4 defaults 0 2
# swap was on /dev/sdb4 during installation
UUID=9430ea0e-7ff9-4167-9bf7-1f3e08d26d34 none swap sw 0 0
# /mnt/Data was on /dev/sdb2 during installation
/dev/sdb2 /mnt/Data ntfs defaults 0 2
# /mnt/Backup was on /dev/sdb3 during installation
/dev/sdb3 /mnt/Backup ntfs defaults 0 2

后面两项就是用来挂载磁盘的,当然每行前面的/dev/sdbx也可以换成UUID=xxxxx,但是由于这两个磁盘的UUID一致,所以可能会有问题,也许可以使用blkid输出的PARTUUID。

sudo mkdir /mnt/{Data,Backup}
sudo mount -a

利用systemd 开机自动挂载smb

# This is a sample service script to mount CIFS/SAMBA shares.
# Please read carefully the comments in this file. For production usage
# you can remove all comments (lines beginning with "#") from this file.
[Unit]
# The description should be used to explain what this servicefile is for
Description=nas cifs mount home
# if we do network mounts like here we *require* 'network-online.service'
# which checks if the network is online
Requires=network-online.target
# our scripts must start *after* 'network-online.service', on timeout and if
# 'network-online.service' fails we can not mount and this scripts fails too
After=network-online.target
# usually we mount networks shares because we want they avaible *before* XBMC starts.
# so XBMC has access to this mounts from beginning. Note: this slows down the boot!
#Before=kodi.service
[Mount]
# The share we want mount
What=//IP_ADDRESS/Data
# Where we want mount this share
Where=/PATH_TO_MOUNT
# Any options you usually use with the "-o" parameter in the mount command
Options=username=haoran.qi,password=password,rw,vers=3.0,dir_mode=0775,file_mode=0664
# filesystem type
Type=cifs
[Install]
WantedBy=multi-user.target

[1]. https://www.litreily.top/2019/02/17/mount/

Linux批量修改文件权限

在使用过程中,经常会遇到如下情况:

需要修改某个目录下的文件夹权限为:0755,所有者为root,文件权限为:0644,所有者为root

1. 单独设置某个目录下所有文件夹的权限

Section titled “1. 单独设置某个目录下所有文件夹的权限”
find -type d -exec chmod 0755 {} \;

2. 单独设置某个目录下所有文件的权限

Section titled “2. 单独设置某个目录下所有文件的权限”
find -not -type d -exec chmod 644 {} \;

3. 如果要单独设置文件夹的所有者,只需要将命令改成如下即可:

Section titled “3. 如果要单独设置文件夹的所有者,只需要将命令改成如下即可:”
find -type d -exec chown root.root {} \;
  • 权限范围:
    u :目录或者文件的当前的用户
    g :目录或者文件的当前的群组
    o :除了目录或者文件的当前用户或群组之外的用户或者群组
    a :所有的用户及群组
  • 权限代号:
    r :读权限,用数字4表示
    w :写权限,用数字2表示
    x :执行权限,用数字1表示
    – :删除权限,用数字0表示
    s :特殊权限

[1].https://justcode.ikeepstudying.com/2018/01/linux-%E6%89%B9%E9%87%8F%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6%E5%A4%B9%E3%80%81%E6%96%87%E4%BB%B6%E7%9A%84%E6%9D%83%E9%99%90%E5%92%8C%E6%89%80%E6%9C%89%E8%80%85chmod-chown/

命令行安装 KVM 虚拟化系统

virt-install \
--name=openwrt \
--vcpus=1 \
--ram=512 \
--os-type=linux \
--disk path=/kvm/disk/openwrt.img,bus=ide \
--autostart \
--network bridge=br0,model=e1000 \
--network bridge=br0,model=e1000 \
--import \
--noautoconsole \
--graphics vnc,listen=0.0.0.0,password=000000

检测代理是否运行正常

使用 curl 通过本地代理发起请求,并只输出 HTTP 状态码:

curl -so /dev/null -w "%{http_code}" google.com -x socks5://127.0.0.1:10080
  • socks5 表示使用的代理协议,可以换成 http 等其他协议。
  • 10080 是代理端口号,应替换为代理服务的 Input 端口。

命令返回值为 301 或 200,说明代理服务工作正常;返回值为 000,说明代理长时间无响应,无法正常工作。

[1]. Linux-Auto-PAC