Java Spring Recruitment Interview Sprint Series: Complete Linux Interview Questions!

Study notes

  • The past and present of Zero-Copy

  • Clear the file sudo sh -c "echo''> a.txt"

  • Rename mv folderAName newFolderName

  • Create soft chain ln -s sourcefile targetfile

    ln -s /xxx/xx/eclipse eclipseLink

  • Use proxy to download files wget -e use_proxy=yes -e http_proxy=xxxx:1080

  • 设置ulimit sudo sh -c “ulimit -n 65535 && exec su userA”

Network articles

ssh

  • lsof -i:22
  • Install basic tools
  • sudo apt-get install openssh-server openssh-client

View all open ports

  • You can netstat -anpcheck which ports are open
    • -a show all
    • -n Do not display aliases, only display numbers
    • -p display process number and process name
    • netstat -natpl Display the listening port of tcp
    • netstat -naupl Display the listening port of udp

CentOS7 open ports: CentOS7 has used firewall as the firewall, and no longer uses iptables

  • root user
  • Open the firewall systemctl start firewalld.service
  • Open the port firewall-cmd --zone=public --add-port=50013/tcp --permanent
    • --Zone-public: table-scoped public
    • –Add-port=8080/tcp: The port for adding tcp protocol is 8080
    • --Permanent: permanent effect, without this parameter means temporary effect
  • Restart firewall systemctl restart firewalld.service
  • Reload configuration firewall-cmd --reload
  • firewall-cmd --list-ports

Test whether the domain name can be resolved

  • nslookup server
    • eg: nslookup www.baidu.com

Test if the port is open

  • telnet ip port

Linux mail server: Postfix

http://cn.linux.vbird.org/linux_server/0380mail.php

  • Linux output redirection
    linux 环境中支持输入输出重定向,用符号<和>来表示。
    0、1和2分别表示标准输入、标准输出和标准错误信息输出,
    将一个脚本的执行过程及执行结果打印到日志的常用命令:
    ./myscript.sh 2>&1 | tee mylog.log
    可以用来指定需要重定向的标准输入或输出,比如 2>a.txt 表示将错误信息输出到文件a.txt中。
    同时,还可以在这三个标准输入输出之间实现重定向,比如将错误信息重定向到标准输出,可以用 2>&1来实现。
    Linux下还有一个特殊的文件/dev/null,它就像一个无底洞,所有重定向到它的信息都会消失得无影无踪。这一点非常有用,当我们不需要回显程序的所有信息时,就可以将输出重定向到/dev/null。
    
  • View the environment variable env | grep -E'M2|MAVEN'

Shadowsocks server-side installation

  • It curl https://bootstrap.pypa.io/get-pip.py -o get-pip.pyis possible that the version of python is low when installing pip
  • python get-pip.py
  • pip install shadowsocks
  • Place shadowsocks
  • vi /etc/shadowsocks.json

{
“server”:“0.0.0.0”,
“server_port”:0,
“local_port”:1080,
“password”:“xxx”,
“timeout”:600,
“method”:“aes-256-cfb”
}

