Skip to content

Instantly share code, notes, and snippets.

@servercharlie
Last active March 10, 2017 03:14
Show Gist options
  • Save servercharlie/86c3b024b633c47610938fd66150634e to your computer and use it in GitHub Desktop.
Save servercharlie/86c3b024b633c47610938fd66150634e to your computer and use it in GitHub Desktop.
Ubuntu 16.10 (Yakkety Yak!) Commands

Speed Testing

@ http://www.spiffy.sg/it-pros/running-speed-test-ubuntu-server/

Installation & Usage

sudo -i
apt-get install speedtest-cli
speedtest-cli

w/ server-selection.

speedtest-cli --list
speedtest-cli --server SERVER_ID

PM2

PM2 - Allow PM2 to bind applications on ports 80/443 without root

It’s a general rule that you shouldn’t run node as root, but only root can bind to ports less than 1024. This is where authbind comes in. Authbind allows non-root users to bind to ports less than 1024.

@ http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#allow-pm2-to-bind-applications-on-ports-80-443-without-root

sudo apt-get install authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80
authbind --deep pm2 update

sudo nano ~/.bashrc
alias pm2='echo -e "\t Executing Authbinded PM2" | wall && authbind --deep pm2'
source ~/.bashrc

pm2 status

PM2 - Commands

pm2 start app.js --name "Anterior" --node-args="--harmony" --watch -e err.log -o out.log
pm2 stop 0 --watch
pm2 logs 0

PM2 - Updating

npm install pm2@latest -g
pm2 update
pm2 completion install

PM2 - Cheatsheet

@ http://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/#cheatsheet

# Fork mode
$ pm2 start app.js --name my-api # Name process

# Cluster mode
$ pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
$ pm2 start app.js -i max      # Same as above, but deprecated yet.

# Listing

$ pm2 list               # Display all processes status
$ pm2 jlist              # Print process list in raw JSON
$ pm2 prettylist         # Print process list in beautified JSON

$ pm2 describe 0         # Display all informations about a specific process

$ pm2 monit              # Monitor all processes

# Logs

$ pm2 logs [--raw]       # Display all processes logs in streaming
$ pm2 flush              # Empty all log file
$ pm2 reloadLogs         # Reload all logs

# Actions

$ pm2 stop all           # Stop all processes
$ pm2 restart all        # Restart all processes

$ pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)
$ pm2 gracefulReload all # Send exit message then reload (for networked apps)

$ pm2 stop 0             # Stop specific process id
$ pm2 restart 0          # Restart specific process id

$ pm2 delete 0           # Will remove process from pm2 list
$ pm2 delete all         # Will remove all processes from pm2 list

# Misc

$ pm2 reset <process>    # Reset meta data (restarted time...)
$ pm2 updatePM2          # Update in memory pm2
$ pm2 ping               # Ensure pm2 daemon has been launched
$ pm2 sendSignal SIGUSR2 my-app # Send system signal to script
$ pm2 start app.js --no-daemon
$ pm2 start app.js --no-vizion
$ pm2 start app.js --no-autorestart

$ pm2 start app.js           # Start app.js

$ pm2 start app.js -- -a 23  # Pass arguments '-a 23' argument to app.js script

$ pm2 start app.js --name serverone # Start a process an name it as server one
                                    # you can now stop the process by doing
                                    # pm2 stop serverone

$ pm2 start app.js --node-args="--debug=7001" # --node-args to pass options to node V8

$ pm2 start app.js -i 0             # Start maximum processes depending on available CPUs (cluster mode)

$ pm2 start app.js --log-date-format "YYYY-MM-DD HH:mm Z"    # Log will be prefixed with custom time format

$ pm2 start app.json                # Start processes with options declared in app.json
                                    # Go to chapter Multi process JSON declaration for more

$ pm2 start app.js -e err.log -o out.log  # Start and specify error and out log


