Skip to content

Latest commit

 

History

History
1365 lines (659 loc) · 28.3 KB

README.md

File metadata and controls

1365 lines (659 loc) · 28.3 KB

Server Administration Quick Reference

Introduction

This is an assorted list of Unix commands. These are commands I've found useful in the past that I don't want to forget.

Table of Contents

Filesystem Commands

Zeroing out a file

cat /dev/null > <filename>

Grep multiple files across multiple directories

find . -type f -print | xargs fgrep -i -l "text_to_grep_for"

Find files larger than 1000000 bytes

ll -R <directoryname> | awk '{if ($5 > 1000000) print $5 "\t" $9}'

find . -xdev -size +1000000c
Linux
find . -xdev -size +1000000c -exec ls -al {} \; | sort -k 5n
HP-UX
find . -xdev -size +1000000c -exec ls -al {} \; | sort -r +4n

Find files modified 3 or fewer days ago

Linux
find . -xdev -type f -mtime 3 -exec ls -al {} \; | sort -rk +5n
HP-UX
find . -xdev -type f -mtime 3 -exec ls -al {} \; | sort -r +4n

Delete object files and print what's deleted

find . -name "*.o" -exec echo 'rm -f {}' \; -exec rm -f {} \;

Show list of directories and their sizes in kilobytes

Linux
du -xhk | sort -k 1n
HP-UX
du -xk | sort +0n

Show how much space a directory is taking up

du -shx

Show how much space subdirectories are taking up:

du -shx $(ls -d */)

Determine what process has a file open

fuser -u /path/to/file

psg <pid>

Monitor a directory for open filehandles. Repeat command every second forever.

while true; do lsof +d /tmp; sleep 1; done

Show space on drives

df -k

Querying file locks max value on HP-UX

kcusage | grep nflocks

Create a file of a given size

dd if=/dev/zero of=don.out bs=1024 count=10240

Delete file by inode

ls -li

find . inum <inode number> -exec rm -l {} \;

Search/Replace in a file

sed -ri 's/(test1|test2)/value3/g' app.cfg

Delete logfiles without an open file handle

find /tmp -type f -exec bash -c "fuser {} || rm {}" \;

Determine if it is an HDD or SSD (0 means SSD, 1 means HDD)

cat /sys/block/sdc/queue/rotational  

Manually formatting and mounting a block device

lsblk  
file -s /dev/xvdb  
mkfs -t ext4 /dev/xvdb  
mkdir /mnt/jenkins  
mount /dev/xvdb /mnt/jenkins  
ll /mnt/jenkins  
df -h  

Kubernetes Commands

General Commands

Login on Azure

az aks get-credentials --name <resource name> -g <resource group>

az aks get-credentials --name <resource name> -g <resource group> --admin

Get Context

kubectl config -get-contexts

Pods

kubectl get pods --all-namespaces

kubectl get pods -l app=nginx-ingress --all-namespaces

kubectl get pods -l app=nginx-ingress -o wide --namespace=kube-system

kubectl describe pods <pod name> --namespace=kube-system

Services

kubectl get services --all-namespaces

kubectl get svc <service name> --namespace=kube-system

kubectl describe services --all-namespaces

kubectl describe svc <service name> --namespace=kube-system

Deployments

kubectl describe deployment <deployment name>

Configmaps

kubectl get configmaps

kubectl get configmaps --namespace=kube-system

kubectl get configmaps --namespace=kube-system -o yaml

SSH

kubectl exec -it <pod name> -- /bin/bash

kubectl exec -it <pod name>-n <namespace> -- /bin/bash

Endpoints

kubectl get ep

Secrets

kubectl get secrets --all-namespaces

Ingress

kubectl get ingress

Events

kubectl get events --all-namespaces

Logs

kubectl logs <pod name> --namespace <namespace name>

kubectl logs -f <pod name>

Horizontal Pod Autoscaler

kubectl autoscale deployment <deployment name> --cpu-percent=50 --min=1 --max=10

kubectl get hpa

kubectl describe hpa

Scale Deployment

kubectl scale --replicas =3 <deployment name> -n <namespace name>

