Linux常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
useradd -g users -m chende # 创建用户

# 没有命令ll,三步走解决问题
vim ~/.bashrc
alias ll='ls $LS_OPTIONS -l'
source ~/.bashrc

# 有些vim复制的行自己动加注释,加入下面这句就可以
vi /root/.vimrc
:set paste
# 如果vim没有颜色,请安装包:zypper in vim-data
# 如果想换一套颜色方案 vi /root/.vimrc 加入下面一行
:colorscheme koehler
# /usr/share/vim/vim74/colors/ 下面很多配色方案[desert, koehler]比较好
# ----------
# centos 下载下面三个包
yum in -y vim-minimal vim-enhanced vim-common
# 之后加入别名 vi ~/.bashrc 加入 alias vi='vim'。配置.vimrc和上面suse一样。

mount -o loop redhat-server-5.4-x86_64-dvd.iso /iso # mount iso文件
cat /etc/resolv.conf 	# DNS配置
cat /etc/hosts		 	# 本地解析
cat /etc/fstab 			# 磁盘挂载

# 文件查找操作相关
ls |wc -l		# 查询文件数,不包含子目录
ls -R |wc -l	# 查询文件数,包含子目录
#-l:统计行
#-w:统计字(英一个字被定义为由空白、跳格或换行字符分隔的字符串)
#-m:统计字符,不能与"-c"一起使用
#-c:统计字节
#-L:打印最长行的长度
find -size -10c -exec rm -rf {} \;# 查找小于10字节的文件,并将他们删除:
du -h --max-depth=0 user # --max-depth=n表示只深入到第n层目录,设置0表示不深入到子目录
find . -name "*.dbf" -atime +5 -exec mv {} /backup/Important/ \;#移动文件到指定目录
# 上面的 -atime +5 指的是 最后一次访问时间在5天前的文件
# 访问时间(access time 简写为atime)
# 修改时间(modify time 简写为mtime)
# 状态修改时间(change time 简写为ctime)

# 磁盘分区常用命令,现在fdisk也支持GPT格式,最好用fdisk工具分区
# 参考:http://www.linuxidc.com/Linux/2015-11/125397.htm
fdisk /dev/sdb
mkfs -t ext4 /dev/sdb1 	  		# 设置文件系统(加-c会check磁盘,很慢,建议不要使用)
tune2fs -c -1 -i -1 /dev/sdb1 	# 关闭系统硬盘自检

# 查询磁盘UUID
方法一:ls -l /dev/disk/by-uuid
方法二:blkid /dev/sdb1

# 磁盘大于2TB,需要使用gpt格式分区,可能旧版本的fdisk不支持,使用命令parted
parted /dev/sdb
mklabel gpt
mkpart primary 0 -1 # 会报下面的错误,意思是没有对齐,影响性能
# Warning: The resulting partition is not properly aligned for best performance.
mkpart primary 2048s 100% # 一般用这句就好了

netstat -ntlp # 查询服务器上监听的所有端口
netstat -nap|grep PID # 查看某个进程对应的连接服务器和端口

# ssh / ftp / vpn 等相关
ssh-keygen -t rsa # 生成对称加密密钥

文件链接

ln 命令用于给文件创建链接,根据 Linux 系统存储文件的特点,链接的方式分为以下 2 种:

  • 软链接:类似于 Windows 系统中给文件创建快捷方式,即产生一个特殊的文件,该文件用来指向另一个文件,此链接方式同样适用于目录。

  • 硬链接:我们知道,文件的基本信息都存储在 inode 中,而硬链接指的就是给一个文件的 inode 分配多个文件名,通过任何一个文件名,都可以找到此文件的 inode,从而读取该文件的数据信息。

基本格式如下:[root@localhost ~]# ln [选项] 源文件 目标文件 选项:

  • -s:建立软链接文件。如果不加 “-s” 选项,则建立硬链接文件;
  • -f:强制。如果目标文件已经存在,则删除目标文件后再建立链接文件;

查询内核参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ulimit -n # 查看用户级的限制,阿里云服务器一般是65535
ulimit -a # 查看当前的各种用户进程限制