Options:

   -h, --help                           output usage information
   -V, --version                        output the version number
   -v --version                         get version
   -s --silent                          hide all messages
   -m --mini-list                       display a compacted list without formatting
   -f --force                           force actions
   -n --name <name>                     set a <name> for script
   -i --instances <number>              launch [number] instances (for networked app)(load balanced)
   -l --log [path]                      specify entire log file (error and out are both included)
   -o --output <path>                   specify out log file
   -e --error <path>                    specify error log file
   -p --pid <pid>                       specify pid file
   --max-memory-restart <memory>        specify max memory amount used to autorestart (in megaoctets)
   --env <environment_name>             specify environment to get specific env variables (for JSON declaration)
   -x --execute-command                 execute a program using fork system
   -u --user <username>                 define user when generating startup script
   -c --cron <cron_pattern>             restart a running process based on a cron pattern
   -w --write                           write configuration in local folder
   --interpreter <interpreter>          the interpreter pm2 should use for executing app (bash, python...)
   --log-date-format <momentjs format>  add custom prefix timestamp to logs
   --no-daemon                          run pm2 daemon in the foreground if it doesn't exist already
   --merge-logs                         merge logs from different instances but keep error and out separated
   --watch                              watch application folder for changes
   --ignore-watch <folders|files>       folder/files to be ignored watching, chould be a specific name or regex - e.g. --ignore-watch="test node_modules "some scripts""
   --node-args <node_args>              space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"
   --no-color                           skip colors
   --no-vizion                          skip vizion features (versioning control)
   --no-autorestart                     do not automatically restart apps

Memory & Processes

Quick find, narrow-pid, kill

Find the process id of a program, check what ports are in use, and kill it

sudo netstat -ntlp | grep LISTEN
ps aux | grep -i “NAME_OF_PROGRAM”
sudo kill -9 PROCESS_ID

Find a process using a specific port

lsof -t -i:[PORT_NUMBER]
netstat -tulpn | grep :[PORT_NUMBER]

List All Open Network Connections

sudo netstat -nlp
sudo ps -ef

By User

sudo pgrep -l -u USER_NAME
sudo ps -ef | grep USER_NAME
fuser PORT_NUMBER/tcp

Get ports used by given PID

lsof -Pan -p PROCESS_ID -i
ss -l -p -n | grep "PROCESS_ID"
sudo kill PROCESS_ID

List Mem Info

Returns something like:

cat /proc/meminfo | grep Mem

Output:

MemTotal:        1972268 kB
MemFree:          544936 kB
MemAvailable:    1437692 kB

Using SMEMSTAT

smemstat reports the physical memory usage taking into consideration shared memory.

Swap reports memory that is swapped out to disk. Unshared memory is reported as USS (Unique Set Size). Unshared memory including the proportion of shared memory is reported as the PSS (Proportional Set Size). The non-swapped physical memory is reported as the RSS (Resident Set Size).

Running with root privilege will report all user space processes, otherwise running without will just report the current user's processes.

@ http://manpages.ubuntu.com/manpages/xenial/man8/smemstat.8.html

sudo apt install smemstat
smemstat

Output as a user:

  PID       Swap       USS       PSS       RSS User       Command
 22807     0.0 B    24.7 M    34.5 M    46.1 M azureuser  node /home/azureuser/ancillary/index.js
 22214     0.0 B    21.4 M    31.2 M    42.8 M azureuser  PM2 v2.4.2: God Daemon (/home/azureuser/.pm2)
 43429     0.0 B  2896.0 K  3011.0 K  4780.0 K azureuser  -bash
 21548     0.0 B  1000.0 K  1982.0 K  6288.0 K azureuser  /lib/systemd/systemd
 43642     0.0 B   140.0 K   188.0 K  1724.0 K azureuser  smemstat
Total:     0.0 B    50.1 M    70.7 M   101.4 M

Output as root:

