29篇 linux related articles

CRLF to LF

You can use vim programmatically with the option -c {command} :

Dos to Unix:

vim file.txt -c "set ff=unix" -c ":wq"

Unix to dos:

vim file.txt -c "set ff=dos" -c ":wq"

"set ff=unix/dos" means change fileformat (ff) of the file to Unix/DOS end of line format

":wq" means write file to disk and quit the editor (allowing to use the command in a loop)

More ~

linux之 /etc/fstab 详解

/etc/fstab 是 Linux 和其他类 Unix 操作系统的配置文件,用于描述文件系统。它包含有关文件系统的位置、挂载点、文件系统类型和挂载选项的信息。

/etc/fstab 的每一行描述一个文件系统,格式如下:

<file system> <mount point> <type> <options> <dump> <pass>
  • <file system> 是文件系统的位置,可以是设备文件(如 /dev/sda1)、分区 UUID(如 UUID=6c38203b-0ddc-4e35-a4a7-60733e8c1532)或文件系统标签(如 LABEL=MyDataPartition)。
  • <mount point> 是文件系统在文件系统层次结构中的挂载点。
  • <type> 是文件系统的类型,如 ext4xfsswap
  • <options> 是挂载该文件系统时使用的选项,如 rw(可读写)、ro(只读)或 noatime(不更新访问时间)。
  • <dump> 指定文件系统是否应由 dump 程序备份。0 表示不备份,1 表示备份。
  • <pass> 指定文件系统在 fsck 程序检查文件系统时应经过的检查次数。0 表示不检查,1 表示检查一次,以此类推。

以下是 /etc/fstab 中的一些常见示例:

/dev/sda1 / ext4 rw,relatime 0 1
/dev/sda2 /home ext4 rw,relatime 0 2
/dev/sda3 /data ext4 rw,relatime 0 3
/dev/sdb1 /mnt/backup ext4 rw,relatime 0 0
/dev/sdc1 swap swap defaults 0 0

在上面的示例中:

  • /dev/sda1 是根文件系统,挂载在 /
  • /dev/sda2/home 目录的文件系统,挂载在 /home
  • /dev/sda3/data 目录的文件系统,挂载在 /data
  • /dev/sdb1 是备份文件系统,挂载在 /mnt/backup
  • /dev/sdc1 是交换分区,用于虚拟内存。

您可以使用 mount 命令挂载文件系统,也可以使用 umount 命令卸载文件系统。

有关 /etc/fstab 的更多信息,请参阅 man fstab 手册页。

More ~

帮你提升效率的5个Linux命令技巧

话不多话,直接上菜:

1. 切回上一个工作目录

这条目录可以直接切回上一次cd的目录,避免复制和输入长目录了。

cd -

如下:切到了Misc, 想切加上一个Downloads目录,即用 cd -

~/Downloads ❯❯❯ cd Misc
~/D/Misc ❯❯❯ cd -
~/Downloads
More ~

Ubuntu中使用Netplan命令来管理网络

netplan从/etc/netplan/ 目录读取配置文件,这个目录下有多个不同的文件,可能会有这样的文件

  • 01-netcfg.yaml
  • 50-cloud-init.yaml
  • config.yaml

但不一定都有,我们可以修改来更改网络配置,如果在这个目录下你没找到文件可以通过这个命令sudo netplan generate来创建一个配置文件。

More ~

crontab定时脚本执行时间与系统时间不一致

事情源于在树莓派上写的定时脚本,定时获取停车费用信息。ubuntu重装后时区都用tzselect设置了,但仍在半夜收到消息通知。
于是发现需要这样设置下。

执行这个设置成中国时区 timedatectl set-timezone Asia/Shanghai

查看本地时间 timedatectl status

还有最重要的,要重启下crontab的服务. systemctl restart cron
有些系统中crontab服务为 crond。

更多关于timedatectl的帮助可以查看这个:

More ~

CentOS7 firewall防火墙允许nginx http https被访问

##添加允许通过服务
查看所有服务 firewall-cmd --get-services

firewall-cmd --add-service=http

firewall-cmd --add-service=https

##参考:

打开443/TCP端口

firewall-cmd --add-port=443/tcp

永久打开3690/TCP端口

firewall-cmd --permanent --add-port=3690/tcp

更新后重新加载

firewall-cmd --reload

查看防火墙,添加的端口也可以看到

firewall-cmd --list-all

More ~