Article directory
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"
(2) View the information of all users currently logged in the system.
who
(3) Check the release version information of the Linux system.
cat /etc/*-release
(4) Check what shells are currently available in the Linux system?
cat /etc/shells
(5) Install cshell in Linux system.
yum install csh
(6) View the list of users created in the current Linux system.
cat /etc/passwd
(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
(8) Use the command to modify the shell of user01 to bash.
chsh -s /bin/bash ypy
(9) Set a password for user user01.
passwd ypy
(10) Check the last line of the /etc/passwd file to check the record information of the new user.
tail -n 1 /etc/passwd
(11) Switch user01 to log in to the system and see if the login is successful.
su – ypy
(12) Modify the user ID of user user01 to 2000.
su – ypy
(13) Add user01 to the sudo additional group.
centOS is added to wheel
usermod -aG wheel ypy
(14) Delete user user01 and its home directory.
userdel -r ypy
(15) Check the IP address configuration of the current Linux system network port.
ifconfig
(16) View the default gateway information of the Linux system.
ip route show
(17) Test whether the communication between the Linux host and the gateway is normal.
-
Find the default gateway address
ip route show default
-
Test communication
ping -c 4 192.168.37.2
(18) Real-time and dynamic monitoring of system CPU, memory and other resource usage.
top
(19) Restart the Linux operating system in a friendly way.
reboot
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
(2) Display the full path of the current working directory.
pwd
(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
(4) Create the directory dir3 and set its permissions to 442.
mkdir dir3
chmod 442 dir3
(5) Create a link to the dir1 folder, and the link name must be customized.
ln -s dir1 ypy
(6) Count the number of all files in the home directory.
ls -l | grep "^-" | wc -l
(7) Write your own "major, class, name, student number" information in the file1 file.
vim file1
(8) Redirect the contents of the file1 file to the file2 file.
cat file1 > file2
(9) Copy the file3 file to the dir1 directory.
cp file3 dir1/
(10) Use the tar command to package and compress the dir1 directory, and customize the backup name.
tar -czvf backup_name.tar.gz dir1
(11) Move the file4 file to the dir2 directory.
mv file4 dir2/
(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
(13) Delete the dir1 directory.
rm -rf dir1
(14) Find all files with the suffix ".conf" in the root directory.
sudo find / -type f -name "*.conf"
(15) Use regular expressions to find all QQ email addresses in the test.txt file.
-
Create test.txt file
-
regular expression
grep -oP '[1-9][0-9]{4,}@qq\.com' test.txt
(16) View the history of user execution commands.
history
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.
- 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
-
view disk
lsblk
-
Create new partition
fdisk /dev/sbd
-
View partitions
fdisk -l /dev/sdb
-
Format new partition
mkfs.ext4 /dev/sdb1
-
Create mount point
mkdir /share
-
Mount new partition
mkdir /share mount /dev/sdb1 /share df -h
-
Set up automatic mounting
nano /etc/fstab
ctrl+0 ctrl+x save and exit
-
View mount results
mount -a df -h
2.2 jdk installation
- 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*
-
Install jdk
yum install -y java-1.8.0-openjdk.x86_64
-
Check whether the installation is successful
java -version
2.3 mysql installation
- 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/
-
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
-
Check mariadb and uninstall it if it exists
rpm -qa|grep mariadb rpm -e --nodeps mariadb-libs
-
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
-
Initialize and set permissions
mysqld --initialize --console chown -R mysql:mysql /var/lib/mysql/
-
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
-
test
show databases;
2.4 tomcat deployment
- 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
-
Unzip
tar -zxvf apache-tomcat-9.0.84.tar.gz
-
start up
./startup.sh #启动 ./shutdown.sh #停止
2.5 Online projects
- 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)
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
-
Enter the bin directory and start tomcat
Access address: http://localhost:8080/MyJavaWeb/
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 "--------------------------------------------------"
-
Add permissions
chmod +x monitor.sh
-
Configure task scheduling
crontab -e
-
View log
cat log.txt~