Linux Notes&& Basic Linux Commands and Syntax

Article directory

Shortcut key https://note.youdao.com/s/WP86L4T7

ctrl+a Cursor moves command line header
ctrl+e Move the cursor to the end of the command line
ctrl+alt+F1 Switch to graphical interface
ctrl+alt+F2~F6 Switch character interface
ctrl shift + Enlarge interface
ctrl shift T Open a new terminal
ctrl + Reduce the interface
ctrl+w delete previous word
ctrl+d Delete the following letters
ctrl+c Comment code
ctrl+u Clear to beginning of line
ctrl +k Delete to end of line

Command syntax format

 command     [options]...      [argument]..

  命令        [选项]         [参数]


offline environment

Machine VM Must be started
VMware foundation platform *
KVM classroom dns,yum,repo,rc.local… *
KVM bastion Gateway system(router) *
KVM utility podman Server
KVM workstation Client - GUI
KVM serve Client - CLI *
KVM server{b-e} Client - CLI

Exam environment installation

[kiosk@foundation0 ~]$ su - root
Password: 
Last login: Fri Oct  8 18:27:05 GMT 2021 from ::1 on pts/3
[root@foundation0 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  9.9M  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/nvme0n1p4  491G   62G  430G  13% /
/dev/nvme0n1p2 1014M  659M  356M  65% /boot
/dev/loop0      7.4G  7.4G     0 100% /content/rhel8.4/x86_64/rhel8-additional
/dev/loop2      6.7G  6.7G     0 100% /content/rhel8.0/x86_64/dvd
/dev/loop1      9.5G  9.5G     0 100% /content/rhel8.4/x86_64/dvd
/dev/nvme0n1p1  200M  6.6M  194M   4% /boot/efi
tmpfs           796M  3.5M  792M   1% /run/user/1000
/dev/sr0        5.9M  5.9M     0 100% /run/media/kiosk/CDROM

[root@foundation0 ~]# yum install -y /run/media/kiosk/CDROM/ex200v82-4.8-211016.x86_64.rpm 

Environment command + password

切换用户
[kiosk@foundation0 ~]$ su -
Password: 
Last login: Fri Oct  8 18:27:05 GMT 2021 from ::1 on pts/3
[root@foundation0 ~]# su -

锁屏解锁密码:redhat

重置课程
[kiosk@foundation0 ~]$ rht-clearcourse 0
[kiosk@foundation0 ~]$ rht-setcourse rh124

查看环境
[kiosk@foundation0 ~]$ cat /etc/rht
RHT_VENUE=ilt
RHT_ENROLLMENT=0
RHT_ROLE=foundation
RHT_COURSE=rh124
RHT_TITLE="Red Hat System Administration I"
RHT_VMS="bastion workstation utility servera serverb "
RHT_VM0="classroom "
RHT_GVMS=""
RHT_PRIVUSEOVS=""
RHT_NOSTARTVMS=""
RHT_NOFINISHVMS=""
RHT_VMTREE=rhel8.2/x86_64


启动相应的机器

configure ip

1.配置ip
[root@localhost ~]# nmcli connection modify "ens32" ipv4.method manual ipv4.addresses  "192.168.1.10/24" ipv4.gateway "192.168.1.254" ipv4.dns "8.8.8.8" connection.autoconnect yes
2.重启网络(红帽8)
[root@servera ~]# nmcli connection down Wired\ connection\ 1  && nmcli connection up Wired\ connection\ 1

重启网络(Centos7)
[root@servera ~]#systemctl restart network

3.检查IP、掩码、网关、DNS
#查看主机名
[root@node1 ~]# hostname    
node1.domain250.example.com
#查网关
[root@node1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.250.254  0.0.0.0         UG    100    0        0 eth0
172.25.250.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
#查ip
[root@node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:00:fa:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.250.100/24 brd 172.25.250.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::b3ba:2f60:8a37:f33e/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
#查看dns
[root@node1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search domain250.example.com
nameserver 172.25.250.254


设置主机名
[root@servera ~]#hostnamectl set-hostname node1.domain250.example.com

vim editor

  • Three modes of vim
命令模式(command):用vim打开一个文件,此时就是命令模式,标识符:1.光标闪动;2.最后一行有文件详细信息
输入模式(insert):按aio任意一个字符进入输入模式,标识符:insert
			a:光标之后输入
			A:光标所在行行尾输入
			I:光标所在行行首输入
			i:光标之前输入
			o:光标下一行输入
			O:光标上一行输入
末行模式(ex):最后一行做操作,标识符:“:”(冒号)
  • Switching between three modes
1.命令模式 aio ---->  输入模式
		  <-----esc
2.命令模式 : ---->  末行模式
		  <-----esc
  • Cursor movement
1.上下左右方向键
2.hjkl左下上右
  • Inline jump
命令模式
行首:home/^(shift 6)
行尾:end/$(shift 4)
单词:w
  • Jump between lines
命令模式
gg:跳转到首行
G:跳转到尾行
5gg/5G:跳转到第5行

末行模式
:3:跳转至第3行
:%:跳转至最后一行
  • Page jump
pgup
pgdown
  • copy
命令模式
	yy:复制光标所在行
	yw:复制单词
	3yy:复制光标所在行到下面的2行(包含光标所在行)
末行模式
	:8,26y:复制8到26行
	:%y:复制全文
  • Paste
p(小):粘贴到光标下一行
P(大):粘贴到光标上一行
  • delete
命令模式
	dd:删除光标所在行
	dw:删除单词
	3dd:删除光标所在行到下面的2行(包含光标所在行)
	x/del:删除光标所在字符
	D:删除光标所在字符到行尾的内容
	dgg:删除光标所在行到首行内容
	dG:删除光标所在行到尾行的内容
	ct" 删除从光标到"位置之间的字符
末行模式
	:8,26d:删除8到26行
	:%d:删除全文
  • Cut: Delete + Paste
  • withdraw
命令模式
	u:一步步撤回
	
反撤回:ctrl r
  • Save, exit
命令模式
	ZZ(大写)
末行模式
	!表示强制(:wq!)
	:w保存
	:q退出
	:wq/x保存退出
调用:
	在vim中:!hostname  调用主机名
  • Set line number, cancel line number
末行模式
:set nu   设置行号
:set nonu 取消行号
:noh 	  取消高亮
  • Find
命令模式
/root 
	n:往下
	N:往上
?root 
	n:往上
	N:往下
  • replace
末行模式
:s/root/qq/   将光标所在行的第一个root替换为qq
:s/root/qq/g   将光标所在行的所有root都替换为qq
:1,5s/bin/wx/g  将1到5行的所有root都替换为qq
:%s/bin/wx/g   将全文的所有root都替换为qq
  • Batch annotation
末行模式
:1,5s/^/#
:1,5s#^#//

可视化视图
1.ctrl v
2.选中你要注释的行
3.按I(大写)
4.按#
5.esc(两下)
  • Cancel batch comments
末行模式
:1,5s/#//

可视化视图
1.ctrl v
2.选中你要取消注释的行
3.按d
  • read, write
    vim 2.txt
    :r /etc/hosts
    在光标下一行读入
    
    写入
    vim 2.txt
    :w /tmp/20.txt
    
    

    practise

    1.通过cp /etc/passwd /tmp/生成vim练习模板文件,用vi编辑器打开/tmp/passwd文件,并以此文件作为练习素材。 
    2.在/tmp/passwd文件中显示行号。 
    3.在第5行后读入/etc/hosts和/etc/selinux/config的内容。 
    4.切换到最后一行,读入/etc/passwd的内容。 
    5.删除10到20行的内容。 
    6.将6、7行剪切到100行后。 
    7.在文件中查找”bin”字符串。 
    8.将文件中所有的”bin”字符串替换为”easthome”。 
    9.保存该文件后退出vi编辑器。 
    10.滤除该文件所有的空行之后,保存到/media/passwd.txt。 
    11.删除/root/passwd.txt。 
    

man

[kiosk@foundation0 ~]$ man chown 
/Ex

Redirect

#              >   覆盖
[root@node1 ~]# echo "123" > 1.txt

#              >>    追加
[root@node1 ~]# echo "123" >>1.txt

#              2>   错误重定向:将错误内容放到后面的文件中
[root@node1 ~]# cat 1.txt 2.txt
123
123
cat: 2.txt: No such file or directory
[root@node1 ~]# cat 1.txt 2.txt 2>10.txt
123
123

#              &>     混合重定向:将正常和错误的全都放到后面那个文件
[root@node1 ~]# cat 1.txt 2.txt &> 20.txt
[root@node1 ~]# cat 20.txt
123
123
cat: 2.txt: No such file or directory


find

find 
--name  查找名字为 file 的文件
--user  查找归 jacques 所有的所有文件
[root@node1 ~]# mkdir /root/findfiles
[root@node1 ~]# find / -user jacques -exec cp -a {} /root/findfiles \;
find: ‘/proc/4828/task/4828/fd/7’: No such file or directory
find: ‘/proc/4828/task/4828/fdinfo/7’: No such file or directory
find: ‘/proc/4828/fd/8’: No such file or directory
find: ‘/proc/4828/fdinfo/8’: No such file or directory

grep

grep root /etc/passwd
#         -i 忽略大小写
#         -v 取反
#         ^root  以root开头的行
#         ^#     
#         bash$  以bash结尾
#         ^$	   空行

#    -i  忽略大小写
[root@node1 ~]# grep -i root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
#     -v  取反
[root@node1 ~]# grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

#     将/etc/resolv.conf中含na的行不含空格写到/root/lines.txt中
[root@node1 ~]# grep na /etc/resolv.conf | grep  -v ^$ > /root/lines.txt
[root@node1 ~]# cat /root/lines.txt
# Generated by NetworkManager
nameserver 172.25.250.254

sed

Use line number as pattern

effect Format describe
separate line numbers 1、$ first line, last line
start line, end line 1,5 Process rows 1 to 5
Starting line ~ step length 2~2 Starting from the second row, process every two rows
Starting position+N 3+5 Process lines 3 to 8

Regular pattern

Format demonstration
/regular expression/ /^root/

sed subcommand

The subcommands of sed tell sed what operations to perform on the specified line, including adding, deleting, modifying, and viewing (printing).

! Not, placed in front of the command, means negation
d delete, means delete
p print means agree (display)
#-n        默认输出,一般和p一起用
#-i        直接修改源文件
#-r        允许使用扩展正则
#-e        允许进行多项编辑


#p    打印    
[root@tang ~]# sed /^root/p /tmp/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

#d    删除
[root@tang ~]# sed /^root/d /tmp/passwd 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync
#a     在被匹配行下一行插入
[root@tang ~]# sed /^bin/a/tanghuiyang /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
/tanghuiyang
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

#i     在被匹配行上一行插入
[root@tang ~]# sed /^bin/i/tanghuiyang /tmp/passwd 
root:x:0:0:root:/root:/bin/bash
/tanghuiyang
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

#c     覆盖被匹配的行
[root@tang ~]# sed /^root/c/tanghuiyang /tmp/passwd
/tanghuiyang
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

#匹配第一行
[root@tang ~]# sed 1p /tmp/passwd
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

#查找并替换
sed '模式匹配s/被替换的内容/替换以后的内容/' 文件
[root@yan ~]# sed 's#root#boot#' /etc/passwd
2 boot:x:0:0:root:/root:/bin/bash
3 bin:x:1:1:bin:/bin:/sbin/nologin
4 …………
56 s@@@ s### s!!! s///

在上述命令中,我们利用 "s###" 的替换功能,将 "root" 替换成了 "ROOT"
但是从结果上可以看出来,匹配到的所在行里,只有第一个 "root" 做出了改变,而后两个则不受影响……
#g:全局替换
[root@yan ~]# sed 's#root#ROOT#'g /etc/passwd
 ROOT:x:0:0:ROOT:/ROOT:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 ……
 ……
 #i: 忽略大小写
 [root@yan ~]# sed 's#root#!!!#'gi /etc/passwd
 ROOT:x:0:0:!!!:/!!!:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 
 #n:第n个被匹配到的...n表示数字
 [root@yan ~]# sed 's#root#@@@@@#1' /tmp/passwd (只匹配第一个,并作出修改,然后将指定数放在修饰符的位置使用)
 ROOT:x:0:0:@@@@@:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 
 [root@yan ~]# sed '1,2s#root#@@@@@#' /tmp/passwd (将1,2作为指定数,放在前面使用,但未添加全局)
 ROOT:x:0:0:@@@@@:/root:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin

 [root@yan ~]# sed '1,2s#root#@@@@@#'g /tmp/passwd (添加了全局后)
 ROOT:x:0:0:@@@@@:/@@@@@:/bin/bash
 bin:x:1:1:bin:/bin:/sbin/nologin
 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 
 #y:转换的命令,对应转换
 [root@yan tmp]# cat a.txt
 abc
 [root@yan tmp]# sed 'y/abc/ABC/' a.txt
 ABC
 [root@yan tmp]# sed 'y/abc/tanbowen/' a.txt
 sed:-e 表达式 #1,字s符 15:“y”命令的字符串长度不同
 
 [root@yan tmp]# sed 'y/abc/tan/' a.txt
 tan

s

Linux system basic commands

  • Shutdown:

1. halt (shut down immediately: simple shutdown command)

2. shutdown -h now (shut down immediately: root user uses - safe shutdown command)

3. Shutdown -h 10 will automatically shut down after 10 minutes. If the shutdown is set through the shutdown command, you can use the shutdown -c command to cancel the restart.

  • Restart:

1、 reboot

2. shutdown -r now to restart immediately (for root user)

3. Shutdown -r 10 will automatically restart after 10 minutes (for root users)

4. shutdown -r 20:35 Restart when the time is 20:35 (for root users). If the restart is set through the shutdown command, you can use the shutdown -c command to cancel the restart.

  • Modify hostname:

1. hostname hostname (temporarily modify the hostname)

2. hostnamectl set-hostname hostname (permanently modify the hostname)

  • List files:

ls

  • Move/switch position:

cd … returns to the previous directory

cd - Return to the previous directory

  • View current user:

1、 who

2、 whoami

  • Show current location:

pwd

Print the absolute path name of the current location

[root@localhost file01]# pwd
/tmp/dir/file01
  • View version:

1、uname -a

2、cat /proc/version

  • Check the IP address:

1、ip addr show

2、ip a

  • View (command) history:

history

  • Check the command type:

type

  • Clear screen:

1、clear

2、ctrl+L

File management

ls command

  • list

ls -a lists all files including hidden files

ls -l lists file details

ls -h lists files and their sizes

ls -d lists the files themselves

ls -i displays the inot number

[root@localhost tmp]# ls /etc
abrt                        gshadow                   printcap
adjtime                     gshadow-                  profile
aliases                     gss                       profile.d
aliases.db                  gssproxy                  protocols
alsa                        host.conf                 pulse
alternatives                hostname                  purple
anacrontab                  hosts                     python
asound.conf                 hosts.allow               qemu-ga
at.deny                     hosts.deny                qemu-kvm
[root@localhost tmp]# ls -d /etc
/etc
  • list recursion

[root@localhost tmp]# ls -R /opt
/opt:
rh  xx

/opt/rh:

/opt/xx:
oo

/opt/xx/oo:

mkdir command

  • Create <directory>

Example:

[root@localhost tmp]# mkdir dir01
[root@localhost tmp]# mkdir dir02 dir03
[root@localhost tmp]# ls
dir01  dir02  dir03
[root@localhost tmp]# mkdir dir{A..E}
[root@localhost tmp]# # mkdir dir{05,06,07}
[root@localhost tmp]# mkdir dir{,.bak}
[root@localhost tmp]# ls
dir  dir01  dir02  dir03  dirA  dirB  dir.bak  dirC  dirD  dirE

  • Create directories recursively

Format

mkdir -p /opt/xx/oo

[root@localhost etc]# ls /opt
rh
[root@localhost etc]# mkdir /opt/xx/oo
mkdir: cannot create directory ‘/opt/xx/oo’: No such file or directory
[root@localhost etc]# mkdir -p /opt/xx/oo
[root@localhost etc]# ls /opt
rh  xx
[root@localhost etc]# ls /opt/xx
oo
[root@localhost etc]# 

touch command

  • Create <empty file>

[root@localhost tmp]# touch test{01..04}
[root@localhost tmp]# ls
dir    dir02  dirA  dirC  dirE    test02  test04
dir01  dir03  dirB  dirD  test01  test03

cp command

  • Copy <directory/file> to <specified location>

Syntax : cp [options]… Source file… Directory
Common options :
 -r/-R Recursive copy (<directory> can be copied)
 -i For <existing target file>, first ask <Do you want to overwrite the old file? >
 -f Force copy (in cp operation, for <existing target file>, force overwrite without giving any instructions)
   Note: -f option cannot be ignored -i option
 -a Copy <all source attributes>

-p copies the file and keeps the original attributes unchanged

Alias ​​command :
 cp='cp -i'If
 you need to call the <original cp command>, the method is: \cp (temporarily cancel the alias)

[root@localhost tmp]# cp test01 dir/test01
[root@localhost tmp]# ls dir
file01  file1  file5  test01

mv command

  • Move <directory/file> to <specified location> (can be used to rename)

Syntax : mv [options]… Source file… Directory
Common options :
 -i For <existing target file>, first ask <Do you want to overwrite the old file? >
 -f Forced move (in the mv operation, for the <existing target file>, forced overwriting without any instructions)
    Note 1: If the <target directory> already exists, it cannot be moved.
    Note 2: The -f option can ignore the -i option
Alias ​​command :
 mv='mv -i'
 If you need to call the <original mv command>, the method is: \mv (temporarily cancel the alias)

★ 举例:
[root@localhost tmp]# mv dir{A..E} dir{02,03} dir01
[root@localhost tmp]# ls
dir  dir01  test01  test02  test03  test04
[root@localhost tmp]# ls dir01
dir  dir02  dir03  dirA  dirB  dirC  dirD  dirE
[root@localhost tmp]# ls
dir  dir01  test01  test02  test03  test04
[root@localhost tmp]# ls dir
file01  file1  file5  test01
[root@localhost tmp]# mv test{01..04} dir01
[root@localhost tmp]# ls
dir  dir01
  • You can also <rename>

  • ★ 举例:
    ### 原地改名:文件/目录 ###
    cd /tmp
    rm -rf *
    touch myfile01
    mv myfile01 myfile02
    mkdir dir01
    mv dir01 dir02
    ### 移动改名:文件/目录 ###
    cd /tmp
    rm -rf *
    touch myfile01
    mv myfile01 /root/myfile02
    mkdir dir01
    mv dir01 /root/dir02
    

rmdir command

  • Delete <empty directory>

Syntax : rmdir [options]… directory…

★ 举例:
cd /tmp
mkdir dir01 dir02 dir03
mkdir dir{
    
    04,05,06}
mkdir dir{
    
    A..E}
rmdir dir01 dir02 dir03
rmdir dir{
    
    04,05,06}
rmdir dir{
    
    A..E}

rm command

  • Delete <directory/file>

Syntax : rm [options]… file…
Common options :
 -i Before deleting each time, it will ask <Do you want to delete? >
 -f Forced deletion (<directory> can be deleted)
    Note 1: The -f option can ignore the -i option
 -r/-R recursive deletion
Alias ​​command :
 rm='rm -i'
 If you need to call the <original rm command> , the method is: \rm (temporarily cancel the alias)

★ 举例:
cd /tmp
touch myfile03 myfile04 myfile05
rm /tmp/myfile03                 <---- 删除<指定文件>,由于<别名的定义>,每次删除之前,会先询问<是否删除?>
rm -f /tmp/myfile04 myfile05     <---- 强制删除<指定文件>
############################################
mkdir dir01 dir02 dir03
rm -rf /tmp/{
    
    dir01,dir02,dir03}  <---- 强制删除<指定目录>
############################################
rm -rf *                         <---- 强制删除<当前目录>中的<所有文件和目录>

rename command

  • (Batch change <string> in <name>, limited function)

Syntax: rename [options] expression replacement file...
Explanation: expression represents <matching content that needs to be changed>, <wildcard> and <regular expression> cannot be used.
    replacement represents <result content that needs to be changed>
    file... represents <file that needs to be renamed>
Commonly used options :
 -v display <detailed process>

★ 举例:
cd /tmp
rm -rf *
touch myfile0001 myfile0002 myfile0003
mkdir dir01 dir02 dir03
rename "dir" "DIR" dir*    <---- 针对<dir开头的文件名或目录名>,统一将<dir字符串>更改为<DIR字符串>
rename "000" "0" myfile*   <---- 针对<myfile开头的文件名或目录名>,统一将<000字符串>更改为<0字符>

View Files

(ls/cat/tac/head/tail/more/less)

ls command: List the contents contained in <specified directory> (already mentioned, omitted)

cat command: display the contents of <file>

Options:

cat -n filename to view the file and display line numbers

The tac command displays the contents of <file> in reverse order

head command: display the content at the beginning of <file>

tail command: display the content at the end of <file>

tail -n 5... file name to view the last 5 rows of the file

cat -f... file name to dynamically view files

more command: split-screen display, simple function, can only scroll down

less command: split screen display, scroll up and down, search for <keyword>

PgUp page up

PaDn page down

/+String highlight n scroll from top to bottom

​ N turn from bottom to top

file type

  • How to distinguish directories and files in Linux systems

    Blue indicates directories (folders in Windows systems)

    White indicates file

    Light blue indicates linked files (similar to shortcuts in Windows systems)

    Green indicates executable files (such as scripts, command program files)

    Red indicates compressed files

    Yellow indicates device files (hardware, keyboard, mouse, network card, and CPU hardware devices all exist in the form of files)

    Red flashing file --> indicates the file is unavailable

file properties file type
- Regular files (including: text files, binary files)
d directory file
b block device is: <block device file>, such as: <hard disk> supports random access in <block unit>
c Character device is: <character device file>, such as: <keyboard> supports linear access in <character unit>
l symbolic link is: <symbolic link file>, also known as <soft link file>
p pipe is: <named pipe file>
s socket is: <socket file>, used to implement communication between <two processes>
[root@localhost tmp]# ls -l
total 0
drwxr-xr-x. 2 root root 6 414 04:30 dir
drwxr-xr-x. 2 root root 6 414 04:27 dir01
drwxr-xr-x. 2 root root 6 414 04:28 dir02
drwxr-xr-x. 2 root root 6 414 04:28 dir03
drwxr-xr-x. 2 root root 6 414 04:28 dirA
drwxr-xr-x. 2 root root 6 414 04:28 dirB
drwxr-xr-x. 2 root root 6 414 04:30 dir.bak
drwxr-xr-x. 2 root root 6 414 04:28 dirC
drwxr-xr-x. 2 root root 6 414 04:28 dirD
drwxr-xr-x. 2 root root 6 414 04:28 dirE

d rwx r-x r-x. 2 root root 6 414 04:30 dir
  u    g   o    所属者 所属组
d :文件类型(目录文件)
u : 所属者
g : 所属组
o : 其他人
权限
r (4)读
w (2)写
x (1)执行



Soft connection and hard connection

  • Soft connection

  • Features: Soft connections can span partitions and can link directories. After the source file is deleted, the connection file will not be available.

  • 命令格式:ln -s 源文件路径 目标路径 (相当于windows创建快捷方式)

  • 注意:创建连接时一定要写目录或文件的绝对路径 哪怕是在当前路径下 也要写绝对路径

    [root@localhost ~]# ls
    anaconda-ks.cfg  Documents  initial-setup-ks.cfg              Music     Public     Videos
    Desktop          Downloads  linuxqq_2.0.0-b1-1024_x86_64.rpm  Pictures  Templates
    [root@localhost ~]# ln -s /Music /tmp
    [root@localhost ~]# ls /tmp
    dir  dir01  Music  vmware-root
    
  • 硬连接

  • 特点:硬连接不可以跨分区,不可以对目录进行链接,删除文件后,链接文件任然可用

  • 命令格式:ln 源文件目录 目标路径

[root@localhost tmp]# ln /tmp/dir01/test01 /tmp  //创建硬连接
[root@localhost tmp]# ls
dir  dir01  Music  test01  vmware-root
[root@localhost tmp]# cat test01     //链接无内容
[root@localhost tmp]# echo qwe > /tmp/dir01/test01   //在源文件中写入内容
[root@localhost tmp]# cat /tmp/test01    //链接文件内容随着更新
qwe
#源文件和链接文件inot号保持一致
[root@localhost tmp]# ls -i test01
17649549 test01
[root@localhost tmp]# ls -i dir01/test01
17649549 dir01/test01
  • 提示:硬连接的文件可以实现同步更新,并保持属性不变

用户和组

创建用户:useradd

格式:
	useradd	[选项]	用户名
-u:指定uid
[root@node1 ~]# useradd -u 1010 user3
[root@node1 ~]# grep user3 /etc/passwd
user3:x:1010:1010::/home/user3:/bin/bash
	
-c:指定描述信息
[root@node1 ~]# useradd -c user4 user4
[root@node1 ~]# tail  -n 2 /etc/passwd
user3:x:1010:1010::/home/user3:/bin/bash
user4:x:1011:1011:user4:/home/user4:/bin/bash

-d:指定家目录
[root@node1 ~]# useradd -d /test1 user5
[root@node1 ~]# tail  -n 2 /etc/passwd

-s:指定shell
[root@node1 ~]# useradd -s /sbin/nologin user6
[root@node1 ~]# tail  -n 2 /etc/passwd
user5:x:1012:1012::/test1:/bin/bash
user6:x:1013:1013::/home/user6:/sbin/nologin

[root@node1 ~]# su - user6
This account is currently not available.

-g:将用户加入到主组
-G:将用户加入到从属组
[root@node1 ~]# useradd -g group1 -G group2 user7
[root@node1 ~]# id user7
uid=1014(user7) gid=1014(group1) groups=1014(group1),1015(group2)

[root@node1 ~]# echo "redhat"  | passwd --stdin user7
Changing password for user user7.
passwd: all authentication tokens updated successfully.

练习

1.创建用户 fred,用户ID为1111,密码为fred1111

2.
创建组sysadm
创建用户harry,natasha,tom
要求harry,natasha的附加组为sysadm
要求tom用户的登陆shell为非交互式shell
三个用户的密码为redhat

修改用户:usermod

[root@node1 ~]# tail -n 2 /etc/passwd
harry:x:3535:3536::/home/harry:/bin/bash
sarah:x:3536:3537::/home/sarah:/sbin/nologin
[root@node1 ~]# usermod -u 5050 -c testsarsh -d /test2 -s /bin/bash sarah
[root@node1 ~]# tail -n 2 /etc/passwd
harry:x:3535:3536::/home/harry:/bin/bash
sarah:x:5050:3537:testsarsh:/test2:/bin/bash

删除用户:userdel

-r 
[root@node1 ~]# userdel user1
[root@node1 ~]# id user1
id: ‘user1’: no such user
[root@node1 ~]# cd /var/spool/mail/
[root@node1 mail]# ls
devops  manalo   remoteuser1  sarah    user1  user3  user5  user7
harry   natasha  rpc          student  user2  user4  user6  wallah
[root@node1 mail]# userdel -r user2
[root@node1 mail]# id user2
id: ‘user2’: no such user
[root@node1 mail]# ls
devops  manalo   remoteuser1  sarah    user1  user4  user6  wallah
harry   natasha  rpc          student  user3  user5  user7
[root@node1 mail]# 

创建组:groupadd

命令格式:
	groupadd [选项] 组名
	-g
[root@node1 ~]# groupadd -g 5055 group4
[root@node1 ~]# tail -2 /etc/group
group3:x:3538:
group4:x:5055:

修改组:groupmod

命令格式:
	groupadd [选项] 组名
	-g :gid
[root@node1 ~]# groupmod -g 6000 group4
[root@node1 ~]# tail -2 /etc/group
group3:x:3538:
group4:x:6000:

	
	-n : 修改组名
	[root@node1 ~]# groupmod -n testgroup4 group4
[root@node1 ~]# tail -2 /etc/group
group3:x:3538:
testgroup4:x:6000:

删除组:groupdel

[root@node1 ~]# groupdel group3

gpasswd:将用户加组

-a 将用户加入到组
-d 将组里的用户删除
[root@node1 ~]# useradd user10
[root@node1 ~]# groupadd group10

[root@node1 ~]# gpasswd -a user10 group10
Adding user user10 to group group10
[root@node1 ~]# id user10
uid=5051(user10) gid=5051(user10) groups=5051(user10),6001(group10)

[root@node1 ~]# gpasswd -d user10 group10
Removing user user10 from group group10
[root@node1 ~]# id user10
uid=5051(user10) gid=5051(user10) groups=5051(user10)

身份切换

以某个用户的身份执行某个命令,使用命令su - username -c cmd

[root@node1 ~]# su - harry -c id
利用harry的身份查看id信息

sudo提权

[root@servera ~]# vim /etc/sudoers    (有高亮、只读)
[root@servera ~]# visudo  (没有高亮,不是只读)

99 ## Allow root to run any commands anywhere
100 root    ALL=(ALL)       ALL
root:用户名
ALL:主机名
ALL:角色
ALL:命令

案例1:让harry用户有root权限

[root@node1 ~]# visudo
	100 root    ALL=(ALL)       ALL
    101 harry   ALL=(ALL)       ALL
    
测试
[root@node1 ~]# su - harry
Last login: Sun Apr 24 11:26:21 EDT 2022 on pts/0
[harry@node1 ~]$ sudo useradd user100
[sudo] password for harry: 
[harry@node1 ~]$ id user100
uid=5052(user100) gid=5052(user100) groups=5052(user100)

案例2:让sysadm组sudo时不需要密码

[root@node1 ~]# visudo
 111 # %wheel        ALL=(ALL)       NOPASSWD: ALL 
 112  %sysmgrs       ALL=(ALL)       NOPASSWD: ALL
测试:
[root@node1 ~]# su - harry
Last login: Sun Apr 24 11:26:21 EDT 2022 on pts/0
[harry@node1 ~]$ sudo cat /var/log/messages

=5051(user10),6001(group10)

[root@node1 ~]# gpasswd -d user10 group10
Removing user user10 from group group10
[root@node1 ~]# id user10
uid=5051(user10) gid=5051(user10) groups=5051(user10)


## 身份切换

以某个用户的身份执行某个命令,使用命令su - username -c cmd

```bash
[root@node1 ~]# su - harry -c id
利用harry的身份查看id信息

sudo提权

[root@servera ~]# vim /etc/sudoers    (有高亮、只读)
[root@servera ~]# visudo  (没有高亮,不是只读)

99 ## Allow root to run any commands anywhere
100 root    ALL=(ALL)       ALL
root:用户名
ALL:主机名
ALL:角色
ALL:命令

案例1:让harry用户有root权限

[root@node1 ~]# visudo
	100 root    ALL=(ALL)       ALL
    101 harry   ALL=(ALL)       ALL
    
测试
[root@node1 ~]# su - harry
Last login: Sun Apr 24 11:26:21 EDT 2022 on pts/0
[harry@node1 ~]$ sudo useradd user100
[sudo] password for harry: 
[harry@node1 ~]$ id user100
uid=5052(user100) gid=5052(user100) groups=5052(user100)

案例2:让sysadm组sudo时不需要密码

[root@node1 ~]# visudo
 111 # %wheel        ALL=(ALL)       NOPASSWD: ALL 
 112  %sysmgrs       ALL=(ALL)       NOPASSWD: ALL
测试:
[root@node1 ~]# su - harry
Last login: Sun Apr 24 11:26:21 EDT 2022 on pts/0
[harry@node1 ~]$ sudo cat /var/log/messages

Guess you like

Origin blog.csdn.net/qq_62761962/article/details/126914984