====== Ubuntu 系统 ======
===== 重要的命令 =====
==== 初始化 ====
包括更改root用户密码、允许以 root 身份 ssh 登录。否则 headless 服务器无法进行后续操作。
sudo passwd root #修改root的密码
su root #转成root身份,否则ubuntu用户没有办法修改任何文件
# 修改 /etc/ssh/sshd_config 文件
PermitRootLogin yes #然后重启ssh服务
sudo service ssh restart
添加新用戶 ''%%sudo adduser xxxx%%''
==== 系统架构 ====
uname -a
arch
dpkg --print-architecture
==== 查看网卡 ====
方法一:''%%ip a%%'' \\
方法二:''%%iwconfig%%''
$ apt install wireless-tools
$ iwconfig
测试网速,先下载测试工具
sudo apt install speedtest-cli -y
speedtest
root@serverpi:~# sudo apt install speedtest-cli -y
root@serverpi:~# speedtest
Retrieving speedtest.net configuration...
Testing from Vodafone Germany Cable (62.143.173.13)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Händle & Korte GmbH (Dusseldorf) [0.46 km]: 31.382 ms
Testing download speed...............................................................................
.Download: 57.35 Mbit/s
Testing upload speed......................................................................................................
Upload: 10.05 Mbit/s
==== 查看端口 ====
查看1194端口当前被什么服务侦听:''%%netstat -ltnup | grep 1194%%''\\
查看openvpn当前侦听哪个端口:''%%netstat -ltnup | grep openvpn%%''
==== 查看内存 ====
列举占用内存最多的10个应用
ps aux --sort=-%mem | head -n 10
==== TimeShift 系统备份 ====
使用 TimeShift 备份到usb 闪存。(([[https://blog.csdn.net/wf19930209/article/details/104236358|Timeshift系统备份与还原]]))
(([[https://zhuanlan.zhihu.com/p/94105263|使用 TimeShift 备份和还原 Ubuntu Linux]]))
应该可以代替 mongodb 的快照备份。安装TimeShift:
sudo add-apt-repository -y ppa:teejee2008/ppa
sudo apt update
sudo apt install timeshift
查看8888端口是否被占用
lsof -i:8888
==== RSYNC 备份服务 ====
参考文章:[[https://www.jianshu.com/p/b0157e4ab801|RSYNC备份服务]] 以及 [[https://zhuanlan.zhihu.com/p/88338737|rsync的基本使用]]
从主机拉数据 (([[https://www.cnblogs.com/MikeZhang/p/rsyncExample_20160818.html|rsync同步目录]]))
rsync -avzP --delete root@{remoteHost}:{remoteDir} {localDir}
参数说明:\\
-a 参数,相当于-rlptgoD(-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件);\\
-z 传输时压缩;\\
-P 传输进度;\\
-v 传输时的进度等信息;\\
--exclude=PATTERN #指定排除不需要传输的文件模式\\
--exclude-from=file #文件名所在的目录文件
向备机推数据
rsync -avzP --delete {localDir} root@{remoteHost}:{remoteDir}
# 大文件夹使用参数 az 就可以l
==== 磁盘镜像备份 ====
备份命令 ''%%sudo dd if=/dev/sda1 of=/media/.../back.img%%'' \\
恢复命令 ''%%sudo dd if=/media/.../back.img of=/dev/sda1%%''
压缩备份:''%%sudo dd if=/dev/sda1 status=progress | gzip -c > ~/elf.bak%%'' \\
压缩还原:''%%gunzip -c ~/elf.bak | sudo dd of=/dev/sda1 status=progress%%''
查看磁盘类型:rota 为 1 表示可旋转,即是 SSD 磁盘,否则是 HDD
> lsblk -d -o name,rota
NAME ROTA
loop0 1
==== Restic 备份 ====
新的备份工具,详情参阅[[it:server:restic|Restic 备份]]
===== BTRFS 文件系统 =====
最详细的说明参阅 [[https://wiki.archlinux.org/title/Btrfs_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)|官方文档]] 以及技术分享。
(([[http://www.manongjc.com/detail/14-isxwumlvszulqzz.html|btrfs文件系统简单命令]]))
(([[https://www.howtoing.com/a-beginners-guide-to-btrfs|btrfs的新手指南]]))
(([[https://segmentfault.com/a/1190000008605135|Btrfs文件系统之subvolume与snapshot]]))
很可惜 Mongodb 没有办法运行在 btrfs 格式的文件系统下。主力服务器托管数据库的磁盘使用 LVM 并且格式化为 xfs 格式。主力服务使用它只是为了方便对系统进行备份,这样很重要。
===== 网络驱动器 =====
通过 [[https://www.cnblogs.com/bluestorm/p/11016495.html|映射网络驱动器]] 访问 NAS 共享的文件夹
Ubuntu下映射网络驱动器
简单的说就是采用mount命令将远端磁盘挂载到本地目录。首先在本地建立一个目录用于挂载远端磁盘,为了方便识别这里直接用远端磁盘的ip(x.x.x.x)作为目录名称。然后,采用mount命令进行挂载:''%%sudo mount //x.x.x.x/Data /home/lfqy/x.x.x.x/ -o username=Administrator,password=123%%''。其中,x.x.x.x是远端磁盘所在机器的ip,Data应该是盘符(或驱动器名称,反正对应的是在windows下映射网络驱动器要指定的内容),''%%/home/lfqy/x.x.x.x/%%''是挂载目录,将远端磁盘挂在到本地的该目录,-o 后面利用选项指明远端磁盘所在主机的用户名和密码(一般windows下的用户名都是Administrator,一定不要拼错)。回车,然后输入密码即可挂载成功。
===== 设置环境变量 =====
sudo vim /etc/profile
export NAME=text_value
# 刷新使其生效
source /etc/profile
===== CPU info =====
// cpu
more /proc/cpuinfo
// gpu
sudo lshw -C display
sudo lshw -C video | grep product
''%%lspci%%'' 会显示所有硬件信息
* ''%%lshw%%'' lists all hardware.
* ''%%lshw -short%%'' provides a short hardware summary.
* ''%%Use cat /proc/cpuinfo%%'' for a crude, but quick way to display your processor information.
* ''%%lspci%%'' lists connected PCI cards.
* ''%%lsusb%%'' lists connected USB devices.
* ''%%cat /proc/net/dev%%'' lists all the network interfaces. Alternatively, try lshw -class network.
* ''%%cat /proc/ioports%%'' lists communication ports and adresses
* ''%%cat /proc/iomem%%'' shows the memory map and reserved zones.
* ''%%cat /proc/interrupts%%'' lists the interruption assignments.
* ''%%cat /proc/dma%%'' lists the Direct Memory Accessess
* ''%%lsmod%%'' lists the modules loaded by the kernel, and helpt to identify the hardware configured
===== 邮件转发 =====
安装完 mailutils 之后会自动启动postfix,如果要重新设置则,用命令 ''%%sudo dpkg-reconfigure postfix%%''
sudo apt install mailutils
sudo dpkg-reconfigure postfix
===== Screen 会话窗口 =====
特别适用于远程登录主机,执行很长时间的大任务。它将任务放在一个独立的环境中执行,就算失去连接。重新登录后也能不丢失原先的工作进程。(([[https://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html|screen 命令详解]]))
$> screen
$> [run some shell command or .sh file or nodejs ]
$> Strg+A+D // 退出窗口,甚至可以退出 Terminal
// 重新登录后恢复 screen
$> screen -r
$> screen -list
screen -S 364249.pts-0.serverfin -X quit
* 列举存在的窗口:screen -list 或者 screen -ls
* 退出窗口:''%%ctrl+a%%'',然后松开,接着按 ''%%d%%''
* 进入窗口
* 创建窗口 ''%%screen%%''
* 进入一个唯一的窗口 ''%%screen -r%%''
* 进入多个窗口中的一个 ''%%screen -r 397019.pts-0.serverfin%%''
* 关闭窗口process
* 先进入它,然后按 ''%%ctrl + a%%'',松开,按 ''%%k%%'' ,下面会出现白色提示,选择 ''%%y%%'' 即可永久关闭窗口进程。
* 直接代码关闭 ''%%screen -S 364249.pts-0.serverfin -X quit%%''
===== crontab 定时任务 =====
参考文章:[[https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html|crontab 用法]]
===== 创建swap =====
[[https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04|创建swap]]
===== 远程桌面 =====
[[https://www.cnblogs.com/xingyu666/p/14132923.html#:~:text=%E5%9C%A8Windows%20%E6%90%9C%E7%B4%A2%E6%A0%8F%E8%BE%93%E5%85%A5,%E5%AF%86%E7%A0%81%EF%BC%8C%E7%82%B9%E5%87%BB%E2%80%9COK%E2%80%9D%E3%80%82|Ubuntu 中设置桌面共享]] 以及 [[https://de.wikihow.com/VNC-unter-Mac-OS-X-einrichten|VNC unter Mac OS X einrichten]]
安装文章中的第三种方式XRDP就好,可以直接使用微软的远程桌面。在Mac上要安装[[https://apps.apple.com/us/app/microsoft-remote-desktop/id1295203466?mt=12|微软远程桌面]] (注意需要开通防火墙3389)
apt install ubuntu-desktop
apt install xrdp
修改conf文件并重启
sudo vim /etc/xrdp/xrdp.ini
port=tcp://:3389
systemctl restart xrdp
防火墙放行 3389 端口
====== 用户 ======
查看用户及密码过期时间:''%%chage -l userName%%''
修改过期提示时间:''%%sudo chage -W WARN_DAYS userName%%''
[[https://www.cyberciti.biz/faq/linux-howto-check-user-password-expiration-date-and-time/|Linux Check User Password Expiration Date and Time]]
====== 进程 ======
''%%ps aux | grep -v grep | grep mongod%%'' -> 获取 mongo 进程的 id 为 20255 \\
//mai 20255 1.0 0.5 43884684 46136 ?? R 六01下午 22:26.64 /opt/homebrew/opt/mongodb-community@5.0/bin/mongod --config /opt/homebrew/etc/mongod.conf//
top 命令查询 ''%%top -pid 20255 -l 1 | tail -n 1 | awk '{print $8}'%%''
====== BBR 加速 ======
[[https://blog.mf8.biz/ubuntu-20-04-bbr/|ubuntu 20.4 单边加速]]
修改系统变量:
echo net.core.default_qdisc=fq >> /etc/sysctl.conf
echo net.ipv4.tcp_congestion_control=bbr >> /etc/sysctl.conf
保存生效 ''%%sysctl -P%%''
执行 ''%%sysctl net.ipv4.tcp_available_congestion_control%%'' 如果结果是这样
sysctl net.ipv4.tcp_available_congestion_control
net.ipv4.tcp_available_congestion_control = bbr cubic reno
就开启了。 执行 lsmod | grep bbr ,以检测 BBR 是否开启。结果大约为 tcp_bbr 24576 3
====== 更改主机名 ======
更改服务器名称,hostnamectl 命令无需重启,重新 ssh 连接就可以。
hostnamectl
hostnamectl set-hostname new-hostname
====== 用户 ======
// 将用户username 添加到组别groupName
sudo usermod -a -G groupName userName
sudo adduser user group
更改用户git的bash shell:''%%sudo chsh -s /bin/bash git%%'' (前提是git已近在sudo组别)
开启git用户的sudo权限:''%%sudo visudo%%'' 并添加 ''%%git ALL=(ALL:ALL) ALL%%''
====== 自动更新域名解析 ======
过时了 cloudflare ddns update
[[https://github.com/K0p1-Git/cloudflare-ddns-updater|项目地址]]
[[https://nocache.org/p/setup-cloudflare-dynamic-dns-ddns-with-cli-on-windows-mac-linux#:~:text=Cloudflare%20API%20v4%20enable%20users%20to%20update%20DNS,and%20it%27s%20available%20in%20Windows%2C%20Mac%20and%20Linux.|DDNS 新的文档]]
填写四个项
auth_email="" # 邮箱
auth_method="global" # Set to "global" for Global API Key or "token" for Scoped API Token
auth_key="" # 公共密钥
zone_identifier="" # 区id
record_name="" # 域名 指向家里的ip地址
====== 软件包 ======
===== 查看包 =====
apt-cache search keyword
===== 卸载包 =====
apt-get remove postfix // 普通卸载
apt-get remove --purge postfix // 删除配置
apt-get autoremove --purge postfix // 检查并删除无用依赖包
// 下载包临时存放的目录
ls /var/cache/apt/archives
// 备份当前安装的所有包
dpkg --get-selections | grep -v deinstall > ~/somefile
dpkg --set-selections < ~/somefile // 恢复所有的安装包
sudo dselect
// 清除缓存
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove // 不再使用的孤立软件
====== 防止攻击 ======
[[https://linux.cn/article-5067-1.html|fail2ban 防止ssh 攻击]]
{{tag>vps}}