22807     0.0 B    24.7 M    34.5 M    46.1 M azureuser  node /home/azureuser/ancillary/index.js
 22214     0.0 B    21.1 M    30.9 M    42.4 M azureuser  PM2 v2.4.2: God Daemon (/home/azureuser/.pm2)
  1948     0.0 B    25.3 M    27.9 M    33.5 M root       python3
  1600     0.0 B    19.1 M    19.2 M    20.3 M root       /usr/lib/snapd/snapd
  1674     0.0 B    11.5 M    13.9 M    19.3 M root       /usr/bin/python3
  1587     0.0 B  4216.0 K  4264.0 K  5804.0 K root       /usr/bin/lxcfs
   488     0.0 B  3340.0 K  3650.0 K  6764.0 K root       /lib/systemd/systemd-journald
 43429     0.0 B  2896.0 K  3001.0 K  4780.0 K azureuser  -bash
   552     0.0 B  2728.0 K  2875.0 K  5236.0 K root       /lib/systemd/systemd-udevd
  1665     0.0 B  1064.0 K  2536.0 K  6468.0 K root       /usr/lib/accountsservice/accounts-daemon
  1910     0.0 B  1052.0 K  2492.0 K  6128.0 K root       /usr/lib/policykit-1/polkitd
     1     0.0 B  1040.0 K  2477.0 K  7112.0 K root       /sbin/init
  1623     0.0 B  2304.0 K  2377.0 K  4276.0 K syslog     /usr/sbin/rsyslogd
 21548     0.0 B  1000.0 K  1986.0 K  6288.0 K azureuser  /lib/systemd/systemd
 43305     0.0 B   544.0 K  1938.0 K  6780.0 K root       sshd: azureuser [priv]
  1795     0.0 B  1584.0 K  1777.0 K  3656.0 K root       /sbin/iscsid
  1652     0.0 B   712.0 K  1721.0 K  6080.0 K root       /usr/sbin/sshd
 21550     0.0 B  1176.0 K  1715.0 K  3012.0 K azureuser  (sd-pam)
  1270     0.0 B  1552.0 K  1575.0 K  2312.0 K root       /sbin/dhclient
  1538     0.0 B  1088.0 K  1402.0 K  3956.0 K messagebus /usr/bin/dbus-daemon
 43661     0.0 B   976.0 K  1222.0 K  3852.0 K root       sudo
  1694     0.0 B   792.0 K  1194.0 K  4784.0 K systemd-re /lib/systemd/systemd-resolved
 43420     0.0 B   212.0 K  1159.0 K  4228.0 K azureuser  sshd: azureuser@pts/0
  1697     0.0 B   700.0 K  1086.0 K  4488.0 K root       /lib/systemd/systemd-logind
  1039     0.0 B   592.0 K  1075.0 K  4744.0 K systemd-ti /lib/systemd/systemd-timesyncd
   554     0.0 B   656.0 K   756.0 K  2716.0 K root       /sbin/lvmetad
  1508     0.0 B   328.0 K   484.0 K  2932.0 K root       /usr/sbin/cron
  1637     0.0 B   292.0 K   482.0 K  2628.0 K root       /usr/lib/linux-tools/4.8.0-39-generic/hv_kvp_daemon
  1626     0.0 B   308.0 K   432.0 K  2624.0 K unscd      /usr/sbin/nscd
  1569     0.0 B   232.0 K   330.0 K  2400.0 K root       /usr/sbin/atd
  1949     0.0 B   240.0 K   323.0 K  1896.0 K root       /sbin/agetty
  1794     0.0 B   144.0 K   304.0 K  1640.0 K root       /sbin/iscsid
  1922     0.0 B   136.0 K   278.0 K  2080.0 K root       /sbin/agetty
  1577     0.0 B   184.0 K   236.0 K  1824.0 K root       /usr/sbin/acpid
 43666     0.0 B   144.0 K   190.0 K  1616.0 K root       smemstat
  1529     0.0 B   108.0 K   149.0 K  1480.0 K root       /usr/lib/linux-tools/4.8.0-39-generic/hv_vss_daemon
Total:     0.0 B   133.3 M   170.8 M   283.4 M

List all processes

ps aux

TREE

# ubuntu tree
sudo apt-get install tree

# print to terminal in tree format
$ tree [DIRECTORY]

