Linux notes

 Monday, 15 2 2016, 14:24

Memory info

dmidecode --type 17

 Monday, 15 2 2016, 14:24

ssh keys

mkdir ~/.ssh

# on remote host

ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa

#on local machine

chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

# on local machine

cat ~/.ssh/id_rsa.pub | ssh user@host 'cat - >> ~/.ssh/authorized_keys'

# on local machine

chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh/

# on remote host

ssh-keygen -f "/path/to/.ssh/known_hosts" -R <ip_address>

# remove ip from known hosts

ssh-keygen -p

# change the passphrase for the key

eval "$(ssh-agent -s)"

# start ssh-agent if not started

ssh-add ~/.ssh/id_rsa

# add the key to ssh-agent (requires passphrase)

http://mah.everybody.org/docs/ssh

# using ssh-agent with ssh

 Monday, 15 2 2016, 14:30

Find distro

cat /etc/*-release

lsb_release -a

cat /proc/version

 Monday, 15 2 2016, 14:31

Find kernel version

uname -a

uname -mrs

 Monday, 15 2 2016, 14:31

Add to path

vim /etc/profile

# in this file

export PATH="/new/path":$PATH

# add this line

 Monday, 15 2 2016, 14:33

Bash & PATH, order of execution

/etc/environment

/etc/profile

/etc/bash.bashrc

~/.profile

~/.bashrc

 Monday, 15 2 2016, 14:34

Interface DHCP

ifdown <ethx>

vim /eth/network/interfaces

# edit this file

auto <ethx>

iface <ethx> inet dhcp

ifup <ethx>

# bring the ethx up

 Monday, 15 2 2016, 14:36

Interface static

ifdown <ethx>

# bring down ethx

iface <ethx> inet static

# set config to static

address 10.11.12.13

netmask 255.255.255.0

gateway 10.11.12.1

ifup <ethx>

# bring the ethx up

 Monday, 15 2 2016, 14:39

Start process in background & unlinked to terminal

nohup <cmd> &

nohup <cmd> > /dev/null 2>&1 &

# redirect all output to /dev/null

<cmd> &

#start in background (not done yet...)

disown

# to unlink the above command from terminal

nohup [/path/to/play/app] -Dhttp.port=[port] -J-Xms128m -J-Xmx128m > [/path/to/log/file] &

# start play app in background & redirect output to log file

 Monday, 15 2 2016, 14:41

PID of process name & kill

pidof <name>

# use option -x for scripts

kill -9 $(pidof -x <name>)

# kill script pid with name

kill -SIGKILL $(pidof -x <name>)

# send SIGKILL signal to script process

 Monday, 15 2 2016, 14:43

SVN

svn checkout [URL] [folder]

# checkout from URL to folder

svn ce [URL] [folder]

# short form of checkout

svn commit

# commit current folder

svn ci -m "message"

# short form of commit plus message

svn status

# get the status

svn st

# short form for status

svn propset svn:ignore .

# set property

svn propget svn:ignore .

# get ignore property

svn propedit svn:ignore .

# edit the ignore property

svn add -N <folder>

#svn add only the top folder

svn add --force .

# add w/ excludes

svn st | grep ^! | awk '{print " --force "$2}' | xargs svn rm

# commit deleted

svn checkout svn://somepath@<revision number> <local-working-directory>

# checkout specific version from svn repo

 Monday, 15 2 2016, 14:50

Time & timezone

/usr/bin/ntpq -p

sntp -P no -r pool.ntp.org

hwclock --systohc

msntp -P no -r pool.ntp.org

# uses port 123

 Monday, 15 2 2016, 14:54

Repair / Reinstall Grub with Live CD

sudo mount /dev/sdXY /mnt

# mount the partition the ubuntu installation is on

sudo mount --bind /dev /mnt/dev && sudo mount --bind /dev/pts /mnt/dev/pts && sudo mount --bind /proc /mnt/proc && sudo mount --bind /sys /mnt/sys

# bind the directories that grub needs access to to detect other operating systems

sudo chroot /mnt

# jump into that using chroot

grub-install /dev/sdX

# install - only need to add the drive letter

grub-install --recheck /dev/sdX

# check

update-grub

# update

exit && sudo umount /mnt/sys && sudo umount /mnt/proc && sudo umount /mnt/dev/pts && sudo umount /mnt/dev && sudo umount /mnt

# exit chrooted & unmount everything

reboot

# restart and done

 Friday, 19 2 2016, 12:27

Add ssh root access

vim /etc/ssh/sshd_config

PermitRootLogin without-password

# find and comment out this line

PermitRootLogin yes

# add this line below the previous one

service ssh restart

# restart the service

 Tuesday, 23 2 2016, 11:38

Tcpdump ICMP

tcpdump -nni vlan111 -e icmp[icmptype] == 8

# check ICMP incomming connections

 Wednesday, 24 2 2016, 10:27

Install / uninstall dpkg

dpkg -i <package.deb>

# install the package

dpkg -r <installed-package>

# uninstall package

dpkg -l | grep <search>

# list installed packages and search for <search>

dpkg -P <installed-package>

# purge installed package (remove everything, including configs)

 Wednesday, 24 2 2016, 12:14

Get IP from ifconfig

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

# get ip one liner

 Thursday, 25 2 2016, 11:48

Check swap usage

vmstat 1

# si = swap in, so = swap out

 Tuesday, 01 3 2016, 15:32

Find ips from lan

apr -n

# list all ips connected in the same network

 Friday, 04 3 2016, 15:51

Search for text in files

grep -rnw '/path/to/somewhere/' -e "pattern"

# -r or -R = recursive, -n = line number, -w = match the whole word, -l (lower-case L) = just give the file name of matching files

grep --include=*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"

# only search through files with .c or .h extensions

grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"

# search through files other than .c or .h extensions

grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"

# exclude dirs (use --include-dir for including dirs)

 Wednesday, 09 3 2016, 11:45

Release code name

lsb_release -c -s

# get only the release code name (-c), short (-s)

 Thursday, 24 3 2016, 15:25

Get number of total threads

ps -elfT | wc -l

ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'

ps -T -p <PID>

# threads of process PID

 Tuesday, 12 4 2016, 08:58

Users: add/list/remove

useradd -m <username>

# adds the user, creates the home folder

adduser <username>

# more user friendly version for useradd; creates everything

cut -d: -f1 /etc/passwd

# list all users in the system

userdel <username>

# remove a user; does not remove home folder!

 Thursday, 05 5 2016, 09:20

No public key available apt-get error

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <missing-key>

# retrieve missing key from key server

 Friday, 06 5 2016, 15:31

Auto-mount ntfs partition

sudo fdisk -l

# find correct /dev/sdx name for the partition (by size for example)

sudo blkid

# find uuid - look at the /dev/sdx partition name from prev step

mkdir /home/<user>/media/win

# create empty folder to which to mount

sudo vim /etc/fstab

# edit file to add new mount point

UUID=<uuid> </mount/point> ntfs rw,auto,users,exec,nls=utf8,umask=003,gid=46,uid=1000 0 0

# add new line to fstab

sudo mount -a

# mount everything (no reboot if errors!)

replace UUID=<xxxx> with /dev/disk/by-uuid/<xxxxx>

# if duplicate devices show up

 Friday, 13 5 2016, 21:50

Start without xserver

sudo nano /etc/default/grub

# edit this file

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

# find this line

GRUB_CMDLINE_LINUX_DEFAULT="text"

# replace with this line

sudo update-grub

# run update grub & restart

sudo service lightdm start

# start xserver manually

 Friday, 13 5 2016, 21:53

Start without xerver - systems that use systemd

sudo systemctl enable multi-user.target --force

sudo systemctl set-default multi-user.target

# tell systemd to not load the graphical login manager

startx

# start x manually

 Friday, 20 5 2016, 10:33

Symlink

ln -s </path/to/existing/file> </path/to/symlink/file>

# add a new symlink to a file or folder

 Friday, 10 6 2016, 14:05

Scala app packer

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.1.1")

# add this first thing into file project/plugins.sbt

lazy val root = (project in file(".")).enablePlugins(JavaAppPackaging, UniversalPlugin)

# add this to build.sbt

sbt universal:packageBin

# run this in project folder

https://github.com/sbt/sbt-native-packager

# more info here

sbt stage

# create app locally, without packing (./target/universal/stage/bin/<your-app>)

 Friday, 10 6 2016, 14:48

sha password

sudo cat /etc/shadow

# show sha passwords for all users

mkpasswd --method=sha-512 --salt=B0qOSbDk <password>

# create password sha hash which I can compare to values in /etc/shadow

 Tuesday, 21 6 2016, 08:53

start / stop service

sudo update-rc.d -f apache2 disable

# disable example for apache

sudo update-rc.d -f apache2 enable

# enable example for apache

sudo update-rc.d -f apache2 defaults

# another way of enable, if the other one fails

echo 'manual' > /etc/init/mysqld.override

# for upstart systems (does not work on elementary)

echo "manual" >> /etc/init/myjob.conf

# another option for upstart systems (not elementary)

 Thursday, 23 6 2016, 12:53

git

git init

# init git in current folder - created subfolder .git

git remote

# show the remote repository aliases stored

git remote -v

# show the remote repository aliases and actual url for each alias

git remote add [alias] [url]

# adds the [url] under a local remote named [alias]

git remote rm [alias]

# remove remote alias

git remote rename [old-alias] [new-alias]

# rename an existing alias

git remote set-url [alias] [new-url]

# update a remote's url

git remote set-url --push [alias] [push-url]

# set the same remote to push to another url, while fetching from the initial one

git fetch [alias]

# download new branches and data from a repository

git pull [alias]

# fetch from a remote repo and try to merge into the current branch

git push [-u | --set-upstream] [alias] [branch]

# push your new branches and data to a remote repository; will attempt to make your [branch] the new [branch] on the [alias] remote

git clone [url | alias]

# copy a git repository so you can add to it

git add [list_of_files | folders]

# adds file contents to the staging area

git status [-s]

# view the status of files in the working directory and staging area

git commit [-m "message"]

# records a snapshop of the staging area

git commit -a [-m "message"]

# automatically stage all tracked, modified files before commit

git reset HEAD

# unstage files from index and reset pointer to HEAD

git reset --soft [commit refference]

# moves HEAD to specified commit refference (ex: HEAD), index and staging are untouched

git reset --hard [commit refference]

# unstage files AND undo any chages in the working directory since last commit

git rm [file]

# remove the file from the staging area entirely and also off the disk

git rm --cached [file]

# remove from staging entorely, but leave file in the working directory

git branch

# list available branches

git branch [branch name]

# create a new branch with the given name

git checkout [branch name]

# switch to the mentioned branch, if exists

git checkout -b [branch name]

# create the new branch and switch to it immediately

git branch -d [branch name]

# delete a branch

git push [remote-name] :[branch-name]

# delete a remote branch

git merge [branch-name]

# merge the branch context [branch-name] into the current one

http://gitref.org/

# more info here

git checkout <branch_name> <relative_path/to/file>

# checkout the specified file from the specified branch into current working branch

git push origin --delete <remote_branch_name>

# delete branch from remote repo

git branch -d <local_branch_name>

# delete local branch

 Tuesday, 28 6 2016, 09:28

Search for text in all files in path

grep -ril "text to find" /path/to/search/

# find the text in path; -i = case insensitive; -r = recursive; -l = list files instead of find results

-n

# line number

-w

# whole words

 Thursday, 07 7 2016, 15:29

Check network connectivity

nc -vzw1 <address> <port>

# check connectivity to <address> on <port>

nc -vz <address/host> <port>

# check connectivity with success / fail message

 Friday, 05 8 2016, 14:38

ssh tunnels

Host <host-name-to-use>

StrictHostKeyChecking no

User <username>

HostName <ip-name-host>

ProxyCommand ssh -q user@first-host nc -q0 %h 22

# add this in ./.ssh/config; it will ssh into the second server <ip-name-host>, by going through the first one <user@first-host>

ssh -L <local-port>:localhost:<remote-port> <remote-host>

# create a ssh tunnel from localhost to remote-host using specified ports (local and remote)

http://www.revsys.com/writings/quicktips/ssh-tunnel.html

# more info here

http://www.linuxhorizon.ro/ssh-tunnel.html

# more info here

ssh -L <local-port>:<remote-host>:<remote-port> user@<another-remote-host>

# create a ssh tunnel from remote-host:remote-port through another-remote-host to localhost:local-port

 Wednesday, 19 10 2016, 15:24

Fix system time offsets

sudo apt-get install ntp

# install ntp service (Network Time Protocol)

sudo service ntp stop

# stop the service for now

sudo ntpdate -s <time.server.org>

# sync with time server

sudo service ntp start

# restart the service

 Monday, 24 10 2016, 09:25

Import into mysql from sql file

mysql -u [user] -p < [/path/to/file.sql]

# that's it

 Monday, 05 12 2016, 10:37

Mount / umount iso image

sudo mount -o loop /path/to/iso/file /media/iso

# mounting an iso image

sudo umount /media/iso

# unmounting the mounted iso image

 Monday, 12 12 2016, 09:23

List, mount, unmount available partitions

sudo parted -l

# list hdds and partitions with details

sudo mount -t ntfs-3g -o ro </path/to/partition> </path/to/mountpoint>

# mount windows ntfs partition in read-only mode (works for hibernated ones)

 Tuesday, 27 12 2016, 19:54

Which process is listening on a port

sudo fuser -v 9000/tcp

# show which process is listening on a specified port (port 9000, tcp in this example)

 Monday, 16 1 2017, 11:51

Get full log since boot

journalctl -b

# shows boot messages

journalctl -b<number>

# shows from <number> boot, 0 = current, 1 = previous, etc.

journalctl -b0 SYSLOG_PID=1

# show logs from current boot and filters messages from PID 1 (init)

journalctl -b0 --system _COMM=systemd

# _COMM=systemd - looks for messages from systemd (a.k.a. init), --system - filters messages from the system log instead of user session logs

 Monday, 16 1 2017, 12:53

Generate plot with startup services

systemd-analyze plot > plot.svg

# generate times and save them to plot.svg (open in browser)

 Thursday, 09 3 2017, 15:05

Network time set

sudo apt-get install ntp

sudo apt-get install ntpdate

sudo service ntp stop

# stop the service

sudo ntpdate -s <time_server>

# update from time server (time01.mdc)

sudo service ntp start

# restart the service

 Monday, 19 6 2017, 07:22

List changed / updated file from package

dpkg-query -L <package_name>

# see all the files the package installed on the system

dpkg-deb -c <package_name.deb>

# see the files a deb will install

apt-file list <package_name>

# see the files contained in a package that is NOT installed