################################################### ## ## Handy Commands - reminders ## ################################################### #### Favorite Find Commands # what files did I change in the last 15 minutes? find . -maxdepth 3 -mmin -15 -type f # remove all Thumbs.db find . -name Thumbs.db -exec rm {} \; # remove all files with filename in /tmp or deeper that are 30 days old or older find /tmp -name '' -mtime +30 -exec rm {} \; # in a home directory find all files created or changed in last 2 days # except for invisible files and directories which are likely to be # updated regularly by running processes and not by manual changes. find . -mtime -2 -type f ! -name '.*’ -not -path "./.*/*" ################################################### #### locate locate # to update the "locate" database: updatedb ################################################### #### RSYNC # SYNTAX: # rsync # # NEVER put a trailing slash on destination # Without a trailing slash on source, "copy entire directory" # the below command will result in /data/tmp/bar rsync foo:/src/bar /data/tmp # Slash at end of source means copy directory CONTENTS (think of it as rsync foo:/src/bar/* /data/tmp/bar) # So if you WANT "bar" at the destination, that directory must already exist # and you need to put it in the destination path rsync foo:/src/bar/ /data/tmp/bar # While preserving all links, attributes, ownership # copy recursively all files in foo:/src/bar to /data/tmp/bar # Use "archive" mode to copy rsync -av foo:/src/bar/ /data/tmp/bar # the -z option compresses during the copy rsync -avz /src/bar foo:/data/tmp/bar -H is used to preserve hard links -P if transfer is interrupted, do not delete partially transferred files (default), instead keep them. rsync -avHP foo:/src/bar/ /data/tmp # -c, if file already exists in destination, skip based on checksum, not mod-time & size rsync -acvHP rsync -acvHPe ssh #use the -e option to specify a protocol or shell rsync --exclude-file=install_excludes.txt -acvHPe ssh username@192.168.0.100:/root/install /tmp rsync -acv -e "ssh -l username" host::module /dest # rsync for large files over network rsync -azPve ssh /home/giantfile.tar username@remotehost.com:/home/username # rsync for backups # -t transfers new files and updates files. If files on source and destination are same, no action. # -r recursive # -p preserve permissions rsync -rpte ssh /home/username/valuable/files username@backuphost:/backup/repository #### Troubleshooting and Maintanance ############################################### #### #### Remember to install "sysstat" to get iostat, sar #### start and enable the sysstat service #### ############################################### # Determine the resource utilization of a host # (install sysstat for iostat, mpstat, and sar) # averages of various system statistics since last boot. vmstat # recent history of I/O wait and idle time sar # statistics of CPU and I/O for devices, partitions, and NFS. iostat # statistics broken down by CPU mpstat -P ALL # partition size and fullness df -Thx squashfs # performance testing for monitoring the Linux kernel perf # measures socket performance - needs to run on 2 machines: sender and receiver qperf # information about cpu architecture lscpu # network performance on a host netstat -s # current active connections to where and using which service netstat -p # showing active tcp connections netstat -natup # show open ports lsof -i -P -n | grep LISTEN # show open ports netstat -tulpn | grep LISTEN # using ss instead of netstat: ss -tulwn ss -tulpw ss -tulw ss -tunlp4 # FLAGS # -t - Show TCP ports. # -u - Show UDP ports. # -n - Do not try to resolve hostnames. # -l - Show only listening ports. # -p - Show the processes that are using a particular socket. # -4 - Show only IPv4 sockets. # #### NETCAT netcat !!! # example use netcat to test udp port 123 on ntp server 192.168.1.5 nc 192.168.1.5 -u 123 # Network - see interfaces (and get interface names) ip -br link show # using output of above command to see the interface names # bring up a link that is down ip link set up # determine link health (am I dropping packets?) ip -s link show # examine the configuration for network interface ethtool # show connected server status ip neighbor show # show port 2100 lsof -i:2100 # find open ports nmap -sTU -O # a snapshot of the top command output top -n 1 -b # measure streaming speed ("real" output shows IO) time cat /usr/share/dict/words # memory installed and used on a host cat /proc/meminfo # which processes are the memory hogs? # (sorts ps aux on column number 4 which is memory) ps aux | sort -nk +4 | tail # list all PCI devices on your computer (and chipset info) # Helpful for determining make, model manufacturer so that you can find # the right drivers for your hardware. lspci # list all shared library dependencies for a program # Helpful for determining missing packages ldd # list all open files # Helpful to run before reboots, before unmounting directories lsof # Even more helpful - alphabetical order, unique listings lsof |awk '{ print $1 }' | sort | uniq # list environmental variables printenv # Another way to display file perms than ls -la stat -c %a (filename) # PS - Process Commands # (sorts ps aux on column number 4 which is memory) ps aux | sort -nk +4 | tail Process tree: ps -ejH ps axjf Information on threads: ps -eLf ps axms Security information: ps -eo euser,ruser,suser,fuser,f,comm,label ps axZ ps -eM Every process running as root (real & effective ID) in user format: ps -U root -u root u Processes excluding the square bracket processes. if /proc//cmdline is an empty string. Usually spawned by kernel. ps aux |grep -v "]" (note the double quotes) PROCESS STATE CODES D uninterruptible sleep (usually IO) R running or runnable (on run queue) S interruptible sleep (waiting for an event to complete) T stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) Z defunct ("zombie") process, terminated but not reaped by its parent. < high-priority (not nice to other users) N low-priority (nice to other users) L has pages locked into memory (for real-time and custom IO) s is a session leader I is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) + is in the foreground process group. # SElinux: final command after making fix in single user mode, before reboot: touch /.autorelabel ########### HARDWARE INFORMATION ############### ipmitool sdr dmidecide | grep -A3 '^System Information' dmidecode -t 1 dmidecode -t 3 dmidecode -t system lshw -c system | head lshw # chipset lspci #### Network Troubleshooting and Maintanance # traceroutes and routes mtr (command line ping-plotter) ping mping hping telnet or BETTER: netcat aka ncat aka nc nmap wireshark aka tshark tcpdump tracepath traceroute tracert route netstat -r arp = ip -r # like ifconfig ip a # like arp or route ip r # like netstat ss [-r] # netcat **** nc # Network Manager Command Line tool - CentOS8 nmcli # more network commands ifconfig = ip -a # dns query - get domain name from ip addr or vice versa nslookup dig #### Working with Files # capture standard out and standard error together in a file {command} 2>&1 filename.txt # perl - display the directories where modules are accessed perl -e "print \"@INC\"" # Display date as yyyymmdd date +%Y%m%d # sort file by third column (delimited by ":"). Valuable for Password files sort -n -t : -k 3 filename # remove new line tr -d '\n|\r' # cut is similar to perl split when used with -d "delimiter" cut -d' ' # https://unix.stackexchange.com/questions/183994/understanding-unix-permissions-and-file-types # Display the chmod file permissions in octal stat -c %a (filename) ################ diff ################### diff file1 file2 diff -r directory1 directory2 diff -ur # compare files side by side -y # ignore space changes -b diff -y -b file1 file2 ########################################## ######## ############## ######## Terminal Helpers ############## ######## ############## ########################################## # to remove carat-H on terminals stty erase # Ubuntu - change default editor sudo update-alternatives --config editor # SCREEN suspend a session so you can reattach to it later: screen # hot key Ctrl-a # detach hot key, then d # list available screens screen -list # reconnect screen -r # terminate current session exit # OR TMUX tmux # easier to script, easier to pass session to someone else # to reconnect tmux attach # hot key Ctrl-b # detach hot key, then d # list available sessions tmux list-sessions # reconnect tmux attach-session tmux attach-session -t # terminate current session exit