# example [csv] directory outputs:
├── csv
│   ├── CVS
│   │   ├── Entries
│   │   ├── Repository
│   │   └── Root

@ https://gist.github.com/hattmarris/0487c577196f2b6f2421

Show current user and shell program

echo $(whoami) is logged in and shell is $0

/root/.bashrc
~/.profile

whoami - current user name
$  - user
#  - root user
~  - home
/  - root
-  - back (navigation)
.. - up one level (navigation)
cd - change directory eg -  cd /var/www  OR  cd ~/Desktop
pwd - print working directory
ls - lists files/folders in the current directory
mv - move file/folder or rename file/folder - eg- mv myFileName myDirectoryPath (mv foo ~/Desktop) OR mv myFileName myNewFileName
cp -a /path/from /path/to - copy contents of folder to another folder
sudo chown -R username:group directory /directory - change ownership of folder and files within to username and group
groups <username> -  lists groups <username> belongs to. blank <username> will list current users groups
sudo usermod -a -G {group} {username} - add user to a group
gksudo - Graphical sudo to start graphical applications as Root. 
path/to/executable/name_of_prog  OR ./name_of_prog IF IN DIRECTORY - running an exe? eg - /opt/Aptana_Studio_3/AptanaStudio3
sudo service apache2 restart - restart server
xdg-open $file - open file graphically
sudo mkdir folder path/name of folder - create new folder as sudo
gksudo nautilus - run the file manager as root 
apt-get remove <package> - remove/uninstall package
apt-get autoremove - removes packages that were installed by other packages and are no longer needed.
rm -r mydir - remove mydir
rm -rf mydir - remove mydir and no prompts

@ https://gist.github.com/AliBritt/f3e205a66a93a4b3ee05

CHMOD

(all access)
sudo chmod 777 -R PATH

(only owner and root can access)
sudo chmod 750 -R PATH

etc
@ https://danflood.com/tech-stuff/chmod-cheat-sheet/
@ https://isabelcastillo.com/linux-chmod-permissions-cheat-sheet

Updates & Upgrades

sudo apt-get update
sudo apt-get upgrade --yes

Display current working directory

pwd

Display Total Disk Size

fdisk -l | grep Disk

(show usage)
df -h

(show volumes)
lsblk

Displays Size of Folder sorted according to size

sudo du -sh .[!.]* *| sort -nr
(Including Subdirectories)

sudo du -sh * | sort -nr
(Excluding Subdirectories)

Displays the total size of the current directory including all the subdirectories.

du -ch | grep total

Sum of directories (-s) in kilobytes (-k):

du -sk *

Sum of directories (-s) in human-readable format (-h : Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte):

du -sh *

Disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize) :

 $$ du -sk .[!.]* *| sort -n

Disk usage of all subdirectories and files including hidden files within the current directory (sorted by reverse filesize) :

 $$ du -sk .[!.]* *| sort -nr

Remove a directory recursively

rm -rf [dir-name]

Rename a directory or move a file or directory

mv [old-path] [new-path]

Get your server's IP address

ifconfig eth0 | grep inet | awk '{ print $2 }'

search packages

apt-cache search packageName 

installing packages

apt-get install packageName

uninstalling packages

apt-get remove packageName 
apt-get remove packageName --purge (removes configuration files too) 

view list of installed packages

dpkg --get-selections | grep packageNameOrKeyword 
@servercharlie
Copy link
Author

servercharlie commented Mar 8, 2017

Show security updates only:
apt-get -s dist-upgrade |grep "^Inst" |grep -i securi
Show all upgradeable packages
apt-get -s dist-upgrade | grep "^Inst"
Install security updates only
sudo apt-get -s dist-upgrade | grep "^Inst" | grep -i securi | awk -F " " {'print $2'} | xargs apt-get install
Check what services need to be restarted after package upgrades.
Figure out what packages you are going to upgrade beforehand and schedule your restarts/reboots.
The problem here is that unless you restart a service it still may be using an older version of
a library (most common reason) that's been loaded into memory before you installed new package
which fixes a security vulnerability or whatever.
checkrestart -v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment