Linux Debian/Ubuntu 基础使用

    153

整合一下

关于apt与apt-get:

  • dpkg是幕后英雄,负责实际的安装工作。

  • apt-get提供了完整的dpkg接口,功能强大但略显繁琐。

  • apt是更友好、更易用的apt-get版本,功能略有精简。

apt和apt-get不仅仅是dpkg的接口,还能完dpkg无法做到的事情,比如从软件库获取文件。选择使用哪一个,取决于你的具体需求和使用场景,简单来说,apt更适合日常使用,而apt-get更受脚本开发者青睐。

apt-get update 
apt-get upgrade 

Vim

apt-get install vim -y

UFW 防火墙

安装

# ubuntu 自带ufw
sudo apt install ufw -y

开放端口

在启动前开启22端口 允许SSH连接

ufw allow OpenSSH
ufw allow 22

关闭端口

ufw deny 80

启动

ufw enable
# Command may disrupt existing ssh connections. Proceed with operation (y|n)?
# Firewall is active and enabled on system startup

关闭

ufw disable

查看状态

ufw status
ufw status verbose
# 开启https服务需要端口
ufw allow https
# 可以使用端口号443代替https配置文件
ufw allow 443/tcp

允许特定的IP地址

ufw allow from 115.127.62.61
# 允许特定IP访问前提条件下,允许其访问特定端口
ufw allow from 115.127.62.61 to any port 22

拒绝连接

ufw deny from 23.24.25.0/24
# 拒绝从23.24.25.0/24访问端口80和443
ufw deny from 23.24.25.0/24 to any port 80
ufw deny from 23.24.25.0/24 to any port 443

删除UFW规则

ufw status numbered
​
# 输出类似如下:
Status: active
​
     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 80/tcp                     ALLOW IN    Anywhere
[ 3] 8080/tcp                   ALLOW IN    Anywhere
​
# 删除规则号3
sudo ufw delete 3

重启防火墙

ufw reload

重置UFW

ufw reset

禁止 ICMP 【禁 ping】

谁TM在Ping我的鸡

打开 UFW 配置文件

vim /etc/ufw/before.rules

修改配置

  • 允许 ping

# ok icmp codes for INPUT
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
​
# ok icmp code for FORWARD
-A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT
  • 禁止 ping

# ok icmp codes for INPUT
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j DROP
-A ufw-before-input -p icmp --icmp-type time-exceeded -j DROP
-A ufw-before-input -p icmp --icmp-type parameter-problem -j DROP
-A ufw-before-input -p icmp --icmp-type echo-request -j DROP
​
# ok icmp code for FORWARD
-A ufw-before-forward -p icmp --icmp-type destination-unreachable -j DROP
-A ufw-before-forward -p icmp --icmp-type time-exceeded -j DROP
-A ufw-before-forward -p icmp --icmp-type parameter-problem -j DROP
-A ufw-before-forward -p icmp --icmp-type echo-request -j DROP

让配置生效

ufw reload

BBR 加速

理论上 Linux Kernel 内核升级到 4.9 及以上版本可以实现 BBR 加速。

# 内核版本高于 4.9 就行
uname -r

修改系统变量

echo net.core.default_qdisc=fq >> /etc/sysctl.conf
echo net.ipv4.tcp_congestion_control=bbr >> /etc/sysctl.conf

保存生效

sysctl -p

验证

  • 如果结果中带有bbr,则证明你的内核已开启bbr

sysctl net.ipv4.tcp_available_congestion_control
# 结果是这样
net.ipv4.tcp_available_congestion_control = bbr cubic reno

注:也可以执行下面命令,如果结果中有bbr,也可以证明你的内核已开启bbr

lsmod | grep bbr

Swap 分配

swap 分区通常被称为交换分区,这是一块特殊的硬盘空间,即当实际内存不够用的时候,操作系统会从内存中取出一部分暂时不用的数据,放在交换分区中,从而为当前运行的程序腾出足够的内存空间。

添加 Swap 分区

检查你的系统是否已经有 Swap 分区:

swapon -s
# 或者
free -m

如果没有返回结果或者 free -mSwap 一列数值是 0,则表示你的系统没有 Swap 分区。

创建 SWAP 分区

  1. 使用 fallocate 创建一个 2GB 大小的 Swap 分区

fallocate -l 2G /swapfile
  1. 如果这个命令无法使用,请安装 util-linux

apt install util-linux
  1. 设置这个文件的权限

chmod 600 /swapfile
  1. 激活 SWAP 分区

mkswap /swapfile
swapon /swapfile

此时,你可以使用 swapon -sfree -m 命令查看 Swap 分区是否已经激活。

设置开机自启

编辑 /etc/fstab 这个文件,加入下面的内容即可:

echo "/swapfile swap swap defaults 0 0" >> /etc/fstab

使用 free -m 命令查看 Swap 分区是否正确

调整系统内核 Swappiness 值

Swapiness 是 Linux 内核的一个属性,定义了系统使用交换空间的频率,Swapiness 的值在 0 到 100 之间 (默认是 60),一个低的值会使内核尽可能地避免交换,而一个高的值会使内核更积极地使用交换空间。

这个值默认是 60,我们可以使用 cat /proc/sys/vm/swappiness 命令查看当前值

改成 100

echo "vm.swappiness=100" >> /etc/sysctl.conf
sysctl -p

使用 sysctl -p 命令使其生效

关闭 Swap

停用 Swap 分区

swapoff -v /swapfile

然后检查 /etc/fstab,删除 /swapfile swap swap defaults 0 0 这一行。

最后删除 /swapfile 这个文件:

rm /swapfile

Docker 安装

Docker 是一个软件平台,让您可以快速构建、测试和部署应用程序。1.40+版本自带的docker compose

卸载旧版本

apt-get remove docker docker-engine docker.io containerd runc

安装 apt 依赖包

apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加 Docker 的官方 GPG 密钥

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - 似乎准备弃用了

Debian
curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
Ubuntu
curl -sSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg

设置稳定版仓库

Debian
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/debian $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
Ubuntu
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list

更新 apt 包索引

apt-get update

安装最新版本

apt-get install docker-ce docker-ce-cli containerd.io

安装特定版本

apt-cache madison docker-ce

使用第二列中的版本字符串安装特定版本

apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
  • 此条为我自己复制用

apt-get install docker-ce=5:23.0.6-1~debian.12~bookworm docker-ce-cli=5:23.0.6-1~debian.12~bookworm containerd.io
systemctl daemon-reload
systemctl restart docker

Docker卸载

删除安装包:

apt-get purge docker-ce

删除镜像、容器、配置文件等内容:

rm -rf /var/lib/docker

附 Docker

https://uain.cc/archives/eDk7stDlhttps://uain.cc/archives/05z7Pw1g

Nginx 配置

Nginx是一个轻量级/高性能的反向代理Web服务器,它是由C语言写的,所以速度非常快、性能非常优秀。

安装

apt install nginx -y
  • 安装成功后的目录:/etc/nginx

  • 配置文件:/etc/nginx/nginx.conf

  • 日志文件:/var/log/nignx

Nginxs配置模板

以halo为例

user www-data;
# worker_processes auto;
worker_processes  2;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}

http {
	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;

	##
	# Gzip Settings
	##

	gzip on;
    gzip_min_length  15k;
	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 9;
	gzip_buffers 16 8k;
	gzip_http_version 1.1;
	gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

    upstream halo {
        server 127.0.0.1:8090;
    }
    server {
        listen 80;
        # listen 443 ssl;
        server_name uain.cc;
        if ($host != 'uain.cc'){
            return 403;
        }
        # ssl_certificate  xxxxxxx.pem绝对路径;
        # ssl_certificate_key xxxxxxxx.key绝对路径;
        location / {
            proxy_pass http://halo;
            proxy_set_header HOST $host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 1024m;
        }
    }
}

Nginx相关指令

  • nginx | 启动

  • nginx -s stop | 关闭

  • nginx -s quit | 等待用户请求的处理后关闭主进程

  • nginx -t | 查看配置文件是否正确

  • nginx -s reopen | 重新打开日志文件

  • nginx -s reload | 重新启动

  • nginx -c config_name | 使用指定的配置文件

Scp 文件传输

scp介绍 scp是secure copy的简写,是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录。

命令格式

scp [参数] [原路径] [目标路径]

