63 篇文章带有标签 “linux”

命令history

操作不记录在历史记录中

有选择性地进行记录(操作命令前面加空格就不记录)

  • 当前会话临时生效
HISTCONTROL=ignorespace
  • 持久化设置,可以修改配置文件:.bash_profile 或 .bashrc。执行source命令后,设置生效。你也可以退出后重新登录。
$ nano .bashrc
export HISTCONTROL=ignorespace
$ source .bashrc

所有的操作不记录

HISTSIZE=0

只在当前会话中起作用,要想持久化请参考上面的设置。

删除历史记录中的某行

history -d linenumber

清除历史记录

history -c

参考资料

命令find

查找文件

-name 或 -iname(大小写不敏感)

find . -name "*.pyc"

-m(最近多长时间修改)

min, 分钟
time, 哪天 0(24小时) 1(24-48小时) 2(48-72小时)
  • 显示 /var/log 目录下最近 10分钟内修改的文件
$ find /var/log -mmin -10
/var/log/messages
  • 以详细信息显示 /var/log 目录下最近 24小时内修改的文件
$ find /var/log -mtime 0 -ls
 33575669      4 drwxr-xr-x  15  root     root         4096 7月 29 06:33 /var/log
 34131780    164 -rw-------   1  root     root       166061 7月 29 10:20 /var/log/messages
  • 显示 /var/log 目录下最近 [0 - 24小时] 修改的文件
find /var/log -mtime -1
  • 显示 /var/log 目录下最近 [24 - 48小时] 修改的文件
find /var/log -mtime 1
  • 显示 /var/log 目录下最近 [48 - ] 修改的文件
find /var/log -mtime +1

-size(文件大小) 显示当前目录下超过 40M的文件 $ find .

命令grep

搜索文件内容

  • 搜索一个文件
grep 'text' hello.txt
  • 忽略字母大小写(-i)
grep -i 'text' hello.txt
  • 搜索多个文件
grep 'text' hello.txt hi.txt
  • 搜索当前目录下所有文件
grep 'text' *
  • 搜索当前目录(包含子目录 -R)下所有文件
grep -R 'text' *

匹配搜索

  • 搜索 pip 配置文件的路径 -R(遍历) -n(行号) -H(文件名)
grep "index-url" ~/.config -RnH
/home/lnsoft/.config/pip/pip.conf:2:index-url = https://mirrors.aliyun.com/pypi/simple/
  • 搜索 pip 配置文件的路径(增加过滤)
find ~ -name pip* | xargs -i grep "index-url" {} --color -nH
/home/lnsoft/.config/pip/pip.conf:2:index-url = https://mirrors.aliyun.com/pypi/simple/
  • 只匹配字符串,不使用正则表达式。
find . | grep -F .run

图像格式转换、尺寸调整

查看图像信息

$ file test.jpg
test.jpg: JPEG image data, JFIF standard 1.02, resolution (DPI), density 96x96, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=7, orientation=upper-left, xresolution=98, yresolution=106, resolutionunit=2, software=Adobe Photoshop CS Windows, datetime=2013:03:18 11:45:34], baseline, precision 8, 750x499, frames 3

ImageMagick

ImageMagick是一个用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装。它可以读取、编辑超过100种图像格式。

安装

  • Ubuntu
sudo apt-get install imagemagick
  • macOS
brew install imagemagick

格式转换

convert test.jpg test.png

灰度

convert -colorspace gray input_file output_file

命令wget

下载多个文件

  • 空格分割
wget https://upload.wikimedia.org/wikipedia/commons/1/13/Intel_CPU_Core_i7_6700K_Skylake_perspective.jpg https://images-na.ssl-images-amazon.com/images/I/51iVSqLIBWL._AC_.jpg
  • 来自文件(-i)
wget -i urls.txt

后台下载(-b)

wget -i urls.txt -b

指定输出目录

wget -i urls.txt -P output

断点续传(-c --continue)

wget -c https://github.com/goharbor/harbor/releases/download/v2.1.3/harbor-offline-installer-v2.1.3.tgz

命令ls

可读的方式显示文件大小

# -h, --human-readable with -l and/or -s, print human readable sizes (e.g., 1K 234M 2G)
ls -lh
ll -h

显示目录的所有子目录的内容

# -R, --recursive list subdirectories recursively
ls -lR
ll -R

按时间进行排序

按时间降序显示当前目录

# -l use a long listing format; -t sort by modification time, newest first
ls -lt
ll -t

按时间升序显示当前目录

# -r --reverse reverse order while sorting
ls -lrt
ll -rt