* 将shadowsocks加入系统服务
* vi /etc/systemd/system/shadowsocks.service
```xml
[Unit]
Description=Shadowsocks
[Service]
TimeoutStartSec=0
ExecStart=/usr/local/bin/ssserver -c /etc/shadowsocks.json
[Install]
WantedBy=multi-user.target
# 设置开机自启命令
systemctl enable shadowsocks

# 启动命令
systemctl start shadowsocks

#查看状态命令
systemctl status shadowsocks
  • Client
  • Create a sh script and add nohup sslocal -s remoteserverip -p remoteserverport -b 127.0.0.1 -l localserverport -k password -m aes-256-cfb >/dev/null 2>&1 &

nohup sslocal -s 3.14.3.65 -p 15432 -b 127.0.0.1 -l 1080 -k abc123_ -m aes-256-cfb >ssl.log 2>&1 &

Record real-time usage of server CPU and memory

#!/bin/bash
fileName=$1

echo "CPU%,MEM%,TIME" > $fileName
for (( i = 0; i < 3000; i++ )) do
    output=`top -b -n1 | grep "Cpu(s)" | awk '{print $2 ","}' | tr -d '\n' && free -m | grep 'Mem' | awk '{print $3/$2 * 100 ","}' | tr -d '\n' && date | awk '{print $4}'`>temp
    echo "$output" >> $fileName
    sleep 1
done
  • cat redis.conf | grep -v “#” | grep -v “^$” View the configuration file, remove all # and remove all spaces

It is forbidden to modify directories and internal files in Linux. I used this command to reduce the permissions of nginx, and put static files in the tmp directory, which can be accessed by nginx users.

If a single file chattr +i aaa.txt is
removed, then the chattr -i aaa.txt
directory and the file chattr -R +i ttt
can be protected by using chattr -R +i A (-R recursively modify the attributes of the directory and its contents) All the contents in the directory A.
Files with the'i' attribute cannot be modified, only the super user can set or clear this attribute.

Application of Linux command in tomcat

  • template

192.168.1.189 - - [03/May/2019:19:47:21 +0000] "GET /html/css/main.css?browserId=other HTTP/1.1" 200 19858

注意

  1. The configuration of access log, the format is not necessarily the same as the above log
  2. cut -f1 -d "" is to cut the string according to the space, -fn means the number
  • Top 20 IP addresses by traffic
cat access_log.2019-0* | cut -f1 -d " " | sort | uniq -c | sort -k 1 -n -r | head -20
  • Top 20 page URLs
cat access_log.2019-0* | cut -f7 -d " " | sort | uniq -c | sort -k 1 -n -r | head -20
  • View the most time-consuming page
cat access_log.2019-0* | sort -k 10 -n -r | head -20
  • Count the proportion of 4xx/5xx requests
total requests: wc -l access_log.2019-0*
404 requests: awk '$9=='404'' access_log.2019-0* | wc -l
500 requests: awk '$9=='500'' access_log.2019-0* | wc -l
  • Find the log containing a certain keyword
awk '/keywords/' access_log* | head -10
awk '/keywords/{print $5,$6}' access_log* | head -10 // 查找包含keywords的关键字并打印第5/6列

vim edit file

  • It is not in edit mode now, you need to enter the i key on the keyboard to enter
  • For example, to delete the sixth line, move the cursor to the sixth line to be deleted, and enter the lowercase d twice in a row, that is, dd

CentOS view real-time network bandwidth occupancy

  • Install iftop, yum install iftop
  • Check the network card ifconfig
  • iftop -i eth0

Keep the connection between server and client

Modify the ssh configuration file /etc/ssh/sshd_config, add or modify ClientAliveInterval to "ClientAliveInterval 60". This parameter means that every 1 minute, the server sends a message to the client to keep the connection. Remember to restart the ssh service after saving service sshd restart.

Hundred questions

How does 65535 come from?
Computers store data in binary. Generally, unsigned int is used to store positive integers.
In computers, each integer is represented by a 16-bit binary number, so the largest number is 16. A 1, that is, 11111111 11111111
converts the binary number 11111111 11111111 into a decimal number to be 65535

The difference between profile and bashrc under Linux

  1. /etc/profile is
    used to set system environment parameters, such as $PATH. The environment variables inside are effective for all users in the system.

  2. The file /etc/bashrc sets the system bash shell related things, which is effective for all users in the system. As long as the user runs the bash command, then the things in it will work.
  3. ~/.bash_profile is
    used to set some environment variables. The function is similar to /etc/profile, but this is set for users, that is, you set environment variables in /home/user1/.bash_profile, then This environment variable only takes effect for the user user1.
  4. The function of ~/.bashrc
    is similar to /etc/bashrc, but it is only for the user and does not take effect for other users.
    In addition, the variables (global) set in /etc/profile can act on any user, while the variables (local) set in ~/.bashrc, etc. can only inherit the variables in /etc/profile, they are "father and child" relationship.

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/114238331