scp 本地用户名 @IP 地址 : 文件名 1 远程用户名 @IP 地址 : 文件名 2

本地服务器->远程服务器

命令格式:

scp local_file remote_username@remote_ip:remote_folder 

或者

 scp local_file remote_username@remote_ip:remote_file 

或者

scp local_file remote_ip:remote_folder

或者

 scp local_file remote_ip:remote_file

将本地服务器的目录传送到远程服务器

命令格式

scp -r local_folder remote_username@remote_ip:remote_folder 

或者

scp -r local_folder remote_ip:remote_folder

远程服务器->本地服务器

与从本地传送到远程服务器相类似,只是将参数位置互换一下。

远程复制文件到本地目录

/opt/soft/的目录中下载nginx-0.5.38.tar.gz 文件到本地/opt/soft/目录

scp root@ip:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/

远程复制目录到本地目录

/opt/soft/中下载mongodb 目录到本地的/opt/soft/目录

scp -r root@ip:/opt/soft/mongodb /opt/soft/

Systemctl 使用

适用于Linux Ubuntu/Debian

安装 systemd

# 使用 yum 安装 systemd(CentOS/RHEL)
yum install systemd

# 使用 apt 安装 systemd(Debian/Ubuntu)
apt install systemd

FRPS为例

创建 frps.service 文件
vim /etc/systemd/system/frps.service

写入内容

[Unit]
Description = frp server
After = network.target syslog.target
Wants = network.target
Requires=network-online.target

[Service]
Type = simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/opt/frp/frps -c /opt/frp/frps.toml
ExecReload=/opt/frp/frps reload -c /opt/frp/frps.toml

[Install]
WantedBy = multi-user.target

使用 systemd 命令管理 frps 服务

# 启动frp
systemctl start frps
# 停止frp
systemctl stop frps
# 重启frp
systemctl restart frps
# 查看frp状态
systemctl status frps

设置 frps 开机自启动

systemctl enable frps

Samba

Samba 可在本地机上使用,在普通云服务器上无法使用

用root账户安装,以免权限问题 默认端口445

安装 samba

apt-get install samba

注意:若提示有依赖问题,就把相应的软件卸载掉先,apt-get autoremove

新建smb账户

smbpasswd -a username

提示错误:Failed to add entry for user

这是因为没有加相应的系统账号,所以会提示Failed to add entry for user的错误,只需增加相应的系统账号,并给home目录权限

groupadd <user> -g 6000
useradd <user> -u 6000 -g 6000 -s /sbin/nologin -d /dev/null
chown -R <user>:<user> /home

列出现有的Samba用户列表

pdbedit -w -L

配置smb.conf文件

备份原有文件

cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

修改smb.conf文件

vim /etc/samba/smb.conf

#======================= Share Definitions =======================

以下内容建议全部删除,并新增以下

[home]
    comment = Shared Folder Home
    path = /home
    browseable = yes
    read only = no
    create mask = 0777
    directory mask = 0777
    valid users = %S

启动服务

service smbd restart  #重启 
systemctl enable nmbd.service  #设置开机服务

附:配置说明

[共享名称]:共享中看到的共享目录名
    comment = 共享的描述. 
    path = 共享目录路径(可以用%u、%m这样的宏来代替路径如:/home/share/%u) 
    browseable = yes/no指定该共享是否在“网上邻居”中可见。
    writable = yes/no指定该共享路径是否可写。
    read only = yes/no设置共享目录为只读(注意设置不要与writable有冲突) 
    available = yes/no指定该共享资源是否可用。
    admin users = bobyuan,jane指定该共享的管理员,用户验证方式为“security=share”时,此项无效。 
    valid users = bobyuan,jane允许访问该共享的用户或组-“@+组名” 
    invalid users = 禁止访问该共享的用户与组(同上) 
    write list = 允许写入该共享的用户
    public = yes/no共享是否允许guest账户访问。 
    guest ok = yes/no意义同“public”。
    create mask = 0700指定用户通过Samba在该共享目录中创建文件的默认权限。0600代表创建文件的权限为rw-------
    directory mask = 0700指定用户通过Samba在该共享目录中创建目录的默认权限。0600代表创建目录的权限为rwx---

附:Windows 免密登录 Linux

https://uain.cc/archives/CH8r8Wv7

消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息