ps aux|grep mongo        # 查看进程ID,目前MongoDB的pid进程号是4842
cat /proc/4842/limits    # 查看4842进程号的相关的ulimit的信息
ll /proc/4842/fd | wc -l # 查看4842进程打开了多少文件

# 不重启服务器,动态修改某个进程能打开文件句柄的方法
prlimit --pid 4842 --nofile=1048576:1048576 # 修改命令
cat /proc/4842/limits  |grep files # 查看是否生效

文件打包加解密

1
2
3
4
# 打包压缩加密
tar -czvf - yourfiles |openssl des3 -salt -k passwd |dd of=yourfiles.des3
# 解压缩,解密
dd if=yourfiles.des3 |openssl des3 -d -k passwd |tar xzvf -

网络诊断

参考阅读:(这里推荐了一些不错的命令行监控工具)

https://zhuanlan.zhihu.com/p/269978796

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 查看网卡状态
ethtool eth0

# 查看网络流量:
nethogs eth0
# 网卡的实时流量统计,可以确定端口,通过端口确定PID,通过PID找到程序路径
iftop -i eth0 -n -P
# 查询进程和端口
lsof -p pid
lsof -i :port

w -i # 查看所有连接的IP地址。
tcpdump -i lo -x -s 1024 -vv # 查询路由

# 参考:https://www.npmjs.com/package/n
# 没有外网,npm无法使用的时候,下面的方法安装node
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
bash n lts

# 代理上网
export http_proxy=x.x.x.x:port	# 没有外网的服务器,可以用squid代理上网
export http_proxy=  			# 取消代理

# 添加路由记录
# Linux
route add -net 10.9.1.0/24 gw 10.10.200.200
route del -net 10.9.1.0/24 gw 10.10.200.200
# Windows
route -p add 10.9.1.0/24 10.10.200.200
route delete 10.9.1.0/24 10.10.200.200

# 给一台Linux系统添加外网IP,指定外网网关就可以访问外网了,比如:
ifconfig ens33:111 210.14.xxx.xxx/27
route add -net 0.0.0.0/0 gw 210.14.xxx.xxx

curl命令详解

参考:https://www.cnblogs.com/leizia/p/16322061.html

RedHat(CentOS)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
# centos7有一个配置界面工具: setup,类似suse中的yast。后面版本没有了?
# 开机启动脚本
chmod +x /etc/rc.d/rc.local	# 放在rc.local文件中,必须给执行权限

getenforce							# 查看selinux状态
setenforce 0 						# 重启sshd失败,关闭selinux,但重启失效
SELINUX=disabled					# vi /etc/sysconfig/selinux
systemctl stop firewalld.service	# 可以考虑先关闭防火墙,方便操作
systemctl disable firewalld.service # 永久关闭,不要重启

# 换源++++++++++++++++++++++++
cat /etc/centos-release
rm -rf /etc/yum.repos.d/CentOS-Base.repo
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release # 很多包都找不到,需要启动EPEL源
# 如果还不行,删除以后再重新安装
yum -y rm epel-release
yum -y install epel-release
yum clean all	# 删除缓存
yum makecache 	# 重建源上所有的缓存

# 发现一个centos的源,非常全
http://bay.uchicago.edu/centos-vault/

# 如果需要,启用RPMForge存储库:
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm

# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 复杂的网络配置
hostname # 查看主机名
hostnamectl set-hostname cd1.tl50.com # 修改主机名

# 装机后ifconfig下面可能有个虚拟网桥,主机做虚拟化的时候用到。一般不同可以关闭
#brctl show							# 查看网桥
ifconfig virbr0 down				# 必须先关闭网桥
#brctl delbr virbr0					# 删除网桥
systemctl stop libvirtd.service		# 关闭服务
systemctl disable libvirtd.service	# 禁止开机启动

# 重启网络服务
systemctl status NetworkManager

# 有个可视化工具可以配置IP地址
nmtui # 按 space 键选中某项
nm-connection-editor # 这个工具直接可视化配置IP地址

