linux常用操作
# linux常用操作
# 1. 重复之前的命令
history 显示命令历史列表
↑(Ctrl+p) 显示上一条命令
↓(Ctrl+n) 显示下一条命令
!num 执行命令历史列表的第num条命令
!! 执行上一条命令
!?string? 执行含有string字符串的最新命令
ctrl+r
2
3
4
5
6
7
8
9
10
11
12
13
# 2. linux挂载共享存储
sudo mount -t cifs //192.168.1.85/public /root/public -o username=sheare,password='',vers=2.0,dir_mode=0777,file_mode=0777
sudo mount -t cifs //192.168.1.85/public /home/fengyu/public -o username=sheare,password='',vers=2.0
sudo mount -t cifs //192.168.1.85/public/ansible-install/v1.5.0/bin /home/fengyu/gitlab/InstallationYTung/bin -o username=sheare,password='',vers=2.0
sudo mount -t cifs //192.168.1.85/publicansible-install/v1.5.0/images-pushed /home/fengyu/gitlab/InstallationYTung//home/fengyu/gitlab/InstallationYTung/resources/images-pushed -o username=sheare,password='',vers=2.0
2
3
4
5
# 3. 静态ip
cat /etc/netplan/50-cloud-init.yaml
2
cat << EOF > /etc/netplan/50-cloud-init.yaml
network:
ethernets:
ens33:
dhcp4: no
addresses: [192.168.172.91/24]
optional: true
gateway4: 192.168.172.81
nameservers:
addresses: [8.8.8.8]
version: 2
EOF
2
3
4
5
6
7
8
9
10
11
12
13
ifconfig eth0 192.168.1.4 netmask 255.255.255.0
route add default gw 192.168.172.81
sudo netplan apply
ip addr
2
3
4
# 4. dns配置
vim /etc/systemd/resolved.conf
systemctl restart systemd-resolved.service
systemd-resolve --status
2
3
# 5. 查看那些文件占用多
#磁盘占用情况
df -h
# 查看那个目录空间大
sudo du -s /* | sudo sort -nr
#当前目录文件夹大小情况
du -h --max-depth=1
#是否删除掉的文件仍然被占用
lsof | grep deleted
2
3
4
5
6
7
8
# 6. 杀进程
lsof -i :端口 kill -9 进程ID
lsof -i:8000 kill -9 1730
# 7. 终端走代理
export ALL_PROXY=socks5://127.0.0.1:1080
# 8. 带密码zip文件解压
zipcloak -d unzip
##目录大小
df -h
命令查看整个硬盘的大小 ,-h表示人可读的
du -d 1 -h
命令查看当前目录下所有文件夹的大小 -d 指深度,后面加一个数值
# 9. 查找30天以前的文件并执行操作
find /Users/xuran/.jenkins/userContent -mtime +30 -name "*.*" -exec ls {} \;
find /Users/xuran/.jenkins/job -mtime +30 -name "*.*" -exec ls {} \;
2
# 10. ubuntu打开文件夹
nautilus /etc alias op='nautilus'
# 11. 图片优化
#jpg
sudo apt-get install jpegoptim
find . -name "*.JPG" | xargs jpegoptim
#png
sudo apt-get install optipng
find . -name *.png | xargs optipng
2
3
4
5
6
# 12. 删除30天前的文件
find /Users/xuran/.jenkins/userContent -mtime +200 -name "*.*" -exec ls {} \;
# 13. 新增用户加入sudo
useradd -d /home/apulis -g "0" -u "552" -m -s /bin/bash "apulis"
useradd -d /home/apulis -g "0" -m -s /bin/bash "apulis"
usermod apulis -a -G sudo
vim /etc/sudoers
%sudo ALL=(ALL:ALL) NOPASSWD:ALL
passwd apulis
2
3
4
5
6
7
8
# 14. root远程登录
vim /etc/ssh/sshd_config
PermitRootLogin yes
2
# 15. 免密登录
ssh-copy-id
.ssh/authorized_keys
2
# 16. docker
配置软件源,安装docker
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt upgrade
sudo apt-get --no-install-recommends install -y docker-ce docker-ce-cli containerd.io
sudo apt-get --no-install-recommends install -y python3-pip python3-setuptools
sudo python3 -m pip install setuptools docker-compose
2
3
4
5
6
7
8
9
10
当前用户加入docker用户组
sudo groupadd docker
sudo usermod -aG docker ${USER}
#sudo chmod 666 /var/run/docker.sock
newgrp docker
sudo systemctl restart docker
sudo systemctl enable docker
sudo systemctl start docker
2
3
4
5
6
7
8
如果要配置 Docker 镜像加速,打开 /etc/docker/daemon.json
文件,registry-mirrors 增加或修改,加入https://registry.docker-cn.com
这个地址,也可以填写阿里云腾讯云等镜像加速地址。
示例
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn/",
"https://hub-mirror.c.163.com",
"https://registry.docker-cn.com"
],
"insecure-registries": [
"10.31.3.222:8443"
]
}
2
3
4
5
6
7
8
9
10
11
12
重启 Docker,使配置生效
sudo systemctl daemon-reload
sudo systemctl restart docker
2
# 17. ohmyzsh
安装
sudo apt install zsh -y
chsh -s /bin/zsh
sudo reboot
echo $SHELL
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
2
3
4
5
6
7
alias vs='code $1'
alias open='nautilus $1'
alias open='xdg-open'
alias idea='_a(){ pycharm64.exe $1 &}; _a'
alias pycharm='_a(){ idea64.exe $1 &}; _a'
plugins=( z git extract sudo web-search last-working-dir zsh-autosuggestions zsh-syntax-highlighting)
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
2
3
4
5
6
7
8
9
10
11
# 18. crontab
sudo service cron start
crontab -e
2
# 19. pip源
pip config set install.trusted-host mirrors.aliyun.com
# 19. gpu驱动
sudo apt install ubuntu-drivers-common
ubuntu-drivers devices
sudo ubuntu-drivers autoinstall
sudo apt install nvidia-driver-455
nvidia-smi
2
3
4
5
# 21. 启用https
# 启用HTTPS
$ sudo apt install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
2
3
4
5
6
7
# 22. 查看本地公网ip
curl myip.ipip.net
##快速查询目录大小
# 23. autossh
autossh -p 50018 -NCPfg root@119.147.212.162 -L 3388:127.0.0.1:5432
# 24. 问题解决
# 24.1. NO_PUBKEY
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com `sudo apt update 2>&1 | grep -o '[0-9A-Z]\{16\}$' | xargs`
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com `sudo apt update 2>&1 | grep -o '[0-9A-Z]\{16\}$' | xargs`
2
3
安装y-ppa-manager
sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install y-ppa-manager
2
3
# 24.2. linuxmint国内源
ubuntu20.04版本代号focal
清华linuxmint源 (opens new window)
编辑 /etc/apt/sources.list.d/official-package-repositories.list
:
- 对于基于 Ubuntu 的原版,以 Linuxmint 19 为例:
deb https://mirrors.ustc.edu.cn/linuxmint/ tara main upstream import backport deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb http://archive.canonical.com/ubuntu/ bionic partner
- 对于基于 Debian 的 LMDE,以 LMDE 2 为例:
deb https://mirrors.ustc.edu.cn/linuxmint/ betsy main upstream import deb https://mirrors.ustc.edu.cn/debian jessie main contrib non-free deb https://mirrors.ustc.edu.cn/debian jessie-updates main contrib non-free deb https://mirrors.ustc.edu.cn/debian jessie-backports main contrib non-free deb https://mirrors.ustc.edu.cn/debian-security/ jessie/updates main non-free contrib deb https://mirrors.ustc.edu.cn/deb-multimedia/ jessie main non-free
然后运行 sudo apt-get update
更新索引以生效。
小技巧
完成后请不要再使用 mintsources(自带的图形化软件源设置工具)进行任何操作,因为在操作后,无论是否有按“确定”,mintsources 均会复写 /etc/apt/sources.list.d/official-package-repositories.list
。
# 24.3. 解决apt-get安装中的E: Sub-process /usr/bin/dpkg returned an error code
在用apt-get安装软件包的时候遇到E: Sub-process /usr/bin/dpkg returned an error code (1)问题,解决方法如下:
cd /var/lib/dpkg/
sudo mv info/ info_bak
sudo mkdir info
sudo apt-get update
sudo apt-get -f install
sudo mv info/* info_bak/
sudo rm -rf info
sudo mv info_bak info
2
3
4
5
6
7
8
# 24.4. dns配置
vim /etc/systemd/resolved.conf
systemctl restart systemd-resolved.service
systemd-resolve --status
2
3