Skip to content

Shell

5 posts with the tag “Shell”

Let's Encrypt with Certbot

This post is going to introducing how to encrypt your website using HTTPS, with Certbot.

All the steps based on CentOS/RHEL(Red Hat Enterprise Linux).

  • OS: CentOS/RHEL
  • Packages: certbot (Optional: certbot-dns-cloudflare)
  • HostName: example.com

Install certbot on your system with dnf package manager:

sudo dnf install python3-certbot

If you wish to use dns-01 challenge, we take cloudflare as an example, you should install certbot-dns-cloudflare plugin:

sudo dnf install python3-certbot-dns-cloudflare

Make sure you have set your hostname to your host ip.

Allow port 80 on your host:

sudo firewall-cmd --add-port=80/tcp

(3). Issuing SSL Certificates with Certbot

Section titled “(3). Issuing SSL Certificates with Certbot”

Using the following command to issue a SSL certificate from Let’s Encrypt

sudo certbot certonly -d example.com

The private key and public key will be stored in folder /etc/letsencrypt/live/example.com/.

(4). Config Your Site with SSL Certificates

Section titled “(4). Config Your Site with SSL Certificates”

This is not the keypoint of this post, configuration should be varies depending on the web server used. Please refer to other resources online.

Optional-1. Config Nginx with Certbot automatically

Section titled “Optional-1. Config Nginx with Certbot automatically”

If you are using Nginx, it would be convenient by using --nginx parameter, replace the command in step(3):

sudo certbot certonly -d example.com --nginx

Then all configurations will be done in nginx file located in /etc/nginx/conf.d/example.com.conf

The dns_cloudflare plugin automates the process of completing a dns-01 challenge (DNS01) by creating, and subsequently removing, TXT records using the Cloudflare API.

The following command will do this operation:

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

In this section, cf_key.txt contains the API TOKEN or Global API Key from Cloudflare, here is the example of this two structure:

# 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

[1].https://certbot-dns-cloudflare.readthedocs.io/en/stable/

[2].https://developers.cloudflare.com/fundamentals/api/get-started/create-token/

[3].https://developers.cloudflare.com/fundamentals/api/get-started/keys/

[4]. Reverse-Proxy-with-Nginx-on-RHEL

Mount Disk in Linux on Boot

BrefAutomatically mount the hard drive at boot

Section titled “BrefAutomatically mount the hard drive at boot”

Before mounting the disk, you need to know the path orUUIDof each disk. At this time, you mainly rely on the two instructionsfdiskandblkid. The former instruction obtains the partition information, and the latter instruction obtains the attributes of theBlock deviceblock 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”

From the above output results, the disk can be mapped to the specific block device path`/dev/sdax`. Because the computer contains two disks, it contains`sda`and`sdb`. The block device path and`UUID`here will be used in the following configuration parameters.
## 2. Temporary MountFor a temporary solution, you can use the following command to mount`/dev/sda1`to the`/mnt/c`directory```
sudo mkdir /mnt/c
sudo mount -t ntfs /dev/sda1 /mnt/c

(1). Modify configuration fileThe solution to automatically mount after booting requires modifying the configuration file/etc/fstab. After opening it with an editor, add a new mount item in the following format

Section titled “(1). Modify configuration fileThe solution to automatically mount after booting requires modifying the configuration file/etc/fstab. After opening it with an editor, add a new mount item in the following format”
$ 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

The last two items are used to mount the disk. Of course, the/dev/sdbxin front of each line can also be replaced byUUID=xxxxx. However, since theUUIDof the two disks are consistent, there may be problems. Maybe you can use thePARTUUIDoutput byblkid.

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

Mount SMB/CIFSUse systemd to automatically mount smb at boot

Section titled “Mount SMB/CIFSUse systemd to automatically mount smb at boot”
# 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

Modify file permissions in batches on Linux

During use, you often encounter the following situations: It is necessary to modify the folder permissions in a certain directory to: 0755, the owner is root, the file permissions are: 0644, the owner is root

1. Set the permissions of all folders in a directory individually```

Section titled “1. Set the permissions of all folders in a directory individually```”

find -type d -exec chmod 0755 {} ;

## 2. Set the permissions of all files in a directory individually```
find -not -type d -exec chmod 644 {} \;

3. If you want to set the owner of the folder individually, you only need to change the command to the following:

Section titled “3. If you want to set the owner of the folder individually, you only need to change the command to the following:”
find -type d -exec chown root.root {} \;
u :目录或者文件的当前的用户
g :目录或者文件的当前的群组
o :除了目录或者文件的当前用户或群组之外的用户或者群组
a :所有的用户及群组
```
  • Permission code: ``` r :读权限,用数字4表示 w :写权限,用数字2表示 x :执行权限,用数字1表示 – :删除权限,用数字0表示 s :特殊权限

Check Whether the Proxy Agent Is Running Normally

Use curl to send a request through the local proxy and print only the HTTP status code:

curl -so /dev/null -w "%{http_code}" google.com -x socks5://127.0.0.1:10080
  • socks5 is the proxy protocol. It can be replaced with another protocol such as http.
  • 10080 is the proxy port. Replace it with the input port of your proxy service.

If the command returns 301 or 200, the proxy service is working. If it returns 000, the proxy service is not responding and cannot be used normally.

[1]. Linux-Auto-PAC