分类 Linux 下的文章

Linux 系统下常用的环境是终端,没有图形化界面使查看文件/文件夹的一些属性没有那么直观,需要配合命令来实现。

使用命令如下:
ls:查看文件大小
du:查看文件夹大小

ls

查看文件大小。

ls -l filename   #显示当前目录下此文件的大小
ls -lh filename  #显示当前目录下此文件的大小,数值显示为适合人们观察的格式, human readable format (kb,mb,gb...)
ls -l *          #显示当前目录下所有文件的大小信息
ls -al *         #显示当前目录下所有文件的大小信息,包括隐藏文件
ls -al /dir/      #显示特定目录下所有文件的大小信息,包括隐藏文件

使用 ls 命令,文件夹的大小会显示此文件夹本身的大小,而不是问价夹内内容的总大小。

du

查看文件夹内容大小

du -sh directory_name    #显示总的 summarized(-s) 目录大小 in human readable(-h) format
du -bsh *                #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format

服务器上搭建了个云盘,有个 temp 目录存放一些临时文件,想要设定指定天数后自动删除,可以使用 Linux 脚本实现。

用到的命令:
find mtime crontab

基本思路是使用 find 命令筛选符合条件的文件或文件夹,使用 crontab 创建定时任务。

find 的详细用法:https://niekun.net/index.php/archives/543.html
crontab 的详细用法:https://niekun.net/index.php/archives/461.html

阅读全文


有时候我们想要限制某个页面的访问权限,需要用户名密码才能进入。

如果使用 nginx 作为反向代理服务器,可以使用 auth_basic 来实现页面加密。

环境需求:

Linux env
nginx
apache2-utils (Debian, Ubuntu) or httpd-tools (RHEL/CentOS/Oracle Linux)

安装环境:

nginx 安装参考:https://niekun.net/index.php/archives/30.html

安装 apache2-utils:

apt-get install apache2-utils

创建账户密码:

使用了 htpasswd 命令创建用户。

关于 htpasswd 的详细使用:https://httpd.apache.org/docs/2.4/programs/htpasswd.html

建议将账户信息文件建立在 nginx 目录,我放在 /etc/nginx/users 目录下:

htpasswd -c /etc/nginx/users/.adminpasswd user1

.adminpasswd 为隐藏的存储所有账户信息的文件,user1 为用户名,回车后会提示输入密码,然后确认密码。

建立其他账户:

htpasswd /etc/nginx/users/.adminpasswd user2

不需要 -c 参数,因为文件已经建立了。

验证账户密码 -v:

htpasswd -v /etc/nginx/users/.adminpasswd user1

删除账户 -D:

htpasswd -D /etc/nginx/users/.adminpasswd user1

使用账户:

打开 nginx 配置文件,在需要账户登录的路径段修改如下:

location /path {
    ...
    auth_basic           "Administrator's Area";
    auth_basic_user_file /etc/nginx/users/.adminpasswd;
    ...
    ...
}

重新加载配置文件:

service nginx configtest
service nginx reload

打开对应页面测试是否提示输入账号密码。



docker 配置为非 root 用户,解决执行命令需要 sudo:

# 1. 创建 docker 用户组
$ sudo groupadd docker

# 2. 添加当前用户到 docker 用户组
$ sudo usermod -aG docker $USER

# 3. 退出登录在重新登录系统,直接执行 docker 命令查看效果

查看 docker 在系统中总共占用多少空间:

docker system df

清理多余重复 image 镜像:

docker image prune -a

清理整个 docker 系统文件,删除未被使用的 image container 等:

docker system prune -a

停止并删除目前正在运行的容器

docker compose down

拉取最新的镜像

docker compose pull

运行镜像

docker compose up -d

一键更新最新镜像

docker compose pull && docker compose up -d

http://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html
http://www.ruanyifeng.com/blog/2018/02/docker-wordpress-tutorial.html

https://docs.docker.com/reference/
https://docs.docker.com/engine/reference/commandline/image/
https://docs.docker.com/engine/reference/commandline/container/
https://docs.docker.com/engine/reference/commandline/run/
https://docs.docker.com/engine/reference/run/#restart-policies---restart

https://hub.docker.com/