GNU/Linux

All posts containing GNU/Linux-related scripts and patterns.

GIT Version Control Basics

Clone a Repository, add files, commit and push to master

 $>git clone ssh://git@gitlab.example.com:22/Project.git
 $>git add [filename|-A]
 $>git commit -a -m "Initial upload..."
 $>git push -u origin master

Create a branch for the new release and check-out to the new branch

 $>git branch release-4.1.11
 $>git checkout release-4.1.11

Make the changes to the files and commit the changes

 $>git commit -a -m "Change description..."

Mark as executable

$>git update-index --chmod=+x build.sh

Push to server

 $>git push -u origin release-4.1.11

Merge changes with master branch

A) Merge with master takes place on server. Pull new master branch from server...
$>git checkout master
$>git pull -a

B) Merge all changes to the master locally and push to server...
$>git checkout master
$>git merge release-4.1.11
$>git push -u origin master

Delete old branch

$>git branch -d release-4.1.11
* Option -D deletes a not merged branch...

Display commits and force to specific commit

$>git reflog
$>git reset --hard e072e63

GNU/Linux RegExp basics

RegExp basic encoding…

? = 0..1
* = 0..n
NULL  = 1..1
+ = 1..n
| = XOR
{3} = 3
{2,8} = 2-8 chars/numbers
^ = Matches beginning
$ = Matches end

Example RegExp

X-[0-9]?[0-9]-((0[1-9])|([1-2][0-9])|(3[0-2]))-[A-Z]{3}[0-9]{3,6}

Matching and Not-matching...
X-88-32-ABC123 -- MATCH
X-7-06-XYZ12345 -- MATCH
X-88-33-ABC123 -- NOTMATCH (33 not within 01-32)
X-88-32-ABC12 -- NOTMATCH (12 not 3-6 numbers)

GNU/Linux WGET Auth

With the wget example below, you can download a file when HTTP auth is required.

wget --http-user=<username> --http-password=<password> --ca-certificate='chain1.pem' 'https://www.example.com/site/file.bin' -O file.bin

Form-based authenticatie wget example using a session cookie…

wget --post-data='UserName=<username>&Password=<password>' --ca-certificate='chain2.pem' --cookies=on --keep-session-cookies --save-cookies=cookie.txt 'https://login.example.com/auth' -O result.txt

wget --referer='https://login.example.com/auth' --ca-certificate='chain2.pem' --cookies=on --keep-session-cookies --load-cookies=cookie.txt 'https://www.example.com/site/file.bin' -O file.bin

GNU/Linux basic VI commands

Text edit

a[text toevoegen na] i[text toevoegen voor] o[text onder toevoegen] O[text boven toevoegen] R[text overschrijven]

Text delete

d[getal+richting verwijderen] d$[verwijder text tot einde regel] dd[verwijder hele regel] x[verwijder huidig character] X[verwijder vorig character] J[koppel de huidige en de volgende regel aan elkaar]

Copy / Paste

y[getal+richting naar buffer kopieren] yw[woord naar buffer kopieren] p[plaats buffer na] P[plaats buffer voor]

Other

u[ongedaan maken] ~[hoofdletters/kleine letters]

File management

:wq[bestand opslaan en afsluiten :w[bestand opslaan] :q[afsluiten] :q![geforceerd afsluiten zonder opslaan]

 

LINUX Loop Device

You can create a loopback or iSCSI file using dd. The following example creates a file with an initial size o 0 bytes and a allocated size of 8Gb.

dd if=/dev/zero of=LUN3 bs=1000 count=0 seek=$[1000*1000*8]

Mount the file using the following command.

mount -o loop /path/to/file /path/to/mountpoint

 

OpenSSL Certificate Commands & Examples

Below script creates al selfgesigned certificate from a private key and removes the password from the key so you can you can use the key-pair in Apache.

# Create Key and Certificate Signing Request (option -des3 creates a triple des encrypted key)...
openssl genrsa -des3 -out server.key 4096
openssl req -new -key server.key -out server.csr

# Remove password from key and sign certificate with key... 
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

# Re-encrypt private key...
openssl rsa -des -in server.key.org -out server.key
openssl rsa -aes256 -in server.key.org -out server.key

For ease you can add all key and signing options to a config file. This way you can also add a subjectAlternate to the certificate.

>openssl req -new -config server.cnf -key server.key -out server.csr

# server.cnf #
[ req ]
default_bits = 4096
prompt = no
encrypt_key = no
distinguished_name = dn
req_extensions = req_ext

[ dn ]
C = NL
O = Totietoot
CN = examplefqdn.totietoot.nl

[ req_ext ]
subjectAltName = DNS:examplefqdn.totietoot.nl, DNS:examplealtname.totietoot.nl

Use the following command to convert the key-pair to pkcs12 format.

openssl pkcs12 -export -in server.crt -inkey server.key [-name tomcat] -out server.p12 -CAfile chain.pem -caname root -chain

Convert pkcs12 file to java keystore (jks):

keytool -importkeystore -deststorepass <password> -destkeypass <password> -destkeystore server.jks -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass <password> -alias <name>

Decode a certificate request or a x509 certificate:

openssl req -in server.csr -noout -text
openssl x509 -in server.crt -noout -text

Convert a PFX file to PEM-format (single file)…

openssl pkcs12 -in server.pfx -out key-n-certs.pem -nodes

Check certificate and connection using openssl…

openssl s_client -showcerts -connect f.q.d.n:1234
openssl s_client -starttls smtp -showcerts -connect f.q.d.n:25 -servername f.q.d.n

Convert certificate (PEM) to public key…

openssl x509 -inform pem -in certificate.cer -pubkey -noout > pubkey.pem

Add a (CA) certificate to the JAVA CACerts certificate truststore…

"C:\Java\jdk1.8.0_121\bin\keytool" -import -alias ADCERT-CA-1 -keystore "C:\Java\jdk1.8.0_121\jre\lib\security\cacerts" -trustcacerts -file ADCERT-CA-1.cer