马哥Linux——第二周作业

时间:2021-11-12 03:07:14

1、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中。

[root@centos7 ~]#who | tr [:lower:] [:upper:] > /tmp/who.out
[root@centos7 ~]#cat /tmp/who.out 
ROOT     :0           2019-06-03 15:45 (:0)
ROOT     PTS/0        2019-06-03 15:59 (192.168.30.1)

 

2、计算1+2+3+...+99+100的总和。

[root@centos7 ~]#echo {1..100} | tr ' ' + | bc
5050
[root@centos7 ~]#seq -s + 100 | bc
5050

 

3、常用的用户以及文件管理命令有哪些,并演示命令以及用法。

 用户管理命令:

修改用户密码

passwd

[root@centos7 ~]#echo '12345678' | passwd --stdin kkk
Changing password for user kkk.
passwd: all authentication tokens updated successfully.

创建用户

useradd

[root@centos7 ~]#useradd user001

修改用户信息

usermod

[root@centos7 ~]#usermod -g kkk -aG root -u 6666 user001

删除用户

userdel

[root@centos7 ~]#userdel -r user1

创建用户组

groupadd

[root@centos7 ~]#groupadd group001

修改用户组信息

groupmod

[root@centos7 ~]#groupmod -g 6666 -n group002 group001

删除用户组

groupdel

[root@centos7 ~]#groupdel group002

显示用户、组信息

getent

[root@centos7 ~]#getent passwd
root:x:0:0:root:/root:/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:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
chrony:x:995:993::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:994:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:992:987::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
kkk:x:1000:1000:kkk,IT,10086,10000:/home/kkk:/bin/bash
tomcat:x:1001:1004::/home/tomcat:/bin/bash
mysql:x:1002:1005::/home/mysql:/bin/bash
user2:x:1004:1008::/home/user2:/bin/bash
user3:x:1005:1009::/home/user3:/bin/bash
[root@centos7 ~]#getent group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
input:x:999:
systemd-journal:x:190:
systemd-network:x:192:
dbus:x:81:
polkitd:x:998:
abrt:x:173:
libstoragemgmt:x:997:
rpc:x:32:
colord:x:996:
cgred:x:995:
dip:x:40:
ssh_keys:x:994:
saslauth:x:76:
rtkit:x:172:
chrony:x:993:
kvm:x:36:qemu
qemu:x:107:
tss:x:59:
libvirt:x:992:
usbmuxd:x:113:
geoclue:x:991:
rpcuser:x:29:
nfsnobody:x:65534:
radvd:x:75:
setroubleshoot:x:990:
pulse-access:x:989:
pulse-rt:x:988:
pulse:x:171:
gdm:x:42:
gnome-initial-setup:x:987:
sshd:x:74:
avahi:x:70:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
ntp:x:38:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
kkk:x:1000:kkk
webs:x:1001:
apps:x:1002:tomcat
dbs:x:1003:mysql
tomcat:x:1004:
mysql:x:1005:
user001:x:1006:
user2:x:1008:
user3:x:1009:
user1group:x:1010:
[root@centos7 ~]#getent shadow
root:$6$fhGPsICHwt401JgQ$5X5y3oDmIBzerlIbNeRFjnXs01bgbVTMLa4oRDQjP8z5fJKDHAGSL7IBjjqB5c9BifVSHXhwlnoVD./0fLhok1::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
systemd-network:!!:18033::::::
dbus:!!:18033::::::
polkitd:!!:18033::::::
abrt:!!:18033::::::
libstoragemgmt:!!:18033::::::
rpc:!!:18033:0:99999:7:::
colord:!!:18033::::::
saslauth:!!:18033::::::
rtkit:!!:18033::::::
chrony:!!:18033::::::
qemu:!!:18033::::::
tss:!!:18033::::::
usbmuxd:!!:18033::::::
geoclue:!!:18033::::::
rpcuser:!!:18033::::::
nfsnobody:!!:18033::::::
radvd:!!:18033::::::
setroubleshoot:!!:18033::::::
pulse:!!:18033::::::
gdm:!!:18033::::::
gnome-initial-setup:!!:18033::::::
sshd:!!:18033::::::
avahi:!!:18033::::::
postfix:!!:18033::::::
ntp:!!:18033::::::
tcpdump:!!:18033::::::
kkk:$6$BMwLhCs1$yuv9kNhqstfVbNx10wZg9sV72pds1I2utcPDRDxgzzGjzfV/eJjVBeduqF8n.3J4flhrXZ.9QR/GLn7NBZ0/o0:18051:0:99999:7:::
tomcat:!!:18051:0:99999:7:::
mysql:!!:18051:0:99999:7:::
user2:!!:18051:0:99999:7:::
user3:!!:18051:0:99999:7:::
[root@centos7 ~]#getent gshadow
root:::
bin:::
daemon:::
sys:::
adm:::
tty:::
disk:::
lp:::
mem:::
kmem:::
wheel:::
cdrom:::
mail:::postfix
man:::
dialout:::
floppy:::
games:::
tape:::
video:::
ftp:::
lock:::
audio:::
nobody:::
users:::
utmp:!::
utempter:!::
input:!::
systemd-journal:!::
systemd-network:!::
dbus:!::
polkitd:!::
abrt:!::
libstoragemgmt:!::
rpc:!::
colord:!::
cgred:!::
dip:!::
ssh_keys:!::
saslauth:!::
rtkit:!::
chrony:!::
kvm:!::qemu
qemu:!::
tss:!::
libvirt:!::
usbmuxd:!::
geoclue:!::
rpcuser:!::
nfsnobody:!::
radvd:!::
setroubleshoot:!::
pulse-access:!::
pulse-rt:!::
pulse:!::
gdm:!::
gnome-initial-setup:!::
sshd:!::
avahi:!::
slocate:!::
postdrop:!::
postfix:!::
ntp:!::
stapusr:!::
stapsys:!::
stapdev:!::
tcpdump:!::
kkk:$6$NpCZ5/9u878/$yzrgz8nItDezMkPawxAIKV.40SQPLPUuV21yr0w.MKU32AhMiFc6uVZMqVNj31dTP31IseNtBufAC5AMedQG7/::kkk
webs:!::
apps:!::tomcat
dbs:!::mysql
tomcat:!::
mysql:!::
user001:!::
user2:!::
user3:!::
user1group:!::

