RHCSA/Linux基础Day5

1.使用tar命令对文件进行打包压缩与解压缩:
        使用gzip方式对文件进行压缩,并指定压缩名为 tar_gzip.tar.gz

[root@RedHat ~]# tar cvfz tar_gzip.tar.gz test.txt
test.txt

        使用bzip2方式对文件夹进行压缩,并指定压缩名为 tar_bzip2.tar.bz2

[root@RedHat ~]# tar cvfj tar_bzip2.tar.bz2 test.txt
test.txt

        使用xz方式对文件进行压缩,并指定压缩名为 tar_xz.tar.xz

[root@RedHat ~]# tar cvfJ tar_xz.tar.xz test.txt
test.txt

        新建文件file1.txt,file2.txt,file3.txt
        对文件file1.txt和file2.txt,进行压缩(使用gzip方式),排除file3.txt(即不对file3进行压缩)
        并指定压缩名为tar_file.tar.gz

[root@RedHat ~]# tar cvfz tar_file.tar.gz --exclude=file3.txt  file1.txt file2.txt file3.txt
file1.txt
file2.txt

       新建文件file4.txt,将file4.txt添加到tar_file.tar.gz中
       查看压缩包tar_file.tar.gz有哪些文件及目录(不解压,只查看)

       解压tar_gzip.tar.gz到指定目录tar_test(没有这个目录就创建)

[root@RedHat ~]# tar xvf tar_gzip.tar.gz -C tar_test/
test.txt

       解压tar_xz.tar.xz

[root@RedHat ~]# tar xvf tar_xz.tar.xz 
test.txt


2.在Linux上的/root目录创建一个Linux.txt,在windows上创建windows.txt

C:\Users\32519>sftp [email protected]
The authenticity of host '192.168.179.130 (192.168.179.130)' can't be established.
ECDSA key fingerprint is SHA256:onQ/I8XVhoCPuOoxG0cr16ktiCjKAi3ZkeSFeFkOamE.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added '192.168.179.130' (ECDSA) to the list of known hosts.
[email protected]'s password:
Connected to 192.168.179.130.

        通过sftp的 get和put命令,将windows上的windows.txt推送到linux上

sftp> put windows.txt
Uploading windows.txt to /root/windows.txt
windows.txt                                                                           100%    0     0.0KB/s   00:00

        通过sftp的 get和put命令,将linux上的linux.txt推送到windows上

sftp> get Linux.txt
Fetching /root/Linux.txt to Linux.txt

        使用rz上传文件windows.txt到linux上

sftp> rz windows.txt
Invalid command.

        使用sz下载文件linux.txt到windows上

sftp> sz Linux.txt
Invalid command.

3.创建普通变量local_data=1并访问

[root@RedHat ~]# export local_data=1
[root@RedHat ~]# echo $local_data 
1

  创建环境变量ROOT_DATA=root, 只有root用户可以访问到

[root@RedHat ~]# vim .bashrc 
# .bashrc
  
#User specific aliases and functions

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

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


export ROOT_DATA=root
~                                                                                                    
~                                                                                                    
~                                                                                                    
~                                                                                                    
".bashrc" 15L, 199C                                                                15,21         All
[root@RedHat ~]# source .bash_profile 
[root@RedHat ~]# echo $ROOT_DATA
root
[rhcsa@RedHat ~]$ echo $ROOT_DATA

  创建环境变量USER_DATA=user, 只有普通用户可以访问到

[root@RedHat ~]# vim .bashrc 
# .bashrc
  
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

USER_DATA=user
~                                                                                                    
~                                                                                                    
                                                                                   17,14         All
[rhcsa@RedHat ~]$ source .bash_profile 
[rhcsa@RedHat ~]$ echo $USER_DATA
user
[root@RedHat ~]# echo $USER_DATA

  创建环境变量DATA=all, root用户和普通用户都可以访问到

[root@RedHat ~]# vim /etc/bashrc 
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
  fi

fi
# vim:ts=4:sw=4
export DATA=all
"/etc/bashrc" 99L, 3017C                                                           99,15         Bot
[root@RedHat ~]# source /etc/bashrc 
[root@RedHat ~]# echo $DATA
all
[root@RedHat ~]# su - rhcsa
[rhcsa@RedHat ~]$ echo $DATA
all

4.创建3个文件test1.txt, test2.txt, test3.txt

[root@RedHat ~]# touch test1.txt test2.txt test3.txt

  使用find查找test1.txt,test2.txt, test3.txt

[root@RedHat ~]# find test*.txt
test1.txt
test2.txt
test3.txt

  使用别名: 将上边命令命名为myfind
  取消别名

[root@RedHat ~]# alias find='myfind'
[root@RedHat ~]# alias find
alias find='myfind'
[root@RedHat ~]# unalias find
[root@RedHat ~]# alias find
-bash: alias: find: not found

5.查看最近使用的10条历史命令

