Java Interview foundation - Linux articles

0. Daily Orders
  • cd directory path: Jump to the specified directory
  • mkdir directory name: New Directory
  • pwd: Get the current path
  • su: switch user
  • ll: Man view the current directory files and directories
  • rm -rf directory: recursive delete all the files in the specified directory
  • vi file: to modify the file
  • mv source folder name the new folder name: move the folder to a specific directory
  • grep: find desired information
  • cp -r source directory name of the new directory name: copy files in the specified directory to the new directory
  • touch File Name: Create a file
  • find -name filename: Find File
  • Owner to view the file: ls -ahl
  • chkconfig --add xxx: xxx script added to the boot entry inside
  • chkconfig --list: to see whether the addition of success
  • shutdown -h now: now designated immediately shut down;
  • shutdown +5 "System will shutdown after 5 minutes": Specifies off after 5 minutes, while sending a warning message to a user to sign
  • reboot: reboot
  • reboot -w: to be a reboot of the simulation (only the record does not really re-boot)

1. Help command: man, help, info
  • help xxx # built-in command displays help information;
    xxx --help # external commands display help information;
  • No man xxx # points within an external, more detailed than the content displayed Help;
  • info xxx # obtained content more detailed than man;

2. Check the active processes of command: ps, top, pidstat
  • ps for all processes within the display system
    ~ ps-elf View all processes;
    ~ ps -ef | grep java view java and all related processes;
  • top for viewing active processes, CPU and memory information, real-time display system resource consumption of each process,
    can the process be sorted by CPU, memory usage and execution time.
  • case pidstat used to monitor all or specified processes consume system resources, including CPU, memory, disk I / O, a thread switch, the number of threads and other data.
    pidstat -u / -r / -d -p process ID - explain: (- u CPU, -r memory, -d disk I / O

3. Check the log commonly used commands: tail, head, cat, more, less, sed
  • tail: the file dynamic monitoring
    tail -fn 200 test.log 200 real-time monitoring of log lines
    tail -n 10 test.log inquiry last 10 lines of the log
    all log after the tail -n +10 test.log 10 lines of inquiry
  • head -n 10 test.log view the first 10 lines of the log
    head -n -10 test.log see other log the last 10 lines other
  • cat -n test.log | grep "run" in the query contains run test.log row
    cat test.log display all the contents of test.log
  •  sed -n '/ 2019-11-17 16:17:20 /, / 2019-12-17 16: 17: 36 / p' test.log find the log of the specified date range
    sed -n '1,3p' test Showing .log line 1-3 log
  • more can show the percentage, Enter can down one line, to be the next space, q to exit view
    more -4 test.log each screen display 4 lines log
    more + / run test.log find the first occurrence from the log "run" character string line, and two lines from the start where the display output
    cat test.log | more -5 entirety test.log obtained in each of 5 lines and
    more 仅能向前移动,却不能向后移动
  • less test.log test1.log exploring multiple files
    ps -ef | less -N ps to view and process information simultaneously display line numbers by less paging
    ps aux | grep redis: View include process redis string
    pgrep redis -a: View including process redis string
  • View all history history
    history | grep xxx View history contains some instructions recorded
    history | more Page View Record
    history -c Clear all history
打开超大文件: 
    1. head -10000 slowquery.log > temp.log  将slowquery.log的前10000行日志写入到temp.log中
    2. tail -10000 slowquery.log > temp.log   将slowquery.log的后10000行日志写入到temp.log中
    3. sed -n '10,10000p' slowquery.log > temp.log 把slowquery.log第10到10000行的数据写入到temp.log中
    4. cat catalina.log | grep   '2017-09-06 15:15:42' > test.log  根据查询条件导出

4. archive command
  • tar -zxvf xxxx.gz: extract the files in the current directory
    tar -zxvf xxxx.gz -C / usr / local unzip the file to a specified location
  • tar -zcvf abc.gz a.txt b.txt c.txt these three files compressed into abc.gz archive
    tar -zcvf abc.gz / abc / compressed into the specified directory archive

5. Kill the operation of a process
方法一:把ps的查询结果通过管道给grep查找包含特定字符串的进程, 管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。
例如: ps -ef | grep firefox 找到firefox进程的进程号
然后:kill -s 9 6496   杀死进程
方法二:  使用pgrep查询某个进程的进程号
例如:pgrep firefox    
然后:kill -s 9 6496
方法三: 使用pkill, pkill无须 “s”,终止信号等级直接跟在 “-“ 后面
例如:pkill -9 firefox

6. authority to order

Operating system, each file has a specific permission, one owner and group, Linux is generally divided into read permissions (readable), write (writable) and execution (excutable), divided into three groups. Corresponding to the file owner (owner), is a group (group) and others (other), by ls -l command we can see the permissions on the file or directory in a directory
types of files:

  • d: represents a directory
  • -: represents a file
  • l: represents the soft link (shortcut window can be considered to be in)

Linux permissions are divided into the following categories:

  • r: Representative permissions are read, may be represented by numeral 4
  • w: represents the permissions are writable, can be expressed as the number 2
  • x: represents the permissions are executable, you can use the number 1 indicates
    in terms of files and directories, perform read and write different meanings. For files:
Permission name Perform operations
r You can use cat to see the contents of the file
w You can modify the contents of the file
x You can run it as a binary file

For directories:

Permission name Perform operations
r You can view a list of directory
w You can create and delete files directory
x You can use cd into the directory

超级用户可以无视普通用户的权限,即使文件目录权限是000,依旧可以访问。 在linux中的每个用户必须属于一个组,不能独立于组外

修改文件/目录的权限的命令:chmod
示例:
修改/test下的aaa.txt的权限为属主有全部权限,属主所在的组有读写权限, 其他用户只有读的权限:
chmod u=rwx,g=rw,o=r aaa.txt
上述示例还可以使用数字表示:chmod 764 aaa.txt

See more here


7. Review the disk situation commands: free, vmstat, du, df

And a display system using free memory, the memory comprising a physical interaction region memory (the swap) and the kernel buffer memory

  • free:
    • [] -B Byte Displays in memory usage. 
    • [] -K in KB display memory usage.
    • [] -M in MB display memory usage.
    • [] -G in GB display memory usage. 
    • [] -O not display buffer adjustment column. 
    • [] -S <interval in seconds> memory usage is continuously observed.
    • [] -T display memory column sums. 
    • [] -V Display version information. 
      Example: free -s 10 every 10 seconds to check memory usage
  • cat / proc / meminfo free command is equivalent to
  • vmstat: dynamic understanding of the operation of the system resources
  • du: displays the amount of disk space use and file for each file and directory.
    Example: du -h tomcat8.5 / folder shows a display tomcat8.5 space each file, directory
  • df: displays the disk space that can be used on disk partition
    Example:
    df -a expressed view all file systems, the default unit KB
    df -h expressed in KB, MB, GB units to display, high readability
    more reference here

8. File Transfer command: ftp, scp, rsync, rz, sz
  • ftp: Ignore
  • scp:
    scp -p ./files/yum.log 192.168.214.187:/tmp/demo/: yum said it would copy the log files in the directory of the current system files to the machine's IP 192.168.214.18 the / tmp / directory under demo

  • sz:
    sz test.log: Download test.log log files to windows
  • : rz
    can upload files file upload, a pop-up box, select the file you want to upload, after determining
  • rsync:
    rsync incremental backup tool can be achieved. With Task Scheduler, rsync can achieve timing or synchronization interval, with inotify or sersync, can achieve real-time synchronization trigger. rsync can remotely copy of scp, cp local copy, rm to delete and "ls -l" to display a list of files and other functions


9. The network related commands: netstat, ifconfig, ss, tcpdump, traceroute, ping, nslookup, dig, nmap
  • ifconfig: View card information of the current system and ip addr command similar.
  • ping: View connection with a machine
  • netstat: for displaying various network-related information, such as network connection, routing tables, interface, Masquerade connection, multicast membership like.
    View current port use of the system: netstat -an
    list all ports: netstat -a
    list all tcp ports: netstat -at
    list all udp ports: netstat -au
    show only listening port: netstat -l
    just list all listening tcp port: netstat -lt
    only lists all the listening udp port: netstat -lu
    only lists all the listening port UNIX: netstat -lx
    displays statistics for all ports: netstat -s
    netstat will output every 2 seconds network information: netstat -t -c 2
    shows the core routing information: netstat -rn
    port to find running: netstat -apn | grep ssh
    identify processes running at the specified port: netstat -an | grep ': 22 '
    view the 6379 port occupancy: netstat - ntlp | grep 6379

  • ss: socket can be used to obtain statistical information can be displayed and similar content netstat, the advantage is that it can display more detailed information about TCP and connection status, and faster and more efficient than netstat.
    Example: ss -l local display all open ports
    ss -pl each process display specific open Socket
    SS Socket -t -a Show all TCP
    SS show all the -u -a Socekt the UDP
    SS State ESTABLISHED -o '(= dport : smtp or sport =: smtp) ' show all SMTP connection established
    ss -o state established' (dport = : http or sport =: http) ' show all HTTP connections established
    ss -x src /tmp/.X11 -unix / * find all the processes connected to X server
    ss -s lists detailed information about the current socket
ss 与 netstat对比:  
    ss命令是iproute工具集(CentOS 最小化安装带有);netstat命令是net-tools工具集;
    当服务器的socket连接数量变得非常大时,无论是使用netstat命令还是直接cat /proc/net/tcp,执行速度都会很慢;
    ss快的秘诀在于,它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块,可以获得Linux 内核中第一手的信息,这就确保了ss的快捷高效。
  • tcpdump: pouring a data transmission network, execute tcpdump command lists after the packet header file designated network interface
    Example: TCP packet information display tcpdump
    tcpdump -c 20: Displays the number of packets
    tcpdump -c 10 -q: Lite mode display 10 packets
    tcpdump -ddd: converted to decimal format
  • traceroute: know what path information from your host computer to the other end of the Internet is to take the
    example:
  1. traceroute -n -m 5 -q 4 -w 3 www.baidu.com
    Description: -n the IP address, the host name does not check, -m -q 4 hops provided each gateway 4 transmits data packets to the external -w probe packets waiting to be sent in response to time is set to 3 seconds
  2. traceroute -p 6888 www.baidu.com substantially probe packets using UDP port 6888 is provided
  3. traceroute -r www.baidu.com bypass the normal routing table, sending directly to a host connected to the network
  • nslookup: DNS records for the query, the query domain name resolution is working, used to diagnose network problems during network failure.
    Example: nslookup www.baidu.com using the machine default DNS to resolve Baidu address
    nslookup www.baidu.com 8.8.8.8 represents the specified DNS server to resolve Baidu address
    nslookup -d www.baidu.com represent time as long as the query, plus cache -d parameter, you can query domain name
  • nmap
    TCP port scan: -sT, -sP
    the TCP port scan the SYN: -sS
    nmap -p 20,21,53-110,30000 -sS 192.168.137.10 -255 --v
    the UDP port scan: the -sU
    the TCP scanning the ACK: - sA
  • dig command is mainly used to query from the DNS domain name server host address information
    query information for a single host dig baidu.com

注意点:
linux连接超时,处理方案:
1. 测试网络是否通:ping 远程IP
2. 如果能ping通则表示与服务器网络连接是正常,接下来测试端口:telnet 远程ip 端口
3.如果telnet不上,要考虑服务器是否有防火墙拦截:
  (1)CentOS7查看防火墙是否运行: firewall-cmd --state
  (2) CentOS6查看防火墙是否运行:/sbin/service iptables status
4. 如果防火墙被拦截,需要放行端口。如果防火墙没有拦截,则要考虑服务内部是否配置限定IP访问。MySQL、MongoDB、Redis都有bind选项,用来限定客户端IP
5. 再检查服务内部是否有其它权限设置,例如MySQL有远程访问配置,Redis有protected-mode改为no

面试题:Linux查看cpu占用率高的进程(阿里面试题)
面试题:查看占用某端口的进程(阿里面试题)
面试题:查看某进程监听的端口(阿里面试题)
面试题:查看系统负载(今日头条面试题)
面试题:查看进程中线程的状态(蘑菇街面试题)

第一题: 
可以使用以下命令查使用内存最多的K个进程:ps -aux | sort -k4nr | head -K
ps -aux中(a指代all——所有的进程,u指代userid——执行该进程的用户id,x指代显示所有程序,不以终端机来区分)
或者直接用命令:top
第一题:
可以使用以下命令查使用CPU最多的K个进程:ps -aux | sort -k3nr | head -K
第二题:
查看占用某端口的进程:
lsof -i:端口号
netstat -tunlp | grep 端口号
第三题:
查看某进程监听的端口:ps -aux | grep tomcat
第四题:
查看系统负载:
top命令查看linux负载:top  
输入top,会输出top - 01:18:39 up 2 days, 18:54, 1 user, load average: 0.04, 0.03, 0.05 
//01:18:39:系统当前时间 
//up 2 days, 18:54 :系统开机到现在经过了2天
//1 users:当前1用户在线 
//load average:0.04, 0.03, 0.05:系统1分钟、5分钟、15分钟的CPU负载信息. 
第五题:
查看进程中线程的状态:top -H
查指定进程的状态:top -H -p <pid>

Guess you like

Origin www.cnblogs.com/yzh-blog/p/11670289.html