{{tag>linux debian ssh "ssh notes"}} ======SSH Notes====== =====SSH Login Message===== ====Acronyms and Definitions==== *MOTD = Message of the Day ====Login Messages==== *''sudo vim /etc/issue.net'' Displays a message before the user begins login, I do not use this. *''sudo vim /etc/ssh/sshd_config'' ensure the following option is set ''Banner /etc/issue.net'' (you can actually used any path/file) *''sudo systemctl reload sshd'' needs to be performed after any change to the configuration file to activate the changes. *''sudo vim /etc/motd'' Displays the banner text after login. This usually has some predefined Linux distribution text ====Login Scripts==== ====Debian==== *''ls -la /etc/update-motd.d/'' to see the scripts that are run after the MOTD is displayed. The Debian script is very simple: #!/bin/sh uname -snrvm ====Ubuntu==== Ubuntu has multiple scripts some that I like and some that I do not like. This system status information script, ++++/etc/update-motd.d/50-landscape-sysinfo| #!/bin/sh # pam_motd does not carry the environment [ -f /etc/default/locale ] && . /etc/default/locale export LANG cores=$(grep -c ^processor /proc/cpuinfo 2>/dev/null) [ "$cores" -eq "0" ] && cores=1 threshold="${cores:-1}.0" if [ $(echo "`cut -f1 -d ' ' /proc/loadavg` < $threshold" | bc) -eq 1 ]; then echo echo -n " System information as of " /bin/date echo /usr/bin/landscape-sysinfo else echo echo " System information disabled due to load higher than $threshold" fi ++++ The main information script is written in python3, ++++/usr/bin/landscape-systeminfo| #!/usr/bin/python3 import sys, os try: if os.path.dirname(os.path.abspath(sys.argv[0])) == os.path.abspath("scripts"): sys.path.insert(0, "./") else: from landscape.lib.warning import hide_warnings hide_warnings() from twisted.internet import reactor from landscape.sysinfo.deployment import run except ImportError: # For some reasons the libraries are not importable for now. We are # probably during an upgrade procedure, so let's exit, expecting the # dependencies to be fixed at next run. sys.exit(2) if __name__ == "__main__": run(sys.argv[1:], reactor) *If twisted not installed, ''pip install twisted'' to install *if cpuinfo not installed, ''pip install py-cpuinfo'' to install ++++ ====Python Code==== ++++python3 agnostic code | #!/usr/bin/python3 import psutil import platform from datetime import datetime import cpuinfo import socket import uuid import re def get_size(bytes, suffix="B"): """ Scale bytes to its proper format e.g: 1253656 => '1.20MB' 1253656678 => '1.17GB' """ factor = 1024 for unit in ["", "K", "M", "G", "T", "P"]: if bytes < factor: return f"{bytes:.2f}{unit}{suffix}" bytes /= factor def System_information(): print("="*40, "System Information", "="*40) uname = platform.uname() print(f"System: {uname.system}") print(f"Node Name: {uname.node}") print(f"Release: {uname.release}") print(f"Version: {uname.version}") print(f"Machine: {uname.machine}") print(f"Processor: {uname.processor}") print(f"Processor: {cpuinfo.get_cpu_info()['brand_raw']}") print(f"Ip-Address: {socket.gethostbyname(socket.gethostname())}") print(f"Mac-Address: {':'.join(re.findall('..', '%012x' % uuid.getnode()))}") # Boot Time print("="*40, "Boot Time", "="*40) boot_time_timestamp = psutil.boot_time() bt = datetime.fromtimestamp(boot_time_timestamp) print(f"Boot Time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}") # print CPU information print("="*40, "CPU Info", "="*40) # number of cores print("Physical cores:", psutil.cpu_count(logical=False)) print("Total cores:", psutil.cpu_count(logical=True)) # CPU frequencies cpufreq = psutil.cpu_freq() print(f"Max Frequency: {cpufreq.max:.2f}Mhz") print(f"Min Frequency: {cpufreq.min:.2f}Mhz") print(f"Current Frequency: {cpufreq.current:.2f}Mhz") # CPU usage print("CPU Usage Per Core:") for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)): print(f"Core {i}: {percentage}%") print(f"Total CPU Usage: {psutil.cpu_percent()}%") # Memory Information print("="*40, "Memory Information", "="*40) # get the memory details svmem = psutil.virtual_memory() print(f"Total: {get_size(svmem.total)}") print(f"Available: {get_size(svmem.available)}") print(f"Used: {get_size(svmem.used)}") print(f"Percentage: {svmem.percent}%") print("="*20, "SWAP", "="*20) # get the swap memory details (if exists) swap = psutil.swap_memory() print(f"Total: {get_size(swap.total)}") print(f"Free: {get_size(swap.free)}") print(f"Used: {get_size(swap.used)}") print(f"Percentage: {swap.percent}%") # Disk Information print("="*40, "Disk Information", "="*40) print("Partitions and Usage:") # get all disk partitions partitions = psutil.disk_partitions() for partition in partitions: print(f"=== Device: {partition.device} ===") print(f" Mountpoint: {partition.mountpoint}") print(f" File system type: {partition.fstype}") try: partition_usage = psutil.disk_usage(partition.mountpoint) except PermissionError: # this can be catched due to the disk that # isn't ready continue print(f" Total Size: {get_size(partition_usage.total)}") print(f" Used: {get_size(partition_usage.used)}") print(f" Free: {get_size(partition_usage.free)}") print(f" Percentage: {partition_usage.percent}%") # get IO statistics since boot disk_io = psutil.disk_io_counters() print(f"Total read: {get_size(disk_io.read_bytes)}") print(f"Total write: {get_size(disk_io.write_bytes)}") ## Network information print("="*40, "Network Information", "="*40) ## get all network interfaces (virtual and physical) if_addrs = psutil.net_if_addrs() for interface_name, interface_addresses in if_addrs.items(): for address in interface_addresses: print(f"=== Interface: {interface_name} ===") if str(address.family) == 'AddressFamily.AF_INET': print(f" IP Address: {address.address}") print(f" Netmask: {address.netmask}") print(f" Broadcast IP: {address.broadcast}") elif str(address.family) == 'AddressFamily.AF_PACKET': print(f" MAC Address: {address.address}") print(f" Netmask: {address.netmask}") print(f" Broadcast MAC: {address.broadcast}") ##get IO statistics since boot net_io = psutil.net_io_counters() print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}") print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}") if __name__ == "__main__": System_information() ++++ ====Some Links:==== *[[How to manage SSH login message|https://www.simplified.guide/ssh/suppress-banner]] *[[HowTo: Set a Warning Message (Banner) in SSH|https://www.shellhacks.com/setup-warning-message-banner-ssh/]] *[[Protect SSH Logins with SSH & MOTD Banner Messages|https://www.tecmint.com/protect-ssh-logins-with-ssh-motd-banner-messages/]] <- home_server:home_server_setup:other_services:webmin|Prev ^ home_server:home_server_setup:other_services:index|Start page ^ home_server:home_server_setup:other_services:bash|Next ->