《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
返回指定网卡的信息
[root@master ~]# salt ‘p2’ network.interface ens160
p2:
|_
address:
192.168.129.250
broadcast:
192.168.129.255
label:
ens160
netmask:
255.255.255.0
network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt ‘p2’ network.interface_ip ens160
p2:
192.168.129.250
network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt ‘p2’ network.interfaces
p2:
ens160:
hwaddr:
00:0c:29:20:9b:da
inet:
|_
address:
192.168.129.250
broadcast:
192.168.129.255
label:
ens160
netmask:
255.255.255.0
inet6:
|_
address:
fe80::20c:29ff:fe20:9bda
prefixlen:
64
scope:
link
up:
True
lo:
hwaddr:
00:00:00:00:00:00
inet:
|_
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
address:
::1
prefixlen:
128
scope:
host
up:
True
network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1
的地址
[root@master ~]# salt ‘p2’ network.ip_addrs
p2:
- 192.168.129.250
network.netstat
返回所有打开的端口和状态
[root@master ~]# salt ‘p2’ network.netstat
p2:
|_
inode:
27799
local-address:
0.0.0.0:22
program:
986/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
inode:
0
local-address:
192.168.129.250:50110
program:
proto:
tcp
recv-q:
0
remote-address:
192.168.129.134:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
inode:
35095
local-address:
192.168.129.250:34736
program:
1529/python3.6
proto:
tcp
recv-q:
0
remote-address:
192.168.129.134:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
inode:
29767
local-address:
192.168.129.250:22
program:
1235/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.129.1:51847
send-q:
0
state:
ESTABLISHED
user:
0
|_
inode:
28197
local-address:
192.168.129.250:22
program:
1048/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.129.1:51829
send-q:
0
state:
ESTABLISHED
user:
0
|_
inode:
27801
local-address:
:::22
program:
986/sshd
proto:
tcp6
recv-q:
0
remote-address:
::😗
send-q:
0
state:
LISTEN
user:
0
network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt ‘p2’ network.ping baidu.com
p2:
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=64.1 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=88.1 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=43.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=51.9 ms
— baidu.com ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 7ms
rtt min/avg/max/mdev = 43.383/61.865/88.102/16.841 ms
network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt ‘p2’ network.reverse_ip 192.168.200.0
p2:
0.200.168.192.in-addr.arpa
service.available
判断指定的服务是否可用
//查看P2端的nginx服务状态
[root@p2 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2021-11-03 18:07:23 CST; 2s ago
Process: 139861 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 139859 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 139857 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 139862 (nginx)
Tasks: 3 (limit: 11300)
Memory: 5.8M
CGroup: /system.slice/nginx.service
├─139862 nginx: master process /usr/sbin/nginx
├─139863 nginx: worker process
└─139864 nginx: worker process
//在master端执行模块查看nginx状态
[root@master ~]# salt ‘p2’ service.available nginx
p2:
True
[root@p2 ~]# systemctl status httpd
Unit httpd.service could not be found.
[root@master ~]# salt ‘p2’ service.available httpd #举一个反面教材,凸显效果
p2:
False
service.get_all
获取所有正在运行的服务
[root@master ~]# salt ‘p2’ service.get_all
p2:
-
NetworkManager
-
NetworkManager-dispatcher
-
NetworkManager-wait-online
-
arp-ethers
-
auditd
-
autovt@
-
basic.target
-
blk-availability
-
bluetooth.target
-
console-getty
-
container-getty@
-
cpupower
-
crond
-
此处省略N行
service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt ‘p2’ service.disabled nginx
p2:
False #是False说明是开机自启
service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt ‘p2’ service.enabled nginx
p2:
True
service.disable
设置指定服务开机不自动启动
[root@master ~]# salt ‘p2’ service.disable nginx
p2:
True
[root@master ~]# salt ‘p2’ service.enabled nginx
p2:
False
service.enable
设置指定服务开机自动启动
[root@master ~]# salt ‘p2’ service.enable nginx
p2:
True
[root@master ~]# salt ‘p2’ service.enabled nginx
p2:
True
service.reload
重新加载指定服务
[root@master ~]# salt ‘p2’ service.reload nginx
p2:
True
service.stop
停止指定服务
[root@master ~]# salt ‘p2’ service.stop nginx
p2:
True
//查看效果
[root@p2 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2021-11-03 18:19:24 CST; 2s ago
Process: 159441 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 139862 (code=exited, status=0/SUCCESS)
- 省略N行
service.start
启动指定服务
[root@master ~]# salt ‘p2’ service.start nginx
p2:
True
//查看效果
[root@p2 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-11-03 18:20:33 CST; 19s ago
Process: 159441 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
Process: 164391 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 164389 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 164387 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 164392 (nginx)
- 省略N行
service.restart
重启指定服务
[root@master ~]# salt ‘p2’ service.restart nginx
p2:
True
service.status
查看指定服务的状态
[root@master ~]# salt ‘p2’ service.status nginx
p2:
True
pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils
,可以使用 cmd.run 进行远程安装
[root@master ~]# salt ‘p2’ pkg.download wget
p2:
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm #下载好的软件放在这里
//查看效果
[root@p2 ~]# ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt ‘p2’ pkg.file_list nginx
p2:
errors:
files:
-
/etc/logrotate.d/nginx
-
/etc/nginx/fastcgi.conf
-
/etc/nginx/fastcgi.conf.default
-
/etc/nginx/fastcgi_params
-
/etc/nginx/fastcgi_params.default
-
/etc/nginx/koi-utf
-
/etc/nginx/koi-win
-
/etc/nginx/mime.types
-
/etc/nginx/mime.types.default
-
/etc/nginx/nginx.conf
-
/etc/nginx/nginx.conf.default
-
/etc/nginx/scgi_params
-
/etc/nginx/scgi_params.default
-
/etc/nginx/uwsgi_params
-
/etc/nginx/uwsgi_params.default
-
/etc/nginx/win-utf
-
/usr/bin/nginx-upgrade
-
/usr/lib/.build-id
-
/usr/lib/.build-id/2d
-
/usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
-
/usr/lib/systemd/system/nginx.service
-
/usr/lib64/nginx/modules
-
/usr/sbin/nginx
-
/usr/share/doc/nginx
-
/usr/share/doc/nginx/CHANGES
-
/usr/share/doc/nginx/README
-
/usr/share/doc/nginx/README.dynamic
-
/usr/share/licenses/nginx
-
/usr/share/licenses/nginx/LICENSE
-
/usr/share/man/man3/nginx.3pm.gz
-
/usr/share/man/man8/nginx-upgrade.8.gz
-
/usr/share/man/man8/nginx.8.gz
-
/usr/share/nginx/html/404.html
-
/usr/share/nginx/html/50x.html
-
/usr/share/nginx/html/index.html
-
/usr/share/nginx/html/nginx-logo.png
-
/usr/share/nginx/html/poweredby.png
-
/usr/share/vim/vimfiles/ftdetect/nginx.vim
-
/usr/share/vim/vimfiles/indent/nginx.vim
-
/usr/share/vim/vimfiles/syntax/nginx.vim
-
/var/lib/nginx
-
/var/lib/nginx/tmp
-
/var/log/nginx
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt ‘p2’ pkg.file_list
p2:
errors:
files:
VALUE_TRIMMED
pkg.group_info
查看包组的信息
[root@master ~]# salt ‘p2’ pkg.group_info ‘Development Tools’
p2:
conditional:
default:
-
asciidoc
-
byacc
-
ctags
-
diffstat
-
elfutils-libelf-devel
-
git
-
intltool
-
jna
-
ltrace
-
patchutils
-
perl-Fedora-VSP
-
perl-Sys-Syslog
-
perl-generators
-
pesign
-
source-highlight
-
systemtap
-
valgrind
-
valgrind-devel
description:
A basic development environment.
group:
Development Tools
id:
None
mandatory:
-
autoconf
-
automake
-
binutils
-
bison
-
flex
-
gcc
-
gcc-c++
-
gdb
-
glibc-devel
-
libtool
-
make
-
pkgconf
-
pkgconf-m4
-
pkgconf-pkg-config
-
redhat-rpm-config
-
rpm-build
-
rpm-sign
-
strace
optional:
-
cmake
-
expect
-
rpmdevtools
-
rpmlint
type:
package group
pkg.group_list
列出系统中所有的包组
[root@master ~]# salt ‘p2’ pkg.group_list
p2:
available:
-
Conflicts BaseOS
-
Dial-up Networking Support
-
Hardware Monitoring Utilities
-
Hardware Support
-
Large Systems Performance
-
Legacy UNIX Compatibility
-
Python Web
-
Server product core
-
Windows File Server
-
Additional Development
-
Anaconda tools
-
Backup Client
-
Base
-
base-x
-
Conflicts AppStream
-
Container Management
-
Debugging Tools
-
Desktop Debugging and Performance Tools
-
Development Tools
-
.NET Core Development
-
File and Storage Server
-
Fonts
-
FTP Server
-
GNOME Applications
-
GNOME
-
Graphical Administration Tools
-
Graphics Creation Tools
-
Guest Agents
-
Guest Desktop Agents
-
Headless Management
-
Infiniband Support
-
Input Methods
-
Internet Applications
-
Internet Browser
-
Java Platform
-
Legacy X Window System Compatibility
-
Mail Server
-
Mainframe Access
-
Multimedia
-
Network File System Client
-
Network Servers
-
Networking Tools
-
Common NetworkManager submodules
-
Office Suite and Productivity
-
Atomic Host ostree support
-
Performance Tools
-
Platform Development
-
KVM platform specific packages
-
Hyper-v platform specific packages
-
Printing Client
-
Remote Desktop Clients
-
Remote Management for Linux
-
RPM Development Tools
-
Scientific Support
-
Security Tools
-
Smart Card Support
-
Standard
-
System Tools
-
TeX formatting system
-
Virtualization Client
-
Virtualization Hypervisor
-
Virtualization Platform
-
Virtualization Tools
-
Basic Web Server
-
Workstation product core
available environments:
-
Server with GUI
-
Server
-
Workstation
-
Custom Operating System
-
Virtualization Host
available languages:
installed:
-
Core
-
VMware platform specific packages
installed environments:
- Minimal Install
pkg.install
安装软件
[root@master ~]# salt ‘p2’ pkg.install wget
p2:
wget:
new:
1.19.5-10.el8
old:
pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt ‘p2’ pkg.list_downloaded
p2:
pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt ‘p2’ pkg.list_pkgs
p2:
NetworkManager:
1:1.22.8-4.el8
NetworkManager-libnm:
1:1.22.8-4.el8
NetworkManager-team:
1:1.22.8-4.el8
NetworkManager-tui:
1:1.22.8-4.el8
acl:
2.2.53-1.el8
audit:
3.0-0.17.20191104git1c2f876.el8
…此处省略N行
pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt ‘p2’ pkg.owner /usr/bin/ls
p2:
coreutils
[root@master ~]# salt ‘p2’ pkg.owner /etc/nginx/nginx.conf
p2:
nginx
pkg.remove
卸载指定软件
[root@master ~]# salt ‘p2’ cmd.run ‘rpm -qa|grep wget’
p2:
wget-1.19.5-10.el8.x86_64
[root@master ~]# salt ‘p2’ pkg.remove wget
p2:
wget:
new:
old:
1.19.5-10.el8
#若要卸载多个文件,中间需要用逗号隔开
pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt ‘p2’ pkg.upgrade name=nginx
p2:
#若想升级系统中所有的软件包则把 name 参数去掉即可
state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt ‘p2’ state.show_highstate
p2:
apache-install:
env:
base
sls:
web.apache.install
pkg:
|_
name:
httpd
- installed
|_
order:
10000
apache-service:
env:
base
sls:
web.apache.install
service:
|_
name:
httpd
|_
enable:
True
- running
|_
order:
10001
state.highstate
执行高级状态
[root@master ~]# salt ‘p2’ state.highstate web.apache.install
p2:
ID: apache-install
Function: pkg.installed
Name: httpd
Result: None
Comment: The following packages would be installed/updated: httpd
Started: 18:43:25.179256
Duration: 611.861 ms
Changes:
installed:
httpd:
new:
installed
old:
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd not present; if created in this state run, it would have been started
Started: 18:43:25.805518
Duration: 20.783 ms
Changes:
Summary for p2
Succeeded: 2 (unchanged=2, changed=1)
Failed: 0
Total states run: 2
Total run time: 632.644 ms
state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt ‘p2’ state.show_state_usage
p2:
base:
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.install
dev:
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
prod:
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
test:
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt ‘p2’ state.show_top
p2:
base:
- web.apache.install
state.top
执行指定的top file,而不是默认的
[root@master ~]# salt ‘p2’ state.top top.sls
p2:
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 21:23:44.821788
Duration: 1558.727 ms
Changes:
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 21:23:46.385995
Duration: 105.409 ms
Changes:
Summary for p2
Succeeded: 2
Failed: 0
Total states run: 2
Total run time: 1.664 s
state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt ‘p2’ state.show_sls web.apache.install
p2:
apache-install:
env:
base
sls:
web.apache.install
pkg:
|_
name:
httpd
- installed
|_
order:
10000
apache-service:
env:
base
sls:
web.apache.install
service:
|_
name:
httpd
|_
enable:
True
- running
|_
order:
10001
查看关于user模块的相关函数
[root@master ~]# salt ‘p2’ sys.list_functions user
p2:
-
user.add
-
user.chfullname
-
user.chgid
-
user.chgroups
-
user.chhome
-
user.chhomephone
-
user.chloginclass
-
user.chother
-
user.chroomnumber
-
user.chshell
-
user.chuid
-
user.chworkphone
-
user.delete
-
user.get_loginclass
-
user.getent
-
user.info
-
user.list_groups
-
user.list_users
-
user.primary_group
-
user.rename
user.add
在minion端上创建一个用户,用法:salt ‘*’ user.add name
[root@master ~]# salt ‘p2’ user.add ran
p2:
True
user.info
返回指定用户信息
[root@master ~]# salt ‘p2’ user.info ran
p2:
fullname:
gid:
1000
groups:
- ran
home:
/home/ran
homephone:
name:
ran
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1000
workphone:
user.getent
返回所有系统用户信息的列表
[root@master ~]# salt ‘p2’ user.getent
p2:
|_
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
0
workphone:
|_
fullname:
bin
gid:
1
groups:
- bin
home:
/bin
homephone:
name:
bin
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
1
workphone:
|_
fullname:
daemon
gid:
2
groups:
- daemon
home:
/sbin
homephone:
name:
daemon
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
2
workphone:
|_
fullname:
adm
gid:
4
groups:
- adm
home:
/var/adm
homephone:
name:
adm
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
3
workphone:
|_
fullname:
lp
gid:
7
groups:
- lp
home:
/var/spool/lpd
homephone:
name:
lp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
4
workphone:
|_
fullname:
sync
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
sync
other:
passwd:
x
roomnumber:
shell:
/bin/sync
uid:
5
workphone:
|_
fullname:
shutdown
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
shutdown
other:
passwd:
x
roomnumber:
shell:
/sbin/shutdown
uid:
6
workphone:
|_
fullname:
halt
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
halt
other:
passwd:
x
roomnumber:
shell:
/sbin/halt
uid:
7
workphone:
|_
fullname:
gid:
12
groups:
home:
/var/spool/mail
homephone:
name:
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
8
workphone:
|_
fullname:
operator
gid:
0
groups:
- root
home:
/root
homephone:
name:
operator
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
11
workphone:
|_
fullname:
games
gid:
100
groups:
- users
home:
/usr/games
homephone:
name:
games
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
12
workphone:
|_
fullname:
FTP User
gid:
50
groups:
- ftp
home:
/var/ftp
homephone:
name:
ftp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
14
workphone:
|_
fullname:
Kernel Overflow User
gid:
65534
groups:
- nobody
home:
/
homephone:
name:
nobody
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
65534
workphone:
|_
fullname:
System message bus
gid:
81
groups:
- dbus
home:
/
homephone:
name:
dbus
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
81
workphone:
|_
fullname:
systemd Core Dumper
gid:
997
groups:
- systemd-coredump
home:
/
homephone:
name:
systemd-coredump
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
999
workphone:
|_
fullname:
systemd Resolver
gid:
193
groups:
- systemd-resolve
home:
/
homephone:
name:
systemd-resolve
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
193
workphone:
|_
fullname:
Account used by the trousers package to sandbox the tcsd daemon
gid:
59
groups:
- tss
home:
/dev/null
homephone:
name:
tss
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
59
workphone:
|_
fullname:
User for polkitd
gid:
996
groups:
- polkitd
home:
/
homephone:
name:
polkitd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
998
workphone:
|_
fullname:
Unbound DNS resolver
gid:
995
groups:
- unbound
home:
/etc/unbound
homephone:
name:
unbound
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
997
workphone:
|_
fullname:
User for sssd
gid:
993
groups:
- sssd
home:
/
homephone:
name:
sssd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
996
workphone:
|_
fullname:
Privilege-separated SSH
gid:
74
groups:
- sshd
home:
/var/empty/sshd
homephone:
name:
sshd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
74
workphone:
|_
fullname:
Random Number Generator Daemon
gid:
992
groups:
- rngd
home:
/var/lib/rngd
homephone:
name:
rngd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
995
workphone:
|_
fullname:
Apache
gid:
48
groups:
- apache
home:
/usr/share/httpd
homephone:
name:
apache
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
48
workphone:
|_
fullname:
Nginx web server
gid:
991
groups:
- nginx
home:
/var/lib/nginx
homephone:
name:
nginx
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
994
workphone:
|_
fullname:
gid:
1000
groups:
- ran
home:
/home/ran
homephone:
name:
ran
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1000
workphone:
user.chgid
更改用户GID
[root@master ~]# salt ‘p2’ user.add hao
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘id hao’
p2:
uid=1001(hao) gid=1001(hao) groups=1001(hao)
[root@master ~]# salt ‘p2’ user.chgid hao 1000
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘id hao’
p2:
uid=1001(hao) gid=1000(ran) groups=1000(ran)
user.chuid
更改用户UID
[root@master ~]# salt ‘p2’ cmd.run ‘id ran’
p2:
uid=1000(ran) gid=1000(ran) groups=1000(ran)
[root@master ~]# salt ‘p2’ user.chuid ran 2000
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘id ran’
p2:
uid=2000(ran) gid=1000(ran) groups=1000(ran)
user.list_groups
列出指定用户所属组的列表
[root@master ~]# salt ‘p2’ user.list_groups ran
p2:
- ran
user.rename
修改指定用户的用户名
[root@master ~]# salt ‘p2’ cmd.run ‘id hao’
p2:
uid=1001(hao) gid=1000(ran) groups=1000(ran)
[root@master ~]# salt ‘p2’ user.rename hao hhr
p2:
False #虽然返回的是False但是操作是成功了
[root@master ~]# salt ‘p2’ cmd.run ‘id hao’
p2:
id: ‘hao’: no such user
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt ‘p2’ cmd.run ‘id hhr’
p2:
uid=1001(hhr) gid=1000(ran) groups=1000(ran)
user.delete
在minion端删除一个用户
[root@master ~]# salt ‘p2’ cmd.run ‘id hhr’
p2:
uid=1001(hhr) gid=1000(ran) groups=1000(ran)
[root@master ~]# salt ‘p2’ user.delete hhr
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘id hhr’
p2:
id: ‘hhr’: no such user
ERROR: Minions returned with non-zero exit code
salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt ‘p2’ cmd.run ‘ls /usr/src/’
p2:
debug
kernels
[root@master ~]# salt-cp ‘p2’ /etc/passwd /usr/src/
p2:
/usr/src/passwd:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls /usr/src’
p2:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp ‘p2’ /root/anaconda-ks.cfg /etc/group /usr/src
p2:
/usr/src/anaconda-ks.cfg:
True
/usr/src/group:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls /usr/src’
p2:
anaconda-ks.cfg
debug
group
kernels
passwd
//在传输脚本时,是不会把执行权限传输过去
[root@master ~]# touch test.sh
[root@master ~]# vim test.sh
[root@master ~]# cat test.sh
#!/bin/bash
jjyy
[root@master ~]# salt-cp ‘p2’ /root/test.sh /opt
p2:
/opt/test.sh:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt’
p2:
total 4
-rw-r–r–. 1 root root 17 Nov 4 14:23 test.sh
[root@master ~]# chmod +x test.sh
[root@master ~]# ll /root/test.sh
-rwxr-xr-x 1 root root 17 11月 4 14:22 /root/test.sh
[root@master ~]# salt-cp ‘p2’ /root/test.sh /opt
p2:
/opt/test.sh:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt’
p2:
total 4
-rw-r–r–. 1 root root 17 Nov 4 14:25 test.sh
file.access
检查指定路径是否存在
[root@master ~]# salt ‘p2’ cmd.run ‘ls /usr/src’
p2:
anaconda-ks.cfg
debug
group
jjyy
kernels
passwd
[root@master ~]# salt ‘p2’ file.access /usr/src/passwd f
p2:
True
[root@master ~]# salt ‘p2’ file.access /usr/src/abc f
p2:
False
检查指定文件的权限信息
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /usr/src/’
p2:
total 12
-rw-r–r–. 1 root root 1284 Nov 3 21:57 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r–r–. 1 root root 512 Nov 3 21:57 group
-rw-r–r–. 1 root root 0 Nov 3 21:56 jjyy
drwxr-xr-x. 2 root root 6 Aug 12 2018 kernels
-rw-r–r–. 1 root root 1159 Nov 3 21:52 passwd
[root@master ~]# salt ‘p2’ file.access /usr/src/passwd r #是否有读权限
p2:
True
[root@master ~]# salt ‘p2’ file.access /usr/src/passwd w #是否有写权限
p2:
True
[root@master ~]# salt ‘p2’ file.access /usr/src/passwd x #是否有执行权限
p2:
False
file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/hh’
p2:
-rw-r–r–. 1 root root 0 Nov 4 18:37 /root/hh
[root@master ~]# salt ‘p2’ file.append /root/hh “hello world” “jjyy runtt” #添加有空格的内容,用引号引起来。(几对引号就添加几行)
p2:
Wrote 2 lines to “/root/hh”
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/hh’
p2:
-rw-r–r–. 1 root root 23 Nov 4 18:39 /root/hh
[root@master ~]# salt ‘p2’ cmd.run ‘cat /root/hh’
p2:
hello world
jjyy runtt
file.basename
获取指定路径的基名
[root@master ~]# salt ‘p2’ file.basename ‘/root/test.sh/jjtt’
p2:
jjtt
file.dirname
获取指定路径的目录名
[root@master ~]# salt ‘p2’ file.dirname ‘/root/test.sh/jjtt’
p2:
/root/test.sh
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt ‘p2’ cmd.run ‘md5sum /etc/passwd’
p2:
f4167f25847ec833a58db8347bfe5129 /etc/passwd
[root@master ~]# salt ‘p2’ file.check_hash /etc/passwd f4167f25847ec833a58db8347bfe5129
p2:
True
file.chattr
修改指定文件的属性
| 属性 | 对文件的意义 | 对目录的意义 |
| — | :-- | :-- |
| a | 只允许在这个文件之后追加数据
不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件
而不允许删除任何文件 |
| i | 不允许对这个文件进行任何的修改
不能删除、更改、移动 | 任何的进程只能修改目录之下的文件
不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt ‘p2’ cmd.run ‘lsattr /root/’
p2:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/ss
-------------------- /root/hh
//添加属性
[root@master ~]# salt ‘p2’ cmd.run ‘lsattr /root/hh’
p2:
----ia-------------- /root/hh
给指定文件去除属性
[root@master ~]# salt ‘p2’ file.chattr /root/hh operator=remove attributes=ai
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘lsattr /root/hh’
p2:
-------------------- /root/hh
file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt ‘p2’ cmd.run ‘id ran’
p2:
uid=2000(ran) gid=1000(ran) groups=1000(ran)
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/’
p2:
total 4
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 0 Nov 3 22:14 ss
[root@master ~]# salt ‘p2’ file.chown /root/ss ran ran #属主、属组必须一致
p2:
None
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/’
p2:
total 4
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
file.copy
在远程主机上复制文件或目录
//拷贝文件
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root’
p2:
total 8
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 23 Nov 4 18:39 hh
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
[root@master ~]# salt ‘p2’ file.copy /root/hh /root/hhhh
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root’
p2:
total 12
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 23 Nov 4 18:39 hh
-rw-r–r–. 1 root root 23 Nov 4 18:52 hhhh
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt’
p2:
total 0
[root@master ~]# salt ‘p2’ file.copy /root/abc /opt/abc recurse=True
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt’
p2:
total 0
drwxr-xr-x. 2 root root 20 Nov 4 19:05 abc
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt/abc’
p2:
total 8
-rw-r–r–. 1 root root 23 Nov 4 18:39 hh
-rw-r–r–. 1 root root 1258 Nov 4 19:05 passwd
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/abc’
p2:
total 4
-rw-r–r–. 1 root root 1258 Nov 4 19:05 passwd
//拷贝目录
[root@master ~]# salt ‘p2’ file.copy /root/abc/ /opt/abc/ recurse=True remove_existing=True
p2:
True
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /opt/abc’
p2:
total 4
-rw-r–r–. 1 root root 1258 Nov 4 19:05 passwd
file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root’
p2:
total 8
drwxr-xr-x. 2 root root 20 Nov 4 19:05 abc
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 23 Nov 4 18:52 hhhh
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
[root@master ~]# salt ‘p2’ file.directory_exists /root/ss
p2:
False #返回False是因为ss是文件而非目录
[root@master ~]# salt ‘p2’ file.directory_exists /root/abc
p2:
True
file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt ‘p2’ cmd.run ‘du -sb /etc’
p2:
21430793 /etc
[root@master ~]# salt ‘p2’ file.diskusage /etc
p2:
21704143
file.file_exists
判断指定文件是否存在
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/’
p2:
total 8
drwxr-xr-x. 2 root root 20 Nov 4 19:05 abc
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 23 Nov 4 18:52 hhhh
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
[root@master ~]# salt ‘p2’ file.file_exists /root/ss
p2:
True
[root@master ~]# salt ‘p2’ file.file_exists /root/abc
p2:
False #返回False是因为abc是目录而非文件
file.find
类似 find 命令并返回符合指定条件的路径列表
The options include match criteria:
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [±]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions:
delete [= file-types] # default type = ‘f’
exec = command [arg …] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria:
maxdepth = maximum depth to transverse in path #深度指的是多少级
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
path-glob:
-
= match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following:
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit
:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[w] [d] [h] [m] [s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following:
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例:
//查找类型为普通文件,以.log结尾的,大小在10以上的文件
[root@master ~]# salt ‘p2’ file.find / type=f name=*.log size=+1m
//在var目录下查询修改后的时间在10天前,大小在10M以上的文件,打印路径,文件大小,修改后的时间
[root@master ~]# salt ‘p2’ file.find /var mtime=+10d size=+10m print=path,size,mtime
//在/var/log下查找以.任意数字的且30天前修改的,大于10m的文件,删除
[root@master ~]# salt ‘p2’ file.find /var/log name=*.[0-9] mtime=+30d size=+10m delete
file.get_gid
获取指定文件的gid
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/hhhh’
p2:
-rw-r–r–. 1 root root 23 Nov 4 18:52 /root/hhhh
[root@master ~]# salt ‘p2’ file.get_gid /root/hhhh
p2:
0
file.get_group
获取指定文件的组名
最后
最后,强调几点:
- 1. 一定要谨慎对待写在简历上的东西,一定要对简历上的东西非常熟悉。因为一般情况下,面试官都是会根据你的简历来问的; 能有一个上得了台面的项目也非常重要,这很可能是面试官会大量发问的地方,所以在面试之前好好回顾一下自己所做的项目;
- 2. 和面试官聊基础知识比如设计模式的使用、多线程的使用等等,可以结合具体的项目场景或者是自己在平时是如何使用的;
- 3. 注意自己开源的Github项目,面试官可能会挖你的Github项目提问;
我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!
以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目。
面试答案
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
ile.diskusage /etc
p2:
21704143
file.file_exists
判断指定文件是否存在
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/’
p2:
total 8
drwxr-xr-x. 2 root root 20 Nov 4 19:05 abc
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r–r–. 1 root root 23 Nov 4 18:52 hhhh
-rw-r–r–. 1 ran ran 0 Nov 3 22:14 ss
[root@master ~]# salt ‘p2’ file.file_exists /root/ss
p2:
True
[root@master ~]# salt ‘p2’ file.file_exists /root/abc
p2:
False #返回False是因为abc是目录而非文件
file.find
类似 find 命令并返回符合指定条件的路径列表
The options include match criteria:
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [±]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions:
delete [= file-types] # default type = ‘f’
exec = command [arg …] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria:
maxdepth = maximum depth to transverse in path #深度指的是多少级
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
path-glob:
-
= match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following:
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit
:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[w] [d] [h] [m] [s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following:
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例:
//查找类型为普通文件,以.log结尾的,大小在10以上的文件
[root@master ~]# salt ‘p2’ file.find / type=f name=*.log size=+1m
//在var目录下查询修改后的时间在10天前,大小在10M以上的文件,打印路径,文件大小,修改后的时间
[root@master ~]# salt ‘p2’ file.find /var mtime=+10d size=+10m print=path,size,mtime
//在/var/log下查找以.任意数字的且30天前修改的,大于10m的文件,删除
[root@master ~]# salt ‘p2’ file.find /var/log name=*.[0-9] mtime=+30d size=+10m delete
file.get_gid
获取指定文件的gid
[root@master ~]# salt ‘p2’ cmd.run ‘ls -l /root/hhhh’
p2:
-rw-r–r–. 1 root root 23 Nov 4 18:52 /root/hhhh
[root@master ~]# salt ‘p2’ file.get_gid /root/hhhh
p2:
0
file.get_group
获取指定文件的组名
最后
最后,强调几点:
- 1. 一定要谨慎对待写在简历上的东西,一定要对简历上的东西非常熟悉。因为一般情况下,面试官都是会根据你的简历来问的; 能有一个上得了台面的项目也非常重要,这很可能是面试官会大量发问的地方,所以在面试之前好好回顾一下自己所做的项目;
- 2. 和面试官聊基础知识比如设计模式的使用、多线程的使用等等,可以结合具体的项目场景或者是自己在平时是如何使用的;
- 3. 注意自己开源的Github项目,面试官可能会挖你的Github项目提问;
我个人觉得面试也像是一场全新的征程,失败和胜利都是平常之事。所以,劝各位不要因为面试失败而灰心、丧失斗志。也不要因为面试通过而沾沾自喜,等待你的将是更美好的未来,继续加油!
以上面试专题的答小编案整理成面试文档了,文档里有答案详解,以及其他一些大厂面试题目。
面试答案
[外链图片转存中…(img-0AtxGhwO-1714743935349)]
[外链图片转存中…(img-i0u6U0Qt-1714743935349)]
[外链图片转存中…(img-PGaom4XH-1714743935350)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!