# 配置网卡,centos8开始不用 systemctrl,而是用nmcli
nmcli device status # 看设备状态
nmcli c show		# 看连接情况
nmcli c up/down ip/name/uuid	# 重启网卡
# centos 的系统如果想修改网卡的名称到 ethx,需要修改内核参数
vi /etc/default/grub
GRUB_CMDLINE_LINUX="... rhgb quiet net.ifnames=0"
# 上面加上net.ifnames=0后,需要生成配置,然后重启服务器
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot

# 如果是虚拟机克隆,MAC地址变化,修改成ifconfig看到的网卡MAC地址
# vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO="static"
HWADDR="00:0C:29:fd:ef:2e"
ONBOOT="yes"
# 添加完了要重启网卡,或者直接重启服务器。
# 有时候虚拟机的网卡总是自动有效和无效,像冲突一样,这个时候重新删除网卡,再添加一次就好了。
# 下面是一份IP配置参考
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static		# 启用静态IP
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
DEVICE=ens33  			# 网卡名
NAME=ens33  
ONBOOT=yes    			# 系统启动时激活网卡
IPADDR=192.168.0.251  	# 你的IP地址
DNS1=8.8.8.8    		# 设置DNS
NETMASK=255.255.255.0   # 设置子网掩码
GATEWAY=192.168.0.1  	# 设置网关
# ++++++++++++++++++++++++++++++++++++++
# DNS解析配置文件
/etc/resolv.conf

# ++++++++++++++++++++++++++++++++++++++
# 开机自动启动
chmod +x /etc/rc.d/rc.local # 设置执行权限
cat /usr/lib/systemd/system/rc-local.service # 看看里面的命令
/etc/rc.d/rc.local start # 尝试执行,看是否报错
# 加路由可能不会成功,因为网络没就绪,无法执行添加路由,前面加一句 [sleep 1] 试一试

vi /usr/lib/systemd/system/rc-local.service
systemctl enable rc-local.service # 可能会报错
# 在 /usr/lib/systemd/system/rc-local.service 最后面加上 下面两行
[Install]
WantedBy=multi-user.target

CentOS 8.3

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
# 下面新配置 centos 需要用到的命令
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
# 有可能上面的源不可用了,可以使用下面的源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/epel-archive-8.repo
# 还可以换腾讯云的源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos8_base.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
# 然后执行
yum -y install epel-release
# 如果还不行,删除以后再重新安装
yum -y rm epel-release
yum -y install epel-release
yum clean all
yum makecache

# 关闭定时更新
systemctl disable dnf-makecache.timer 
systemctl stop dnf-makecache.timer
systemctl status dnf-makecache.timer

yum -y install htop
yum -y install screen
yum -y install net-tools
yum -y install bridge-utils
yum -y install telnet
yum -y install mlocate
yum -y install chrony
yum -y install vim
yum -y install make
yum -y install psmisc
yum -y install tar
yum -y install wget
yum -y install curl
yum -y install gzip
yum -y install unzip
yum -y install rsyslog
yum -y install iproute2
yum -y install subversion
updatedb

yum -y install nodejs

ifconfig virbr0 down
brctl delbr virbr0
systemctl stop libvirtd.service
systemctl disable libvirtd.service

# install docker
curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
# 安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 删除旧包
yum erase podman buildah
yum remove docker docker-common docker-selinux docker-engine
# 或者用下面的一句话安装(不加--allowerasing 可能会报错)
yum install --allowerasing docker-ce docker-ce-cli containerd.io