Setting up Ingress

Create Cert

openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out don_test.crt -keyout don_test.key

Add Cert

kubectl create secret tls <secret name> --cert=don_test.crt --key=don_test.key

Create Public IP on Azure

az network public-ip create -g <resource group name> -n <namespace name> --alocation-method static --reverse-fqdn example.westus.cloudapp.azure.com --dns-name example

Create Ingress Controller

helm install <chart name> --namespace <namspace name> --set controller.service.loadBalancerIP="<insert ip here>" --set controller.replicaCount=2

Deploy App

kubectl apply -f <yaml filename>

Log Analytics on Azure

CPU Graph

Perf |where CounterName == "cpuUsageNaneCores and ObjectName == "K8SContainer"

| where TimeGenerated > ago(1d)

| summarize avg(CounterValue), percentiles(CounterValue, 50, 95) by bin(TimeGenerated, 1h)

Events

KubeEvents | where TimeGenerated > ago(1d)

KubeEvents | where SourceComponent == "cluster-autoscaler"

Vim

Removing ^M characters at end of lines in vi

:%s/^V^M//g

The ^V is a CONTROL-V character and ^M is a CONTROL-M. When you type this, it will look like this:

:%s/^M//g

alternate command:

dos2unix <filename>

Comment a block of text in vim

  • control-V
  • <highlight text with cursor> (use arrow keys. Only one column will be highlighted)
  • shift-I
  • #
  • escape

Copy column in vim

  • control-V
  • <highlight text with cursor>
  • p

Opening a new file in vim

:n <filename>

:e <filename>

List buffers

:ls

Switch to a different buffer

:b<buffer number>

:bnext

:bprev

Open file in new tab

:tabe <filename>

Multiple Windows in Vim

Syntax Description
:split <filename> split window and load another file
vplit <filename> vertical split
ctrl-w up arrow move cursor up a window
ctrl-w ctrl-w move cursor to another window (cycle)
ctrl-w_ maxmize current window
ctrl-w= make all equal size
10 ctrl-w+ increase window size by 10 lines
:hide close current window
:only keep only this window open

Enable mouse in vim (lets you resize split windows)

:set mouse=a

Set tab to 4 spaces

set smartindent
set tabstob=4
set shiftwidth=4
set expandtab

Enable tab character

set noexpandtab

Miscellaneous

Sending a text file via e-mail

mailx -s "SUBJECT" [email protected] < file.out

Show how many rows and columns your display is set to

resize

System logs are located at:

Linux
/var/log/messages
HP-UX
/var/adm/syslog

List all files in tar archive

tar -tvf filename.tar

Tar a directory

tar -cvf <name>.tar <directoryname>

See which packages are assigned where in a failover

HP-UX
cmviewcl
AIX
  /usr/sbin/cluster/utilities/clfindres

Or this command:

  /usr/sbin/cluster/clstat

Getting a list of installed packages

HP-UX
swlist

swlist -l patch
AIX
lslpp -L

Getting list of products from a depot file (HP-UX)

swlist -d -s <depot filename>

Installing product from a depot (HP-UX)

swinstall -s <depot filename>

Converting unix epoch seconds to exec serial date format

=A1/86400+(25569-(5/24))

Print contents of a file in reverse order

sed -s '1!G;h;$p'

Get checksum of a file

cksum <filename>

Search contents of all files in a directory for a string

grep "string to search" *

Grep string, including 10 lines before and after

grep –B 10 –A 10 string_to_search <filename>

Repeat a command every 5 seconds

watch -n5 <command>

Show OS limits for a process (Linux only)

cat /proc/<PID>/llimits

Print file in binary format

xxd -b <filename>

Print file in hexadecimal format

hexdump <filename>

Print file in octal format

od <filename>

See all environment variables for a process running on Linux:

xargs -n 1 0 < /proc/<PID>/environ

Getting Centrify info on a user

adinfo -u <user id>

Check if a server is a physical or virtual

Linux
dmidecode -t 1

Output:

"Manufacturer: Vmware, Inc." is a virtual
AIX
lparstat -I | grep Type

Output:

Shared-SMT is a virtual

