Linux final comprehensive training (software development direction)

Linux operating system

1. Command design

1.1 Common operations of administrators

(1) Display the current system time in the format of "2023/12/17 23:59:59".

date +"%Y/%m/%d %H:%M:%S"

image-20231224014744772

(2) View the information of all users currently logged in the system.

who

image-20231224014751404

(3) Check the release version information of the Linux system.

cat /etc/*-release

image-20231224014756015

(4) Check what shells are currently available in the Linux system?

cat /etc/shells

image-20231224014800067

(5) Install cshell in Linux system.

yum install csh

image-20231224014828869

image-20231224014834052

(6) View the list of users created in the current Linux system.

cat /etc/passwd

image-20231224014843976

(7) Create a new user (use your own name, take user01 as an example here), set its shell to csh, and create its home directory /home/user01.

  • Create a home directory /home/ypy
useradd -m -d /home/ypy -s /bin/csh ypy

image-20231224014848782

(8) Use the command to modify the shell of user01 to bash.

chsh -s /bin/bash ypy

image-20231224014852776

(9) Set a password for user user01.

passwd ypy

image-20231224014856694

(10) Check the last line of the /etc/passwd file to check the record information of the new user.

tail -n 1 /etc/passwd

image-20231224014900400

(11) Switch user01 to log in to the system and see if the login is successful.

su – ypy

image-20231224014903856

(12) Modify the user ID of user user01 to 2000.

su – ypy

image-20231224014908625

image-20231224014911821

(13) Add user01 to the sudo additional group.

centOS is added to wheel

usermod -aG wheel ypy

image-20231224014916315

(14) Delete user user01 and its home directory.

userdel -r ypy

image-20231224014920272

(15) Check the IP address configuration of the current Linux system network port.

ifconfig

image-20231224014925477

(16) View the default gateway information of the Linux system.

ip route show

image-20231224014929562

(17) Test whether the communication between the Linux host and the gateway is normal.

  • Find the default gateway address

    ip route show default
    

    image-20231224014935736

  • Test communication

    ping -c 4 192.168.37.2
    

    image-20231224014939450

(18) Real-time and dynamic monitoring of system CPU, memory and other resource usage.

top

image-20231224014942897

(19) Restart the Linux operating system in a friendly way.

reboot

image-20231224014946876

1.2 Common operations for ordinary users

(1) Switch to a normal user and view the information of the currently logged in user.

whoami
w

image-20231224014953446

(2) Display the full path of the current working directory.

pwd

image-20231224014957424

(3) Under the user's home directory, create empty files file1, file2, file3, file4, and create directories dir1 and dir2.

cd ~
touch file1 file2 file3 file4
mkdir dir1 dir2

image-20231224015000826

(4) Create the directory dir3 and set its permissions to 442.

mkdir dir3
chmod 442 dir3	

image-20231224015004955

(5) Create a link to the dir1 folder, and the link name must be customized.

ln -s dir1 ypy

image-20231224015008692

(6) Count the number of all files in the home directory.

ls -l | grep "^-" | wc -l

image-20231224015011860

(7) Write your own "major, class, name, student number" information in the file1 file.

vim file1

image-20231224015014761

(8) Redirect the contents of the file1 file to the file2 file.

cat file1 > file2

image-20231224015025455

(9) Copy the file3 file to the dir1 directory.

cp file3 dir1/

image-20231224015028072

(10) Use the tar command to package and compress the dir1 directory, and customize the backup name.

tar -czvf backup_name.tar.gz dir1

image-20231224015031623

(11) Move the file4 file to the dir2 directory.

mv file4 dir2/

image-20231224015036314

(12) Change the permissions of the dir2 directory and all files in the directory so that the owner has full permissions, people in the same group have read and write permissions, and others only have execution permissions.

chmod -R 761 dir2

image-20231224015040939

(13) Delete the dir1 directory.

rm -rf dir1

image-20231224015043695

(14) Find all files with the suffix ".conf" in the root directory.

sudo find / -type f -name "*.conf"

image-20231224015048374

(15) Use regular expressions to find all QQ email addresses in the test.txt file.

  • Create test.txt file

    image-20231224015053800

  • regular expression

    grep -oP '[1-9][0-9]{4,}@qq\.com' test.txt
    

    image-20231224015110553

(16) View the history of user execution commands.

history

image-20231224015115903

2. Comprehensive application

2.1 Mount partition

For all operations here, it is recommended to take a snapshot. If there is a problem, go back directly to the snapshot.

image-20231224015306602

  1. Add a new 800M disk to your operating environment, divide a 256M partition based on the new disk, and make the partition into an available file system (mount point/share) that can be automatically mounted. Provide operation steps and relevant screenshots (including disk viewing, mount viewing, etc.).
  • Added 800m disk

image-20231224015418007

  • view disk

    lsblk
    

    image-20231224015457364

  • Create new partition

    fdisk /dev/sbd
    

    image-20231224015520036

  • View partitions

    fdisk -l /dev/sdb
    

    image-20231224015551072

  • Format new partition

    mkfs.ext4 /dev/sdb1
    

    image-20231224015651351

  • Create mount point

    mkdir /share
    
  • Mount new partition

    mkdir /share
    mount /dev/sdb1 /share
    df -h
    

    image-20231224015824005

  • Set up automatic mounting

    nano /etc/fstab
    

    image-20231224015843921

    ctrl+0 ctrl+x save and exit

  • View mount results

    mount -a
    df -h
    

    image-20231224015935348

2.2 jdk installation

  1. Download a Linux version of the JDK installation package from the Oracle official website (choose the version corresponding to your development environment), and deploy it to the Linux environment. The installation steps and test results are given.
  • Query the installed jdk version

    yum -y list java*
    

    image-20231224020355477

  • Install jdk

    yum install -y java-1.8.0-openjdk.x86_64
    

    image-20231224020417187

  • Check whether the installation is successful

    java -version
    

    image-20231224020441688

2.3 mysql installation

  1. Download a Linux version of the database installation package from the MySQL (or Oracle, etc.) database official website (choose the version corresponding to your development environment), and deploy it to the Linux environment. The installation steps and test results are given.
  • download mysql

    https://downloads.mysql.com/archives/community/

    image-20231224020628411

  • Move the file to the root user of the virtual machine and unzip it

    tar -xvf mysql-8.1.0-1.el7.x86_64.rpm-bundle.tar
    
    image-20231224020642966
  • Check mariadb and uninstall it if it exists

    rpm -qa|grep mariadb
    rpm -e --nodeps mariadb-libs
    

    image-20231224020705656

  • Install dependencies

    rpm -ivh mysql-community-common-8.1.0-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-plugins-8.1.0-1.el7.x86_64.rpm
    rpm -ivh mysql-community-libs-8.1.0-1.el7.x86_64.rpm
    rpm -ivh mysql-community-client-8.1.0-1.el7.x86_64.rpm
    rpm -ivh mysql-community-icu-data-files-8.1.0-1.el7.x86_64.rpm
    rpm -ivh mysql-community-server-8.1.0-1.el7.x86_64.rpm
    

    image-20231224020731766

  • Initialize and set permissions

    mysqld --initialize --console
    chown -R mysql:mysql /var/lib/mysql/
    

    image-20231224020749586

  • Start mysql and start automatically at boot

    systemctl start mysqld
    systemctl enable mysqld
    
  • Check the temporary password and change it to 123456

    cat /var/log/mysqld.log|grep localhost
    mysql -uroot -p
    

    image-20231224020815175

image-20231224020826454

  • test

    show databases;
    

    image-20231224020845839

2.4 tomcat deployment

  1. Download a Linux version of the tomcat deployment package from the Apache official website (choose the version corresponding to the software you develop), and deploy it to the Linux environment. It can run normally. The deployment steps and test results are given.
  • Download the compressed package and upload it to linux

    https://tomcat.apache.org/download-90.cgi

    image-20231224020951288

  • Unzip

    tar -zxvf apache-tomcat-9.0.84.tar.gz
    

    image-20231224021009503

  • start up

    ./startup.sh		#启动
    ./shutdown.sh	#停止  
    

    image-20231224021030264

image-20231224021035034

2.5 Online projects

  1. Deploy the web program you wrote into tomcat and enable normal access. The operation steps and test results are given.
  • Package the project in idea (clear first and then package)

    image-20231224021138932

    image-20231224021144122

If you encounter an error when packaging, such as severlet does not exist, you need to add dependencies.

pom.xml

      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
      </dependency>
  • Copy the packaged war package to the webapps directory in tomcat in linux

image-20231224021256846

image-20231224021302062

  • Enter the bin directory and start tomcat

    image-20231224021321081

    Access address: http://localhost:8080/MyJavaWeb/

    image-20231224021339633

3. Programming

Please design a shell script to periodically collect the percentage of Linux system CPU, memory, and disk overhead, as well as the CPU and memory overhead of the tomcat process. The required collection period is 1 minute. The collected shell script, periodic task configuration information and collected results are given.

  • Create shell script

    vim script.sh
    
    #!/bin/bash
    
    # 获取当前时间
    timestamp=$(date +"%Y-%m-%d %T")
    
    # 获取系统CPU使用率(取前5个核心的平均值)
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}' | awk '{sum+=$1} END {print sum/5}')
    
    # 获取系统内存使用率
    mem_usage=$(free | awk 'NR==2{printf "%.2f\n", $3*100/$2}')
    
    # 获取磁盘使用率
    disk_usage=$(df -h | awk '$NF=="/"{printf "%s\n", $5}')
    
    # 获取tomcat进程的PID
    tomcat_pid=$(pgrep -f "catalina")
    
    # 获取tomcat进程CPU使用率
    tomcat_cpu_usage=$(top -p $tomcat_pid -bn1 | grep $tomcat_pid | awk '{print $9}')
    
    # 获取tomcat进程内存使用率
    tomcat_mem_usage=$(top -p $tomcat_pid -bn1 | grep $tomcat_pid | awk '{print $10}')
    
    # 输出采集结果
    echo "时间: $timestamp"
    echo "系统CPU使用率: $cpu_usage%"
    echo "系统内存使用率: $mem_usage%"
    echo "磁盘使用率: $disk_usage"
    echo "Tomcat进程CPU使用率: $tomcat_cpu_usage%"
    echo "Tomcat进程内存使用率: $tomcat_mem_usage%"
    echo "--------------------------------------------------"
    
    

    image-20231224021424309

  • Add permissions

    chmod +x monitor.sh
    
  • Configure task scheduling

    crontab -e
    

    image-20231224021531969

  • View log

    cat log.txt~
    

    image-20231224021535951

    image-20231224021539754

Guess you like

Origin blog.csdn.net/weixin_46290752/article/details/135177680