RHCSAday3-从命令行管理文件

一、创建文件命令练习:

(1) 在/目录下创建一个临时目录test;

[root@server ~]# mkdir /test
[root@server ~]# ll -d /test
drwxr-xr-x. 2 root root 6 Oct 21 11:07 /test

(2)在临时目录test下创建五个文件,文件名分别为passwd,group,bashrc,profile,sshd_config;

[root@server ~]# cd /test/
[root@server test]# touch {passwd,group,bashrc,profile,sshd_config}
[root@server test]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 21 11:09 bashrc
-rw-r--r--. 1 root root 0 Oct 21 11:09 group
-rw-r--r--. 1 root root 0 Oct 21 11:09 passwd
-rw-r--r--. 1 root root 0 Oct 21 11:09 profile
-rw-r--r--. 1 root root 0 Oct 21 11:09 sshd_config

(3)在/test创建/etc/motd的软链接,文件名为motd.soft;创建/etc/motd的硬链接为motd.hard

[root@server test]# ln -s /etc/motd motd.soft
[root@server test]# ln /etc/motd motd.hard
[root@server test]# ll -di motd.soft motd.hard 
17077280 -rw-r--r--. 2 root root 0 Sep 10  2018 motd.hard
 1546055 lrwxrwxrwx. 1 root root 9 Oct 21 11:18 motd.soft -> /etc/motd
[root@server test]# ll -di /etc/motd
17077280 -rw-r--r--. 2 root root 0 Sep 10  2018 /etc/motd

二、重定向练习:

(1)将系统内核版本信息,发行版本信息,写入到/test/motd.soft文件中

[root@server test]# cat /etc/redhat-release > /test/motd.soft 
[root@server test]# uname -a >> /test/motd.soft
[root@server test]# cat /test/motd.soft 
Red Hat Enterprise Linux release 8.5 (Ootpa)
Linux server.local 4.18.0-348.el8.x86_64 #1 SMP Mon Oct 4 12:17:22 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux

(2)将当前主机主机名,当前用户使用的shell信息追加到/test/motd.hard文件中

[root@server test]# hostname >> /test/motd.hard 
[root@server test]# echo $SHELL >> /test/motd.hard 
[root@server test]# cat /test/motd.hard 
Red Hat Enterprise Linux release 8.5 (Ootpa)
Linux server.local 4.18.0-348.el8.x86_64 #1 SMP Mon Oct 4 12:17:22 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux
server.local
/bin/bash

(3)将根目录下的文件的文件名写入/test/file文件中

[root@server test]# ls / > /test/file
[root@server test]# cat /test/file 
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var

(4)查看当前工作目录是否为/test目录,将当前工作目录的详细信息追加到/test/file文件中

[root@server test]# pwd
/test
[root@server test]# ll -d /test >> /test/file
[root@server test]# cat /test/file 
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
test
tmp
usr
var
drwxr-xr-x. 2 root root 127 Oct 21 11:33 /test

三、tee命令练习:

(1)将当前时间添加至/test目录下的passwd,group,bashrc,profile,sshd_config文件中

[root@server test]# date | tee  /test/{passwd,group,bashrc,profile,sshd_config}
Fri Oct 21 11:48:01 CST 2022
[root@server test]# cat /test/passwd 
Fri Oct 21 11:48:01 CST 2022
[root@server test]# cat /test/group 
Fri Oct 21 11:48:01 CST 2022
[root@server test]# cat /test/bashrc 
Fri Oct 21 11:48:01 CST 2022
[root@server test]# cat /test/profile 
Fri Oct 21 11:48:01 CST 2022
[root@server test]# cat /test/sshd_config 
Fri Oct 21 11:48:01 CST 2022

(2)将当前用户的用户名追加至/test目录下的passwd,group,bashrc,profile,sshd_config文件中

[root@server test]# echo $USER | tee -a /test/{passwd,group,bashrc,profile,sshd_config}
root
[root@server test]# cat /test/passwd 
Fri Oct 21 11:48:01 CST 2022
root
[root@server test]# cat /test/group 
Fri Oct 21 11:48:01 CST 2022
root
[root@server test]# cat /test/bashrc 
Fri Oct 21 11:48:01 CST 2022
root
[root@server test]# cat /test/profile 
Fri Oct 21 11:48:01 CST 2022
root
[root@server test]# cat /test/sshd_config 
Fri Oct 21 11:48:01 CST 2022
root

四、vim命令练习:

(1)将/etc/passwd文件内容读入/test/passwd,并修改文件里的root字符为admin