Dedicated-SMT is physical
HP-UX
model

Output:

"ia64 hp server Integrity Virtual Machine" is a virtual

"ia64 hp server rx260" is a physical
Windows
systeminfo

Output:

"System Manufacturer: System Manufacturer" is a physical

"System Manufacturer: Vmware, Inc. System Model: VMWare Virtual Platform" is a virtual

List kernel parameters

HP-UX 11.11
/usr/sbin/kmtune -l
HP-UX 11.23 and above
kctune

cat /stand/tunes

List details about a particular parameter

kctune -v -q maxuprc
Redhat
sysctl -a

SSH to box without password

ssh-keygen -t rsa

ssh-copy-id <userid>@<hostname>

ssh <userid>@<hostname>

Getting process list with full arguments an HP-UX

/usr/bin/env UNIX95=1 /bin/ps -eo 'state,uid,ppid,args'

Syncronize panes in Tmux

:setw synchronize-panes

Bind key for synchronize-panes in Tmux

bind a set-window-option synchronize-panes

Switching version of a tool on Redhat

scl enable python27 python (opens a python shell)

scl enable python27 bash (opens a new bash shell with python configured)

Color diff in two columns

sdiff -w 230 <file 1> <file 2> | colordiff | less -r

Networking

Show sockets being used

ss

Show ports open for listening

netstat -a | grep LISTEN | grep -v unix

Show what process is using a particular port

lsof -i :9703

Alternatively, you can run:

netstat -Aan | grep <port number>

rmsock <socket number> tcpcb

fuser -v -n tcp 5432

Show all programs with an open network connection

lsof -l

Listen to traffic on a port

tcpdump -nn -I eth0 port 5432

sudo tcpdump -nn

Show # of retransmitted network packets

netstat -s -t | egrep "trans|select"

Testing network connection (TCP)

telnet localhost 9000

nc -vz localhost 9000

Testing network connection (UDP)

nc -vzu localhost 5514

Compiler Stuff

List symbols in a binary

nm -a <filename>

dump -Tv <filename>

List symbols in a windows binary

set PATH to "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin"

cmd

cd "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin"

vcvarsall.bat

cd "dir_where_your_binary_is"

dumpbin /all <filename>

List .o files embedded in .a files (also shows architecture)

objdump -f <filename>

Extract .o files from .a flle

ar x <filename>

Determining what libraries a binary is dynamically linked to

ldd <filename>

Look for a symbol across multiple files

find . -name "*.sl" .-exec sh -c 'echo lib: $0; nm -a $0 | grep tp_sleep' {} \;

RPM

RPM Commands

Check for a package in linux

rpm -qa | grep ssh

Info on a package

rpm -qi <package name>

Info on a package that is not installed

rpm -qip <file name>

List files in a package

rpm -ql <package name>

Which rpm owns a particular file

rpm -qf <filename>

List files in an rpm archive

rpm -qpil <filename>

Install local rpm file

rpm -ivh <filename>

List most recently installed rpms

rpm -a --last | head -10

List dependencies of an rpm

rpm -qR <package name>

List Repos

yum repolist enabled

yum repolist all

zypper repos

Find out what RPM provides file

yum whatprovides <filename>

Get info on a package

yum info <filename>

Install a local rpm via yum

yum localinstall <file name>

Make yum see the lastest packages

yum clean expire-cache

Run Puppet

puppet agent -t

Mock Commands

Populate chroot environment

mock -r epel-6-i386 --init

mock -r epel-6-x86_64 --init

Spawn shell

mock -r epel-i386 --shell

mock -r epel-6-x86_64 --shell

Copy files into chroot environment

mock -r epel-6-i386 --copyin <filename> /

Copy file out of chroot environment

mock -r epel--i386 --copyout <file path> .

Install rpms

mock -r epel-6-i386 --install <rpm packagename>

mock -r epel-6-x86_64 --install <rpm packagename>

FPM Commands

Create a Jenkins .deb package

fpm -s dir -t deb -n jenkins -v 1.396 --prefix /opt/jenkins -d "sun-java6-jre (> 0)" jenkins.war

Create a Jenkins rpm package