按大小进行排序

按大小降序显示当前目录

# -S sort by file size, largest first
ls -lS
ll -S

按大小升序显示当前目录

ls -lrS
ll -rS

统计目录下文件数量

统计当前目录下的文件数量

ls -l | grep "^-" | wc -l

统计当前目录下的目录数量

ls -l | grep "^d" | wc -l

统计当前目录下(包含子目录)的文件数量 ls -lR | grep

SSH允许使用密码进行root登录

安装了Ubuntu系统后,默认ssh不允许使用密码进行root登录,通过如何配置可以实现允许。

登录root

$ su - root

查看ssh配置文件中的PermitRootLogin项

$ nano /etc/ssh/sshd_config
# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

修改ssh配置文件中的PermitRootLogin项:PermitRootLogin yes

$ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

查看ssh配置文件中的PermitRootLogin项

nano /etc/ssh/sshd_config
# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

重启sshd服务

  • systemctl
$ systemctl restart sshd

Linux上查找系统信息

操作系统

Linux内核版本

  • uname
$ uname -r
4.18.0-147.5.1.el8_1.x86_64
  • /proc/version
$ cat /proc/version
Linux version 4.18.0-147.5.1.el8_1.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Wed Feb 5 02:00:39 UTC 2020
  • hostnamectl
$ hostnamectl | grep Kernel
            Kernel: Linux 4.18.0-147.5.1.el8_1.x86_64

查找CODENAME

$ cat /etc/os-release | grep VERSION_CODENAME 
VERSION_CODENAME=focal

操作系统信息

$ lsb_release -a
  • Ubuntu
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04 LTS
Release:	20.04
Codename:	focal
  • CentOS
LSB Version:	:core-4.1-amd64:core-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 8.1.1911 (Core) 
Release:	8.1.1911
Codename:	Core

在Ubuntu上安装NVIDIA GPU驱动

在一台新安装的 Ubuntu20.04 系统上安装 NVIDIA GPU 驱动。

安装 gcc make 工具

$ sudo apt-get install gcc make

禁用系统默认驱动 nouveau

  1. 编辑配置文件
$ sudo nano /etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
options nouveau modeset=0
## 另一种方法
# cat << EOF >/etc/modprobe.d/blacklist-nouveau.conf
blacklist nouveau
options nouveau modeset=0
EOF
  1. 更新 initramfs
$ sudo update-initramfs -u
  1. 重启系统
$ sudo reboot
  1. 验证 nouveau 是否禁用成功(当什么也不显示出来时代表成功)
$ lsmod | grep nouveau

安装 NVIDIA 驱动 查看显卡型号 $ lspci | grep -i nvidia 0000:43:00.0 3D controller: NVIDIA Corporation TU104GL [Tesla T4] (rev a1) 0000:47:00.

Linux系统禁用交换分区

Kubernetes集群为了不影响性能要禁用交换分区。

查看交换区信息

$ swapon --show

临时禁用 /proc/swaps 中的交换分区(系统重启后会失效)

$ sudo swapoff -a

启用 /etc/fstab 中的所有交换区

$ sudo swapon -a

禁用交换分区(系统重启后也有效)

$ sudo sed -i '/swap/s/^/#/' /etc/fstab

查看是否禁用交换分区(什么也不显示代表成功)

$ swapon

参考资料

Linux系统DNS设置

之前在文件/etc/resolv.conf中设置了,过段时间总是自动恢复默认值。(注释中写的很详细,是不可编辑的由系统自动生成的文件)

/etc/resolv.conf # This file is managed by man:systemd-resolved(8). Do not edit. # # This is a dynamic resolv.conf file for connecting local clients to the # internal DNS stub resolver of systemd-resolved. This file lists all # configured search domains. # # Run "resolvectl status" to see details about the uplink DNS servers # currently in use. # # Third party programs must not access this file directly, but only through the # symlink at /etc/resolv.conf. To manage man:resolv.

Linux系统上修改用户名

今天同事安装了一台新的服务器Ubuntu20.04,但用户名和主机名不是我想要的,这里尝试了直接修改Linux文件的方式。

登录root用户

$ su - root

修改用户信息

/etc/passwd

# nano /etc/passwd
username:x:1000:1000:username:/home/username:/bin/bash

/etc/shadow

# nano /etc/shadow
username:D78/D2/DdYW.FVG.GlqDlZsZ4sK21gSxhDooqWlJtCVl3oUbDUTKtGxBWkCE3E/Oha40kjDrk0pBbsvT4TwtzuH61vYmnJ/GY.bAHWbVv1:18545:0:99999:7:::