[root@server test]# cat /etc/passwd >> /test/passwd 
[root@server test]# vim /test/passwd
打开文件进入末行模式 ':%s/root/admin/g'
修改完成后保存并退出 'ESC + :wq'
#查证
[root@server test]# cat /test/passwd 
Fri Oct 21 11:48:01 CST 2022
admin
admin:x:0:0:admin:/admin:/bin/bash
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/admin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
clevis:x:995:989:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:994:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:993:987:GlusterFS daemons:/run/gluster:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
chrony:x:992:986::/var/lib/chrony:/sbin/nologin
setroubleshoot:x:991:984::/var/lib/setroubleshoot:/sbin/nologin
saslauth:x:990:76:Saslauthd user:/run/saslauthd:/sbin/nologin
libstoragemgmt:x:989:983:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
dnsmasq:x:981:981:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sssd:x:980:980:User for sssd:/:/sbin/nologin
cockpit-ws:x:979:979:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:978:978:User for cockpit-ws instances:/nonexisting:/sbin/nologin
flatpak:x:977:977:User for flatpak system helper:/:/sbin/nologin
colord:x:976:976:User for colord:/var/lib/colord:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:975:975::/run/gnome-initial-setup/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
xue:x:1000:1000:xue:/home/xue:/bin/bash

(2)将/etc/group文件内容读入/test/group,只保留root开头的行内容

[root@server test]# cat /etc/group | grep ^root >> /test/group 
[root@server test]# cat /test/group
Fri Oct 21 11:48:01 CST 2022
root
root:x:0:

(3)将/root/.bashrc文件内容读入/test/bashrc,删除#号开头的行内容 命令 常用选项 说明

[root@server test]# cat /root/.bashrc >> /test/bashrc 
[root@server test]# vim /test/bashrc
打开文件后进入末行模式:':g/^#/d'
修改完成后保存并退出 'ESC + :wq'
#查验
[root@server test]# cat /test/bashrc 
Fri Oct 21 11:48:01 CST 2022
root

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

(4)将/etc/ssh/sshd_config文件内容读入/test/sshd_config,在该文件的第17行后添加一行内容Port 22

[root@server test]# cat /etc/ssh/sshd_config >> /test/sshd_config
[root@server test]# vim /test/sshd_config
打开文件后进入末行模式:':17'
之后输入:'o'
写入数据:Port 22
保存并退出:'ESC + :wq'
#查验
[root@server test]# cat -n /test/sshd_config 
     1	Fri Oct 21 11:48:01 CST 2022
     2	root
     3	#	$OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $
     4	
     5	# This is the sshd server system-wide configuration file.  See
     6	# sshd_config(5) for more information.
     7	
     8	# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
     9	
    10	# The strategy used for options in the default sshd_config shipped with
    11	# OpenSSH is to specify options with their default value where
    12	# possible, but leave them commented.  Uncommented options override the
    13	# default value.
    14	
    15	# If you want to change the port on a SELinux system, you have to tell
    16	# SELinux about this change.
    17	# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
    18	Port 22
    19	#
    20	#Port 22
    21	#AddressFamily any
    22	#ListenAddress 0.0.0.0
    23	#ListenAddress ::

(5)将/test/sshd_config文件中的第40-50行的yes改为no

[root@server test]# vim /test/sshd_config
打开文件后进入末行模式:':40,50s/yes/no/g'
修改完成后保存并退出: 'ESC + :wq'
#查验
打开文件后进入末行模式:':set nu'

查看40-50行内容是否修改
在这里插入图片描述

(6)将/test/sshd_config文件另存为/test/sshd.conf

[root@server test]# vim /test/sshd_config
打开文件后进入末行模式:':w /test/sshd_conf'
保存并退出: 'ESC + :wq'
#查验
[root@server test]# ll /test/sshd.conf -d
-rw-r--r--. 1 root root 4309 Oct 21 15:46 /test/sshd.conf

(7)将/test目录下的passwd,group,bashrc文件中的第一行内容复制至文档最后一行

[root@server test]# vim /test/passwd
打开文件后进入末行模式:':1 co $'
保存并退出: 'ESC + :wq'

passwd文件查验
在这里插入图片描述

[root@server test]# vim /test/group
打开文件后进入末行模式:':1 co $'
保存并退出: 'ESC + :wq'

group文件查验
在这里插入图片描述

[root@server test]# vim /test/bashrc
打开文件后进入末行模式:':1 co $'
保存并退出: 'ESC + :wq'

bashrc文件查验
在这里插入图片描述

(8)将/test目录下的profile,sshd_config文件中前两行内容复制至文档倒数第二行

[root@server test]# vim /test/profile
打开文件后进入末行模式:':1,2 co $-1'
保存并退出: 'ESC + :wq'

查验profile文件
在这里插入图片描述

[root@server test]# vim /test/sshd_config
打开文件后进入末行模式:':1,2 co $-1'
保存并退出: 'ESC + :wq'

查验sshd_config文件
在这里插入图片描述

五、总结

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值