[root@RedHat ~]# history 10
   49  alias find
   50  alias find=myfind 
   51  myfind test
   52  alias find
   53  alias find='myfind'
   54  alias find
   55  unalias find
   56  alias find
   57  history -19
   58  history 10

6.在一行上执行两个命令,打印123和从root切换到普通用户

[root@RedHat ~]# echo 123;su - rhcsa
123
[rhcsa@RedHat ~]$ 

7.通配符使用
  创建3个文件 file1, file2, file3
  1.* 去匹配3个文件

[root@RedHat ~]# ls -l file*
-rw-r--r--. 1 root root 0 Mar 26 18:12 file1
-rw-r--r--. 1 root root 0 Mar 26 18:14 file2
-rw-r--r--. 1 root root 0 Mar 26 18:17 file3

  2.? 匹配3个文件

[root@RedHat ~]# ls -l file?
-rw-r--r--. 1 root root 0 Mar 26 18:12 file1
-rw-r--r--. 1 root root 0 Mar 26 18:14 file2
-rw-r--r--. 1 root root 0 Mar 26 18:17 file3

  3.[]匹配file1和file3

[root@RedHat ~]# ls -l file[1,3]
-rw-r--r--. 1 root root 0 Mar 26 18:12 file1
-rw-r--r--. 1 root root 0 Mar 26 18:17 file3

  4.[^]匹配file2

[root@RedHat ~]# ls -l file[^1,3]
-rw-r--r--. 1 root root 0 Mar 26 18:14 file2

  5.[!]匹配file2

[root@RedHat ~]# ls -l file[!1,3]
-rw-r--r--. 1 root root 0 Mar 26 18:14 file2

  6.{}匹配file1和file3

[root@RedHat ~]# ls -l file{1,3}
-rw-r--r--. 1 root root 0 Mar 26 18:12 file1
-rw-r--r--. 1 root root 0 Mar 26 18:17 file3

8.引号的使用举例: 无引号,单引号,双引号,反引号,$()

[root@RedHat ~]# echo $DATA
all
[root@RedHat ~]# a=$DATA
[root@RedHat ~]# echo $a
all
[root@RedHat ~]# a='$DATA'
[root@RedHat ~]# echo $a
$DATA
[root@RedHat ~]# a="$DATA"
[root@RedHat ~]# echo $a
all
[root@RedHat ~]# pwd
/root
[root@RedHat ~]# a=`pwd`
[root@RedHat ~]# echo $a
/root
[root@RedHat ~]# a=$(pwd)
[root@RedHat ~]# echo $a
/root

9.linux中用户的类型

(1)超级用户——用户名为root,它具有一切权限,只有进行系统维护(例如:建立用户等)或其他必要情形下 才用超级用户登录,以避免系统出现安全问题。

(2)系统用户(伪用户)——是Linux系统正常工作所必需的内建的用户。 主要是为了满足相应的系统进程对文件属主的要求而建立的,例如:bin、daemon、adm、lp等用户。 系统用户不能用来登录.

(3)普通用户——是为了让使用者能够使用Linux系统资源而建立的,我们的大多数用户属于此类。

  linux中用户组的类型

(1)基本组(私有组):建立账户时,若没有指定账户所属的组,系统会建立一个和用户名相同的组,这个组就是基本组,基本组只容纳一个用户。当把其他用户加入到该组中,则基本组就变成了附加组。
(2)附加组(公有组):可以容纳多个用户,组中的用户都具有组所拥有的权利。
(3)系统组:一般加入一些系统用户。

  linux中存储用户信息的文件是哪个?且其中的字段是什么意思

1.用户账号文件——/etc/passwd
passwd 是一个文本文件,用于定义系统的用户账号,由于所有用户都对passwd有读权限,所以该文件
中只定义用户账号,而不保存口令。
passwd文件中:
每行定义一个用户账号
字段 含义
登录名 登录名
加密口令
使用SHA-512/SHA-256/MD5算法加密后的密码,若为空,表示该用户无需密码即可登录,
若为“*”表示该账号不能用于登录系统,若为“!!”表示该账号密码已被锁定
最后一次修改时间
最近一次更改密码的日期,以距离1970年1月1日的天数表示
最小时间间隔 密码在多少天内不能被修改。默认值为0,表示不限制
最大时间间隔 密码在多少天后必须被修改。默认值为99999,表示不进行限制
警告时间
提前多少天警告用户密码将过期,默认值为7天,0表示不提供警告不活动时间 密码过期多少天后禁用此用户
失效时间
密码失效日期,以距离1970年1月1日的天数表示,默认为空,表示永久可用标志 保留未用,以便以后发展之用
每行由7个字段组成,字段之间用“:”分隔,其格式如下:
账号名称:密码:UID:GID:个人资料:主目录:Shell
字段说明:
账号名称:用户登录Linux系统时使用的名称。
密码:以前是以加密格式保存密码的位置,现在密码保存在/etc/shadow文件中,此处只是密码占位符“x”
或“*”。若为“x”,说明密码经过了shadow的保护
UID:用户的标识,是一个数值,用它来区分不同的用户
GID:用户所在基本组的标识,是一个数值,用它来区分不同的组,相同的组具有相同的GID。
个人资料:可以记录用户的完整姓名、地址、办公室电话、家庭电话等个人信息。
主目录:类似Windows 的个人目录,通常是/home/username,这里username是用户名,用户执行
“cd~”命令时当前目录会切换到个人主目录。
Shell:定义用户登录后激活的Shell,默认是Bash Shell
2.用户密码文件——/etc/shadow
每行定义了一个用户信息,行中各字段用“:”隔开,其格式如下:
登录名:加密口令:最后一次修改时间:最小时间间隔:最大时间间隔:警告时间:不活动时间:失效时间:标志
为提高安全性,用户真实的密码采用MD5加密算法加密后,保存在配置文件中。
只有root用户可以读取。

  linux中存储组信息的文件是哪个?且其中的字段是什么意思?

