搬山行者

无志愁压头,有志能搬山

业余程序员的学习笔记~


Ubuntu学习

目录

更新源

ubuntu20
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

sudo vim /etc/apt/sources.list

#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
图形化更新
https://blog.csdn.net/bingqingsuimeng/article/details/80000228

软件安装

ssh-server
install
sudo apt-get install openssh-server

config root
ps -e |grep ssh

vim /etc/ssh/sshd_config
PermitRootLogin  no 改为yes
service sshd restart

redis-cli
apt install redis-tools
pdf阅读器
sudo apt-get install okular
chrome

参考链接

https://registry.npmmirror.com/binary.html?path=chromedriver/

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*; sudo apt-get -f install
firefox webdriver

下载链接

sudo mv geckodriver /usr/local/bin/
测试代码
from selenium import webdriver
driver = webdriver.Firefox()
driver.close()
静默启动
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
firefox_options.add_argument('--headless')
driver = webdriver.Firefox(firefox_options=firefox_options)

常用命令

新建sudo用户

adduser sysops
usermod -aG sudo sysops

sudo useradd -r -m -s /bin/bash myttt
查找用户
 cat  /etc/passwd | grep runner
文件相关

参考

查找大文件
find / -type f  -size +100000k
查看目录下每个文件夹的大小
du -sh *
查找上G和T的目录并排序
du -h --max-depth=1 | grep [TG] |sort
du -h --max-depth=1 | sort
删除7天前的文件
find . -mtime  +7   -name "*.sql" -exec rm -rf {} \;
删除当前目录下100天前的目录
find . -maxdepth 1 -mtime  +100 -type d -exec rm -rf {} \;
列出文件列表时忽略某些文件
# 列出/path/to/dir下的文件和目录,去除后缀是jpg的文件
ls /path/to/dir -I "*.jpg"

# 多个-I一起使用
ls /path/to/dir -I "*.jpg" -I "*.avi"
文件比较
cmp -l file1.bin file2.bin | awk '{print $1, $2, $3}'
tar

打包当前目录

tar czvf my.tar .

打包时排除某些目录和文件

tar --warning=no-file-changed --exclude '*.pyc' --exclude .gitignore --exclude .git -czvf tester.tar.gz . && true=0 || false=1

将执行该tar命令的用户作为解压后的文件目录的所有者

tar --no-same-owner -zxvf xxxx.tar.gz

解压到指定目录

tar -zxvf /home/tesrter/deploy/server/tester.tar.gz -C /home/tesrter/deploy/server/

解压xz压缩包

tar -xf archive.tar.xz
sed

全局替换文件中字符串

sed -i 's/wulan/tesrter/g' supervisord.conf
查看串口信息

参考

sudo cat /proc/tty/driver/serial
查找目录并删除
find . -name .git | xargs rm -rf
列出文件夹树结构
tree cases/ --sort=size  -L 1
查看系统日志占用空间
journalctl --disk-usage 
清理系统日志
sudo journalctl --vacuum-size=10M
sudo journalctl --vacuum-time=1w
显示分辨率
xrandr
xrandr -s 1024x768

batchrc 配置

显示git分支
vim ~/.bashrc
function git-branch-name {
  git symbolic-ref --short -q HEAD 2>/dev/null
}
function git-branch-prompt {
  local branch=`git-branch-name`
  if [ $branch ]; then printf " [%s]" $branch; fi
}
PS1="\u@\h \[\033[0;36m\]\W\[\033[0m\]\[\033[0;32m\]\$(git-branch-prompt)\[\033[0m\] \$ "
历史记录补全
vim ~/.bashrc
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

Shell 脚本

set 命令
set -u 
# 检查脚本内的变量,如有变量未定义将终止,属于脚本的自检测功能
set -e
# 执行出错立刻终止
set -o pipefail
# 管道命令部分执行出错也立刻终止
set -x
# 显示脚本的执行过程

FAQ

/dev/loop 问题
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.8G     0  7.8G   0% /dev
tmpfs           1.6G  2.0M  1.6G   1% /run
/dev/sda1        98G   62G   32G  67% /
tmpfs           7.9G   76K  7.9G   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/loop2      2.7M  2.7M     0 100% /snap/gnome-system-monitor/169
/dev/loop3      640K  640K     0 100% /snap/gnome-logs/106
/dev/loop1       62M   62M     0 100% /snap/core20/1328
/dev/loop0       66M   66M     0 100% /snap/gtk-common-themes/1519
/dev/loop4      165M  165M     0 100% /snap/gnome-3-28-1804/161
/dev/loop5      100M  100M     0 100% /snap/core/11993
/dev/loop6      768K  768K     0 100% /snap/gnome-characters/741
/dev/loop7       56M   56M     0 100% /snap/core18/2253
/dev/loop8      2.7M  2.7M     0 100% /snap/gnome-calculator/920
/dev/loop9       66M   66M     0 100% /snap/gtk-common-themes/1515
/dev/loop11     2.5M  2.5M     0 100% /snap/gnome-calculator/884
/dev/loop12     219M  219M     0 100% /snap/gnome-3-34-1804/77
/dev/loop14      62M   62M     0 100% /snap/core20/1270
/dev/loop13     219M  219M     0 100% /snap/gnome-3-34-1804/72
/dev/loop15     248M  248M     0 100% /snap/gnome-3-38-2004/87

解决方法:

sudo apt autoremove --purge snapd

是否有副作用还未知

卸载了系统自带的python后抢救
sudo dpkg --configure -a
sudo apt-get install -f
sudo apt-get install ubuntu-minimal
sudo apt-get install ubuntu-desktop

ubuntu20 虚拟机网络接口没有启动
sudo dhclient
140.82.114.4
修复apt错误
1.将**info**文件夹更名

sudo mv /var/**lib**/**dpkg**/**info** /**var**/**lib**/**dpkg**/**info_old** 
 

2.新建一个新的**info**文件夹

sudo mkdir /var/**lib**/**dpkg**/**info**

3.确保软件包列表是否为最新的

sudo apt-get update

  

4.修复依赖关系,安装程序包所依赖的包

apt-get -f install

  

5.执行完上一步操作后会在新的info文件夹下生成一些文件,现将这些文件全部移到info_old文件夹下:

sudo mv /var/**lib**/**dpkg**/**info**/* /**var**/**lib**/**dpkg**/**info_old**

  

6.把自己新建的info文件夹删掉

sudo rm -rf /var/lib/dpkg/info

  

7.把以前的info文件夹重新改回名字

 sudo mv /var/lib/dpkg/info_old /var/lib/dpkg/info
移除Snap
snap list

sudo systemctl disable snapd.service
sudo systemctl disable snapd.socket
sudo systemctl disable snapd.seeded.service

# remove packages in snap list
sudo snap remove firefox
sudo snap remove snap-store

sudo rm -rf /var/cache/snapd/
sudo apt autoremove --purge snapd
rm -rf ~/snap
sudo nano /etc/apt/preferences.d/firefox-no-snap

type conent:

Package: firefox*
Pin: release o=Ubuntu*
Pin-Priority: -1

install firefox

sudo add-apt-repository ppa:mozillateam/ppa
sudo apt update
sudo apt install firefox
忽略合盖
sudo vim /etc/systemd/logind.conf
HandleLidSwitch=ignore               # 接内部电源
HandleLidSwitchExternalPower=ignore  # 接外部电源
sudo systemctl restart systemd-logind