查看用户id信息

id

[root@centos7 ~]#id kkk
uid=1000(kkk) gid=1000(kkk) groups=1000(kkk)

切换用户

su

[root@centos7 ~]#su - kkk
Last login: Tue Jun  4 22:47:51 CST 2019 on pts/0

文件管理命令

编辑文本文件内容

nano

[root@centos7 ~]#nano f1

查看目录文件

ls

[root@centos7 ~]#ls -a
.   anaconda-ks.cfg  .cache   dead.letter  Documents  .esd_auth      initial-setup-ks.cfg  Music     Public     Videos
..  .bash_history    .config  Desktop      Downloads  .ICEauthority  .local                Pictures  Templates

查看文件属性列表

ll、ls -l

[root@centos7 ~]#ll
total 12
-rw-------. 1 root root 1894 May 17 08:35 anaconda-ks.cfg
-rw-------. 1 root root   14 Jun  1 15:33 dead.letter
drwxr-xr-x. 2 root root    6 May 17 08:44 Desktop
drwxr-xr-x. 2 root root    6 May 17 08:44 Documents
drwxr-xr-x. 2 root root    6 May 17 08:44 Downloads
-rw-r--r--. 1 root root 1925 May 17 08:43 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 May 17 08:44 Music
drwxr-xr-x. 2 root root    6 May 17 08:44 Pictures
drwxr-xr-x. 2 root root    6 May 17 08:44 Public
drwxr-xr-x. 2 root root    6 May 17 08:44 Templates
drwxr-xr-x. 2 root root    6 May 17 08:44 Videos

修改文件所有者、所有组

chown

[root@centos7 ~]#chown kkk:kkk /data/f1

查看文件三个时间

stat

[root@centos7 ~]#stat /data/f1 
  File: ‘/data/f1’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 803h/2051d      Inode: 67          Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/     kkk)   Gid: ( 1000/     kkk)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2019-06-05 01:52:08.860744447 +0800
Modify: 2019-06-05 01:52:08.860744447 +0800
Change: 2019-06-05 01:52:57.333748189 +0800
 Birth: -

复制

cp

[root@centos7 ~]#cp -a /data/f1 .

移动或重命名文件

mv

[root@centos7 ~]#mv /data/f1 /data/f2

删除

rm

[root@centos7 ~]#rm -f /data/f1
[root@centos7 ~]#rm -rf /data/dir1/

创建空目录

mkdir

[root@centos7 ~]#mkdir /data/dir1

创建空文件

>>、touch、>

[root@centos7 ~]#>> /data/f2
[root@centos7 ~]#touch /data/f1
[root@centos7 ~]#> /data/f3

查看文本文件内容

cat

[root@centos7 ~]#cat /data/f1 
test

修改文件所有组

chgrp

[root@centos7 ~]#chgrp kkk /data/f1

修改文件权限

chmod

[root@centos7 ~]#chmod 777 /data/f1

设置文件ACL

setfacl

[root@centos7 ~]#setfacl -m u:user2:r /data/f1

查看文件ACL

[root@centos7 ~]#getfacl /data/f1
getfacl: Removing leading '/' from absolute path names
# file: data/f1
# owner: root
# group: kkk
user::rwx
user:user2:r--
group::rwx
mask::rwx
other::rwx

 查看或修改文件保留权限

umask

[root@centos7 ~]#umask
0022
[root@centos7 ~]#umask 0024
[root@centos7 ~]#umask -p
umask 0024
[root@centos7 ~]#umask -S
u=rwx,g=rx,o=wx

判断文件类型

file

[root@centos7 ~]#file /usr/bin/cat
/usr/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=fac04659ab9a437b5384c09f4731023373821a39, stripped
[root@centos7 ~]#file /data/f1 
/data/f1: ASCII text

创建链接

ln

[root@centos7 ~]#ln -s /data/f1 /root/f1_link

查看软链接对应的源文件路径

readlink

[root@centos7 ~]#readlink f1_link 
/data/f1

 

4、创建用户gentoo,附加组为bin和root,默认shell为/bin/csh,注释信息为“Gentoo Distribution”。

[root@centos7 ~]#useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoo
[root@centos7 ~]#getent passwd gentoo
gentoo:x:6673:6675:Gentoo Distribution:/home/gentoo:/bin/csh
[root@centos7 ~]#id gentoo
uid=6673(gentoo) gid=6675(gentoo) groups=6675(gentoo),0(root),1(bin)

 

5、在/testdir/dir里创建的新文件自动属于webs组,组apps的成员如:tomcat能对这些新文件有读写权限,组dbs的成员如:mysql只能对新文件有读权限,其他用户(不属于webs、apps、dbs)不能访问这个文件夹。

[root@centos7 ~]#groupadd webs
[root@centos7 ~]#groupadd apps
[root@centos7 ~]#groupadd dbs
[root@centos7 ~]#useradd tomcat -G apps
[root@centos7 ~]#useradd mysql -G dbs
[root@centos7 ~]#mkdir -p /testdir/dir
[root@centos7 ~]#chgrp webs /testdir/dir/
[root@centos7 ~]#chmod g+s /testdir/dir/
[root@centos7 ~]#ll -d /testdir/dir/
drwxr-sr-x. 2 root webs 6 Jun  5 01:28 /testdir/dir/
[root@centos7 ~]#setfacl -Rm g:apps:6 /testdir/dir/
[root@centos7 ~]#setfacl -Rm g:dbs:4 /testdir/dir/
[root@centos7 ~]#setfacl -Rm o::0 /testdir/dir/
[root@centos7 ~]#getfacl /testdir/dir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/dir/
# owner: root
# group: webs
# flags: -s-
user::rwx
group::r-x
group:apps:rw-
group:dbs:r--
mask::rwx
other::---

 

6、找出ifconfig中的ip地址。要求结果只显示本机的IPv4地址。

[root@centos6 ~]#ifconfig | grep 'inet addr' | tr -s ' ' : | cut -d : -f 4
192.168.1.120
192.168.30.101
127.0.0.1

 

7、查出用户UID最大值的用户名、UID及shell类型。

[root@centos6 ~]#cut -d : -f 1,3,7 /etc/passwd | sort -t : -k 2 -nr | head -n 1
nfsnobody:65534:/sbin/nologin

 

8、复制/etc/rc.d/init.d/functions文件至/tmp目录,替换/tmp/functions文件中的/etc/sysconfig/init为/var/log。

:%s@/etc/sysconfig/init@/var/log@g