fpm -s dir -t deb -n jenkins -v 1.396 --prefix /opt/jenkins -d "sun-java6-jre (> 0)" jenkins.war

Postgres

List databases

psql -l

Command line interface

psql

Connect to a database

\c <database name>

List tables

\d

Services

Start/stop a service

SysV Init
service <service name> start

service <service name> stop

service <service name> restart

service <service name> status
Systemd
systemctl start <service name>

systemctl stop <service name>

systemctl restart <service name>

systemctl status <service name>

List all services

SysV Init

chkconfig –list

Systemd

systemctl list-unit-files --type=service  

Disable/enable a service on startup

SysV Init

chkconfig <service name> off

chkconfig <service name> on

Systemd

systemctl disable <service name>

systemctl enable<service name>

Java Keytool

List Certs in a Keystore

/usr/java/latest/bin/keytool -keystore /usr/java/latest/jre/lib/security/cacerts --list

Add Cert to Keystore

/usr/java/latest/bin/keytool -keystore /usr/java/latest/jre/lib/security/cacerts -import -file <path to cert> -alias example.domain.com -storepass PASSWORD -noprompt

Delete Cert from Keystore

/usr/java/latest/bin/keytool -keystore /usr/java/latest/jre/lib/security/cacerts -delete -alias example.domain.com -storepass PASSWORD -noprompt

Download Cert from Wedsite

openssl x609 -in <(openssl s_client -connect example.domain.com:443 -prexit 2>/dev/null)

Creating a self-signed cert

openssl req -newkey rsa:2048 -nodes -keyout filename-key.pem -509 -days 365 -out filename-cert.pem

Creating a pfx file

openssl pkcs12 -export -out filename.pfx -inkey filename-key.pem -in filename-cert.pem

Export the private key from PFX

openssl pkcs12 -export - filename.pfx -nocerts -out filename-key.pem -nodes

Convert PFX to PEM file

opensssl pkcs12 -in filename.pfx -nokeys -out filename-cert.pem

Remove passphrase from private key

openssl rsa -in filename-key.pem -out filename-key2.pem

Convert PFX to key and cert file

openssl pkcs12 -in filename.pfx -clcerts -nokeys -out filename.cer

openssl pkcs12 -in filename.pfx -nocerts -nodes -out filename-encrypted.key

openssl rsa -in filename-encrypted.key -out filename.key

openssl pkcs12 -nokeys -clcerts -in filename.pfx -out filename.cer

Validating the key and cert (md5 should be the same)

openssl x509 -noout -modulus -in filename-cert.pem | openssl md5

openssl rsa -noout -modulus -in filename-key.pem | openssl md5

Docker

Enabling non-root user to run Docker client

gpasswd -a <user name> docker

Starting an interactive session

docker run -t -I ubuntu /bin/bash
Get shell inside running container
docker exec -I -it <container name> bash

Creating a new base image

cat filename.tar | docker import - <image name>

Inspecting a container

docker inspect <container name>

Removing docker0 bridge

service docker stop

ip link set dev docker0 down

brctl delbr docker0

iptables -t nat -F POSTROUTING

route -n

systemctl daemon-reload

service docker start

Mac

Fixing hostname

scutil -set ComputerName "<computer name>"

scutil -set HostName "<computer name>"

scutil -set LocalHostName "<computer name>"

Getting AD Groups

dscl "/ActiveDirectory/DOMAIN/fqdn" read /Users/userid

Git

Get changed files between commits

git diff --name-only <commit id> HEAD 
git diff --name-only <commit id> HEAD 

Get changes across all branches

git log --name-only --since "5 days ago" --until="now" --all

Get changed files across all branches

git log --name-status --since "5 days ago" --until="now" --all | grep -E '^[A-Z]\b' | sort -k 2,2 -u

List large blobs in repository

git rev-list --objects --all |
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
  sed -n 's/^blob //p' |
  sort --numeric-sort --key=2 |
  cut -c 1-12,41- |
  $(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest

Find branches that contain a commit

git branch -a --contains <commit>

Find commit that contains a blob

git whatchanged --all --find-object=<blob id>