Linux notes

 Monday, 15 2 2016, 15:24

Memory info

dmidecode --type 17

 Monday, 15 2 2016, 15: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, 15:30

Find distro

cat /etc/*-release

lsb_release -a

cat /proc/version

 Monday, 15 2 2016, 15:31

Find kernel version

uname -a

uname -mrs

 Monday, 15 2 2016, 15:31

Add to path

vim /etc/profile

# in this file

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

# add this line

 Monday, 15 2 2016, 15:33

Bash & PATH, order of execution

/etc/environment

/etc/profile

/etc/bash.bashrc

~/.profile

~/.bashrc

 Monday, 15 2 2016, 15: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, 15: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, 15: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, 15: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, 15: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, 15: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, 15: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, 13: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, 12:38

Tcpdump ICMP

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

# check ICMP incomming connections

 Wednesday, 24 2 2016, 11: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, 13: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, 12:48

Check swap usage

vmstat 1

# si = swap in, so = swap out

 Tuesday, 01 3 2016, 16:32

Find ips from lan

apr -n

# list all ips connected in the same network

 Friday, 04 3 2016, 16: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, 12:45

Release code name

lsb_release -c -s

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

 Thursday, 24 3 2016, 16: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, 10: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, 11: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, 17: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, 23: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, 23: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, 12: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, 16: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, 16: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, 10: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, 14: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, 11: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, 17: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, 16: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, 17: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, 11:25

Import into mysql from sql file

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

# that's it

 Monday, 05 12 2016, 11: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, 10: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, 20: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, 12: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, 13: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, 16: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, 09: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

 Tuesday, 28 11 2017, 17:46

Locale warning Perl

export LANGUAGE=en_US.UTF-8

export LC_ALL=en_US.UTF-8

locale-gen en_US.UTF-8

dpkg-reconfigure locales

#will open a dialog under Debian for selecting the desired locale. This dialog will not appear under Ubuntu.

AcceptEnv LANG LC_*

# comment this line in /etc/ssh/sshd_config so that it won't accept env vars

SendEnv LANG LC_*

# on client side, comment this line in /etc/ssh/ssh_config so that it won't send env var anymore

 Tuesday, 28 11 2017, 17:48

Set timezone

sudo timedatectl set-timezone <timeszone>

it's not interactive as dpkg-reconfigure tzdata

timedatectl list-timezones

list all timezones

echo "Australia/Adelaide" | sudo tee /etc/timezone

# manually change timezone in file

sudo dpkg-reconfigure --frontend noninteractive tzdata

 Wednesday, 06 12 2017, 16:09

Service status

sudo service --status-all

# for systemv

systemctl status <service_name>

# for systemd

 Wednesday, 06 12 2017, 17:32

Fix freezes in keyboard / mouse

lsusb

# list connected USB devices

lsusb -t

# get detailed port number of devices for each bus and device (bus and device numbers are known from lsusb)

ls /sys/bus/usb/drivers/usb

# check symlinks here: e.g. 3-13 is responsible for mouse (bus 3 port 13)

echo 2 | sudo tee /sys/bus/usb/drivers/usb/<bus-port-symlink>/power/autosuspend

# change autosuspend value from 0 to 2 (if it was 0)

echo on | sudo tee /sys/bus/usb/devices/<bus-port-symlink>/power/level >/dev/null

# set power to "on"

lsusb -v

# huge info about usb devices

evtest

# cool utility to see events realtime

 Thursday, 04 1 2018, 11:51

Check open ports on remote host

nc -zv <host> <port>

# using netcat, single port check

nc -zv <host> <port1> <port2> <port3>

# using netcat, multiple ports

nc -zv <host> <port1-port2>

# using netcat, range of ports

$ (echo > /dev/tcp/<host>/<port>) >/dev/null 2>&1 && echo "It's up" || echo "It's down"

# can use tcp or udp, details here: https://superuser.com/a/806331

 Thursday, 15 2 2018, 09:21

Fix Desktop (Plank + Wingpanel) slow initialize in Loki

mv /etc/xdg/autostart/at-spi-dbus-bus.desktop /etc/xdg/autostart/at-spi-dbus-bus.desktop.disabled

# rename / disable this file, seems that's the issue

 Wednesday, 07 3 2018, 09:47

Add certificates for chrome

echo QUIT | openssl s_client -connect <host>:<port> | sed -ne '/BEGIN CERT/,/END CERT/p' > <certfile_name>

# save the certificate; the echo QUIT is needed to end the cer reading

certutil -d sql:$HOME/.pki/nssdb -A -t "P,," -n <cert_tag> -i <certfile_name>

# add cert from file to db

certutil -d sql:$HOME/.pki/nssdb -L

# list certs in db - should see the added tags

apt install libnss3-tools

# this is needed for certutil

openssl x509 -outform der -in your-cert.pem -out your-cert.crt

# convert from pem to crt

openssl pkey -in mumble.pem -out mumble-key.pem

# extract the key in PKCS8 form

openssl rsa -in mumble.pem -out mumble-key.pem

# extract the key as RSA (for OpenSSL version older than 1.0.0)

openssl x509 -in file.pem -inform PEM -out file.crt

# convert from pem to crt - this actually works!

sudo mkdir /usr/share/ca-certificates/extra

# create folder for extra certificates

sudo cp ca.crt /usr/share/ca-certificates/extra/

# copy crt file to extra

sudo dpkg-reconfigure ca-certificates

# reconfigure, let ubuntu do the job and add the certificate (to /etc/ca-certificates.conf)

 Friday, 09 3 2018, 09:33

Sudo without password

sudo visudo

# opens file to edit

<username> ALL=(ALL) NOPASSWD: ALL

# add this to the end of the file, save and close

 Thursday, 29 3 2018, 09:39

Network manager cli

nmcli device show <eth-device>

# shows info about the specified eth

 Friday, 11 5 2018, 11:17

Import export mysql

mysqldump -u [uname] -p[pass] db_name > db_backup.sql

# export one db to file

mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql

# export all dbs to file

mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql

# export specific tables to file

mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql

# do it remotely

mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

# import into database from file

 Friday, 10 8 2018, 08:54

Fix / change sources list

sudo vim /etc/apt/sources.list

# edit sources list and change country (ro --> de, de--> ro, etc...)

https://launchpad.net/ubuntu/+archivemirrors

Official Archive Mirrors for Ubuntu - with status