Record SSH operation logs in Linux

Source: Internet
Author: User

When logging SSH operation logs in Linux and logging on to the management host using ssh, you can save commands and outputs for ssh operation to the specified log file to facilitate subsequent viewing of operation logs. When logging on to the host through SSH, run the following command: Java code ssh username @ server | tee-a logfile.txt where the tee command reads the standard input and outputs the content to the standard output and (multiple) at the same time) file. This operation is cumbersome every time you use SSH, and you may forget the append logging function. Use the following steps to enable the local ssh program to automatically add the log function: 1. First, create a folder for storing logs and grant the read/write permission to Java code $ mkdir ~ /Ssh_logs/$ sudo chmod-R 777 ~ /Ssh_logs/2. Change the original ssh program to another name and create an execution script. The script calls the original ssh program, I just added the Java code $ sudo mv/usr/bin/ssh/usr/bin/ssh_ori $ sudo vi/usr/bin/ssh to create ssh during the call. the file content is as follows: ---------------------------------------------------------------------#! /Bin/sh mkdir-p ~ /Ssh_logs IP = $ (echo $1 | grep-oP "((? :(?: 25 [0-5] | 2 [0-4] \ d | [01]? \ D? \ D) \.) {3 }(?: 25 [0-5] | 2 [0-4] \ d | [01]? \ D? \ D) ") LOGNAME =$ {IP }_$ (date +" % Y % m % d _ % T ") ssh_ori $ @ | tee-~ /Ssh_logs/$ {LOGNAME }. log --------------------------------------------------------------------- in the script, the access IP address is extracted using a regular expression, and the log file name is determined based on the IP address and the current timestamp) 3. Add the execution permission Java code $ sudo chmod a + x/usr/bin/ssh will write the operation and output to the corresponding log file when you log on to the host using ssh later, the log file format is [access IP_8-bit datetime _ hour/second. log example: each time you run [ssh 192.168.1.100-l user1] or [ssh user1@192.168.1.100] (host name is not supported) will generate a new log file according to the current time and access IP: [192.168.1.100 _ 201301__17: 36: 18.l Og]. All the operations and output of the ssh session are saved in the file to facilitate troubleshooting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.