/etc/group

# nano /etc/group
adm:x:4:syslog,username
cdrom:x:24:username
sudo:x:27:username
dip:x:30:username
plugdev:x:46:username
lxd:x:116:username
username:x:1000:

修改HOME路径

# mv /home/old_username /home/new_username

修改主机名 /etc/hostname # nano /etc/hostname hos

在Ubuntu上安装nvidia-docker2

在 Ubuntu20.04 上安装 nvidia-docker2

配置 apt 仓库(repository)

$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
  sudo apt-key add -
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-docker.list

更新安装包的列表

$ sudo apt-get update

安装 nvidia-docker2

$ sudo apt-get install -y nvidia-docker2
  • 安装后可以查看 nvidia runtime 配置
$ nano /etc/docker/daemon.json
{
    "runtimes": {
        "nvidia": {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}

在Ubuntu上安装Docker

在 Ubuntu20.04 上安装 Docker

安装 Docker

$ curl -fsSL https://get.docker.com | sh -

把用户名(username)加入到 docker 组,这样以后在非 root 用户下不用每次操作都 sudo

$ sudo usermod -aG docker username

需要退出用户会话,重新登录方可生效。

卸载Docker

  • 删除Docker及其依赖
$ sudo apt-get remove --auto-remove docker
  • 删除所有数据
$ sudo rm -rf /var/lib/docker

NFS配置

Ubuntu

服务端

  • 安装服务端
sudo apt install nfs-kernel-server
  • 查看服务状态
sudo systemctl status nfs-server
  • 查看开启的NFS协议
sudo cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2
  • 配置访问的路径
sudo nano /etc/exports
/data/nfs        172.16.33.0/24(rw,sync,fsid=0,crossmnt,no_subtree_check)
  • 应用配置
sudo exportfs -ra
  • 查看当前应用
sudo exportfs -v
  • 重启服务
sudo systemctl restart nfs-server

客户端

  • 安装客户端
sudo apt install nfs-common
  • 查看NFS服务器导出列表
showmount -e 172.16.33.157
Export list for 172.16.33.157:
/data/nfs 172.16.33.0/24,172.16.128.164
  • 挂载NFS
sudo mount -t nfs 172.16.33.157:/ $(pwd)/nfs
  • 移除挂载
sudo umount $(pwd)/nfs

参考资料 如何在Ubuntu 18.04上安装和配置NFS服务器 ubuntu18.

磁盘:分区-格式化-挂载

分区

列出块设备信息

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 71.3M  1 loop /snap/lxd/18772
loop1    7:1    0 55.4M  1 loop /snap/core18/1944
loop2    7:2    0 31.1M  1 loop /snap/snapd/10492
loop3    7:3    0 31.1M  1 loop /snap/snapd/10707
loop4    7:4    0 71.2M  1 loop /snap/lxd/18674
loop5    7:5    0 55.4M  1 loop /snap/core18/1932
sda      8:0    1  558G  0 disk 
├─sda1   8:1    1    1M  0 part 
└─sda2   8:2    1  558G  0 part /
sdb      8:16   1  2.2T  0 disk 

创建分区 sudo fdisk /dev/sdb Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.

Linux设置时区

查看系统的时区

$ ll /etc/localtime
lrwxrwxrwx. 1 root root 35 2月  18 2020 /etc/localtime -> ../usr/share/zoneinfo/UTC

列出有效的时区

$ timedatectl list-timezones
Africa/Abidjan
......
America/New_York
......
Asia/Shanghai
......

设置时区

# timedatectl set-timezone Asia/Shanghai

查看系统的时区

$ ll /etc/localtime
lrwxrwxrwx. 1 root root 35 2月  18 2020 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

SSH使用密匙登录

生成身份验证密钥

ssh-keygen -t rsa,在~/.ssh/目录下生成私匙id_rsa和公匙id_rsa.pub两个文件。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 直接回车
Enter passphrase (empty for no passphrase): 直接回车
Enter same passphrase again: 直接回车
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:foo-YWwIv/a/HGEGt9P6vvmff/QjBGEvzlYM4hBWeR0 username@hostname
The key's randomart image is:
+---[RSA 2048]----+
|         +oo.ooE.|
|        . o..o+. |
|      . .  .. .+ |
|  .    o o  o.o  |
|   o o  S .  +.  |
|    o =+ +  .. ..|
|     + .+ . . o..|
|    o .o = . + .o|
|   . o+o=o+o. o..|
+----[SHA256]-----+