用户组账号文件——/etc/group
系统中的每一个文件都有一个用户和一个组的属主。使用“ls –l”命令可以看到每一个文件的属主和组。
系统中的每个组,在/etc/group文件中有一行记录
任何用户均可以读取用户组账户信息配置文件。
用户组的真实密码保存在/etc/gshadow配置文件中。
group文件字段说明:
字段          说明
Groupname   组的名字
Passwd      组的加密口令
GID         是系统区分不同组的ID,在/etc/passwd域中的GID字段是用这个数来指定用户的基本组
Userlist    是用“,”分开的用户名,列出的是附加组的成员。

10.创建下列用户、组和组成员资格:
        1.创建名为 sysmgrs 的组

[root@RedHat ~]# groupadd sysmgrs

        2.创建用户 natasha 同时指定sysmgrs作为natasha的附加组

[root@RedHat ~]# useradd natasha -G sysmgrs 

        3.创建用户 harry 同时指定 sysmgrs作为harry的附加组

[root@RedHat ~]# useradd harry -G sysmgrs 

        4.创建用户 sarah 指定shell类型为/sbin/false(无权访问系统上的交互式 shell)且不是 sysmgrs 的成员

[root@RedHat /]# useradd sarah -s /sbin/false

        5.设置natasha 、 harry 和 sarah 的密码都是 123

[root@RedHat ~]# passwd natasha 
Changing password for user natasha.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@RedHat ~]# passwd harry 
Changing password for user harry.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@RedHat ~]# passwd sarah 
Changing password for user sarah.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

        6.创建用户lockuser, 并指定家目录为/home/lock, 然后锁定该用户

[root@RedHat ~]# useradd lockuser -d /home/lock |passwd -l lockuser 

        7.创建用户limituser, gid为1555,userid为1666, 让其密码在10天后过期

[root@RedHat ~]# groupadd test -g 1555
[root@RedHat ~]# useradd limituser -g 1555 -u 1666 -f 10
[root@RedHat ~]# tail -1 /etc/passwd
limituser:x:1666:1555::/home/limituser:/bin/bash

        8.解锁lockuser, 并设定下次登录时必须修改密码

[root@RedHat ~]# passwd -u lockuser|passwd -e lockuser 

        9.让natasha具备修改 harry密码的权限(sudo)
          visudo
          Host_Alias RHCSA=lwz
          User_Alias USER11=natasha
          Cmnd_Alias CHPASS=/usr/bin/passwd harry
          USER RCHSA=(root)  CHPASS

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
## 
## This file must be edited with the 'visudo' command.

## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using
## wildcards for entire domains) or IP addresses instead.
# Host_Alias     FILESERVERS = fs1, fs2
# Host_Alias     MAILSERVERS = smtp, smtp2
  Host_Alias RHCSA=lwz
## User Aliases
## These aren't often necessary, as you can use regular groups
## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
## rather than USERALIAS
# User_Alias ADMINS = jsmith, mikem
  User_Alias USER11=natasha

## Command Aliases
## These are groups of related commands...
   Cmnd_Alias CHPASS=/usr/bin/passwd harry
## Networking
# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

## Installation and management of software
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

## Services
# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

## Updating the locate database
# Cmnd_Alias LOCATE = /usr/bin/updatedb

## Storage
# Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount

## Delegating permissions
# Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp

## Processes
# Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

## Drivers
# Cmnd_Alias DRIVERS = /sbin/modprobe

# Defaults specification

#
# Refuse to run if unable to disable echo on the tty.
#
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home
Defaults    match_group_by_gid

# Prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. Starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# We enable always_query_group_plugin to restore old behavior.
# Disable this option for new behavior.
Defaults    always_query_group_plugin

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
USER RHCSA=(root) CHPASS
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

## Allows members of the users group to mount and unmount the
## cdrom as root

# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

    10.创建用户testuser并设置密码,修改用户名为normaluser   

[root@RedHat ~]# useradd testuser
[root@RedHat ~]# passwd testuser 
[root@RedHat ~]# usermod -l normaluser testuser

    11.删除lockuse

[root@RedHat ~]# userdel -r lockuser 

你可能感兴趣的:(linux)