systemctl enable docker.service
systemctl start docker.service
systemctl stop docker.service
mkdir /home/dockerRoot/
cp -R /var/lib/docker/* /home/dockerRoot

# vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://h8viyefd.mirror.aliyuncs.com"],
  "graph":"/home/dockerRoot",
  "insecure-registries":["10.10.200.11:5000"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-level": "warn",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "5"
  }
}
systemctl start docker.service
# 主机上的一些服务需要关闭,否则可能影响后续docker的运行
systemctl stop getty@tty1.service
systemctl mask getty@tty1.service
systemctl status getty@tty1.service
# ksmtuned.service
systemctl disable ksmtuned.service
systemctl stop ksmtuned.service
systemctl status ksmtuned.service

# 配置开机启动rc.local看上面的介绍

# ssh远程之后可能有下面的提示,这个是Web主机管理启动,可以启用或者关闭
# -> Activate the web console with: systemctl enable --now cockpit.socket
systemctl status cockpit.service
systemctl status cockpit.socket
systemctl mask cockpit.service 
systemctl mask cockpit.socket

# centos 系统可能默认开启 rpcbind服务,对应端口是111,为避免攻击,可以关闭此服务
systemctl disable rpcbind.service
systemctl stop rpcbind.service
systemctl status rpcbind.service
systemctl disable rpcbind.socket
systemctl stop rpcbind.socket
systemctl status rpcbind.socket

SuSE(openSuSE)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 开机启动脚本
chmod +x /etc/init.d/after.local	# 放在after.local文件中,必须给执行权限

# 新系统一般需要安装这些应用
zypper in -y timezone
zypper in -y chrony
zypper in -y htop
zypper in -y screen
zypper in -y lsb-release
zypper in -y nodejs10
zypper in -y subversion
zypper in -y vim
zypper in -y make
zypper in -y gcc
zypper in -y which 
zypper in -y psmisc # 安装 killall 命令
zypper in -y wget
zypper in -y curl
zypper in -y tar
zypper in -y gzip
zypper in -y unzip
zypper in -y syslog-ng
zypper in -y iproute2
zypper in -y mlocate
zypper in -y telnet
zypper in -y iputils # 有ping命令
zypper in -y net-tools-deprecated # ifconfig等工具
zypper in -y cronie # crontab命令

# 换国内的源
zypper ar https://mirrors.aliyun.com/opensuse/distribution/leap/15.2/repo/oss/ oss
zypper ar https://mirrors.aliyun.com/opensuse/distribution/leap/15.2/repo/non-oss/ nonoss
zypper ar https://mirrors.aliyun.com/opensuse/update/leap/15.2/oss/ oss-update
zypper ar https://mirrors.aliyun.com/opensuse/update/leap/15.2/non-oss/ nonoss-update

# 老版本有个德国的源可以用
zypper ar http://ftp5.gwdg.de/pub/opensuse/discontinued/distribution/11.4/repo/oss/ oss
zypper ar http://ftp5.gwdg.de/pub/opensuse/discontinued/distribution/11.4/repo/non-oss/ non-oss

Docker常用配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# 关闭此服务,否则可能CPU跑死到 100%
systemctl stop getty@tty1.service  
systemctl mask getty@tty1.service  

# 注意安装包
zypper in -y vim
zypper in -y make
zypper in -y gcc
zypper in -y which 
zypper in -y psmisc
zypper in -y wget
zypper in -y curl
zypper in -y tar
zypper in -y gzip
zypper in -y unzip
zypper in -y syslog-ng
zypper in -y iproute2
zypper in -y cronie
zypper in -y bash-completion

时间时区设置

ntp ntpdate chrony timedatectl

参考:/2021/05/10181354-ntp-time.html

Debian / Ubuntu

1
2
3
apt-get update
apt-get install vim
apt-get install net-tools # 网络相关命令

GNU Screen

ssh shell 登录 linux 系统之后,默认只有一个窗口,这个窗口运行的程序,在关闭连接之后就自动被kill了;如何多窗口操作,并保持在窗口操作的命令在断开连接之后任然运行呢?有办法,这个工具就是Screen。

GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
zypper in screen        # 安装包

# ssh登录之后
screen -DD -R [name]    # 新建命名会话
screen -x               # 当前所有会话列表
screen -x [name]        # 进入指定会话

# 进入screen之后
ctrl+A (shift)?         # 显示快捷键信息
ctrl+A+D                # 退出当前会话
ctrl+A+C                # 新建窗口
ctrl+A+N                # 下一个窗口
ctrl+A+P                # 上一个窗口
ctrl+A+K                # 杀死当前窗口(suse)
ctrl+A (shift)K         # 杀死当前窗口(centos)
exit                    # 关闭当前窗口
ctrl+A+W                # 当前会话所有窗口列表
ctrl+A ctrl+A           # 最近两个窗口之间切换

Windows

1
2
# 现在Windows支持Linux子系统
# Windows store中找一下 Linux SuSE 等发行版,还可以了解下 Windows Terminel

(完)