Linux: Difference between revisions

From Piszczynski
No edit summary
Line 15: Line 15:
===Sort===
===Sort===
Pipe output to sort to sort it or display contents of file sorted:
Pipe output to sort to sort it or display contents of file sorted:
*<command> | sort
<syntaxhighlight lang="bash"><command> | sort</syntaxhighlight>
*sort <options> <filename>
<syntaxhighlight lang="bash">sort <options> <filename></syntaxhighlight>
----
----
Sort on numerical value:
Sort on numerical value:
*sort -n
<syntaxhighlight lang="bash">sort -n</syntaxhighlight>
----
----
sort in reverse order:
sort in reverse order:
*sort -r
<syntaxhighlight lang="bash">sort -r</syntaxhighlight>
----
----
Sort by specific column (useful with commands that return data in columns like du)
Sort by specific column (useful with commands that return data in columns like du)
*sort -k <column number>
<syntaxhighlight lang="bash">sort -k <column number></syntaxhighlight>
*sort -k 3nr (will sort by the 3rd column in reverse number order)
<syntaxhighlight lang="bash">sort -k 3nr (will sort by the 3rd column in reverse number order)</syntaxhighlight>
----
----
Sort and remove duplicates  
Sort and remove duplicates  
*sort -u
<syntaxhighlight lang="bash">sort -u</syntaxhighlight>
----
----
Ignore case when sorting  
Ignore case when sorting  
*sort -f
<syntaxhighlight lang="bash">sort -f</syntaxhighlight>
----
----
sort by human numeric values:
sort by human numeric values:
*sort -h
<syntaxhighlight lang="bash">sort -h</syntaxhighlight>
----
----
Redirect output to another file:
Redirect output to another file:
*sort > <other file>
<syntaxhighlight lang="bash">sort > <other file></syntaxhighlight>


===zip===
===zip===
Decompress tar.gz files:
Decompress tar.gz files:
*tar –xvzf documents.tar.gz
<syntaxhighlight lang="bash">tar –xvzf documents.tar.gz</syntaxhighlight>


== Get Linux Info ==
== Get Linux Info ==
Get current build and Linux OS data:
Get current build and Linux OS data:
*cat /etc/os-release
<syntaxhighlight lang="bash">cat /etc/os-release</syntaxhighlight>


== Remote Desktop Protocol App for linux ==
== Remote Desktop Protocol App for linux ==
Line 54: Line 54:
To user also need to install xrdp and tightvnc:
To user also need to install xrdp and tightvnc:


*sudo apt install xrdp xorgxrdp -y
<syntaxhighlight lang="bash">sudo apt install xrdp xorgxrdp -y</syntaxhighlight>
*echo env -u SESSION_MANAGER -u DBUS_SESSION_BUS_ADDRESS cinnamon-session>~/.xsession
<syntaxhighlight lang="bash">echo env -u SESSION_MANAGER -u DBUS_SESSION_BUS_ADDRESS cinnamon-session>~/.xsession</syntaxhighlight>


Other newer option is xserver-xorg-core
Other newer option is xserver-xorg-core
Line 65: Line 65:


Get ip address and interfaces with:
Get ip address and interfaces with:
*'''ifconfig'''
<syntaxhighlight lang="bash">ifconfig</syntaxhighlight>
*'''ip -a'''
<syntaxhighlight lang="bash">ip -a</syntaxhighlight>
*'''ip link'''
<syntaxhighlight lang="bash">ip link</syntaxhighlight>


Set the network interface settings on the configuration file:
Set the network interface settings on the configuration file:
*'''sudo nano /etc/netplan/<name of config file eg 00-installer-config.yaml>'''
<syntaxhighlight lang="bash">sudo nano /etc/netplan/<name of config file eg 00-installer-config.yaml></syntaxhighlight>


Example of the .yaml file:
Example of the .yaml file:
Line 89: Line 89:


Check size of swap file:  
Check size of swap file:  
*swapon -s
<syntaxhighlight lang="bash">swapon -s</syntaxhighlight>


increase swap file to 8GB:
increase swap file to 8GB:


*sudo swapoff -a #turn off swap file
<syntaxhighlight lang="bash">sudo swapoff -a #turn off swap file</syntaxhighlight>
*sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 #set size of blocks for swap file and create
<syntaxhighlight lang="bash">sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 #set size of blocks for swap file and create</syntaxhighlight>
*sudo chmod 0600 /swapfile #Assign it read/write permissions for root only
<syntaxhighlight lang="bash">sudo chmod 0600 /swapfile #Assign it read/write permissions for root only</syntaxhighlight>
*sudo mkswap /swapfile #Format the file as swap
<syntaxhighlight lang="bash">sudo mkswap /swapfile #Format the file as swap</syntaxhighlight>
*sudo swapon -a #enable swap file - will also be activated on next reboot without this command
<syntaxhighlight lang="bash">sudo swapon -a #enable swap file - will also be activated on next reboot without this command</syntaxhighlight>




Alternate commands to make 8GB swap file
Alternate commands to make 8GB swap file
*sudo swapoff /swapfile
<syntaxhighlight lang="bash">sudo swapoff /swapfile</syntaxhighlight>
*sudo rm  /swapfile
<syntaxhighlight lang="bash">sudo rm  /swapfile</syntaxhighlight>
*sudo fallocate -l 8G /swapfile
<syntaxhighlight lang="bash">sudo fallocate -l 8G /swapfile</syntaxhighlight>


==App Image==
==App Image==
Line 111: Line 111:
You will need to make the appimage executable to be able to run it:
You will need to make the appimage executable to be able to run it:


*'''chmod u+x <appimage file>'''
<syntaxhighlight lang="bash">chmod u+x <appimage file></syntaxhighlight>


==SSH==
==SSH==
===Enable SSH===
===Enable SSH===
Install openssh server:
Install openssh server:
*sudo apt-get install openssh-server
<syntaxhighlight lang="bash">sudo apt-get install openssh-server</syntaxhighlight>


login.
login.
Line 122: Line 122:
===Enable login using ssh Keys===
===Enable login using ssh Keys===
Generate keys: Be careful if you have already generated a key as this will overwrite the current key  
Generate keys: Be careful if you have already generated a key as this will overwrite the current key  
*ssh-keygen
<syntaxhighlight lang="bash">ssh-keygen</syntaxhighlight>


This gets stored in ~/.ssh
This gets stored in ~/.ssh


Copy key to server (must have ssh via password enabled)
Copy key to server (must have ssh via password enabled)
*ssh-copy-id name@server
<syntaxhighlight lang="bash">ssh-copy-id name@server</syntaxhighlight>


This gets sored on remote server in ~/.ssh/authorized_keys file. you can append keys to this fille to add more logins:
This gets sored on remote server in ~/.ssh/authorized_keys file. you can append keys to this fille to add more logins:
*cat ~/.ssh/id_rsa.pub | ssh name@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
<syntaxhighlight lang="bash">cat ~/.ssh/id_rsa.pub | ssh name@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"</syntaxhighlight>


Copy over manually:
Copy over manually:
Line 136: Line 136:


Check public key
Check public key
*cat ~/.ssh/id_rsa.pub
<syntaxhighlight lang="bash">cat ~/.ssh/id_rsa.pub</syntaxhighlight>


Make dir:
Make dir:
*mkdir -p ~/.ssh
<syntaxhighlight lang="bash">mkdir -p ~/.ssh</syntaxhighlight>


copy key over:
copy key over:
*echo <public_key_string> >> ~/.ssh/authorized_keys
<syntaxhighlight lang="bash">echo <public_key_string> >> ~/.ssh/authorized_keys</syntaxhighlight>


===Disable Password Authentication===
===Disable Password Authentication===
Edit config file:
Edit config file:
*sudo nano /etc/ssh/sshd_config
<syntaxhighlight lang="bash">sudo nano /etc/ssh/sshd_config</syntaxhighlight>


Change:
Change:
*PasswordAuthentication no
<syntaxhighlight lang="bash">PasswordAuthentication no</syntaxhighlight>


Restart ssh service:
Restart ssh service:
*sudo systemctl restart ssh
<syntaxhighlight lang="bash">sudo systemctl restart ssh</syntaxhighlight>

Revision as of 11:55, 16 November 2023

Basic Commands

Copy

Copy a file:

cp <file> <Destination>

copy multiple files:

cp <file1> <file2> <Destination>

Prevent overwriting: (will overwrite files by default)

cp -n <source> <dest> (can also use -i option to decide for each file)

Copy directory:

cp -r <source> <destination>

Copy contents of directory to another directory:

cp <source>/. <destination>

Sort

Pipe output to sort to sort it or display contents of file sorted:

<command> | sort
sort <options> <filename>

Sort on numerical value:

sort -n

sort in reverse order:

sort -r

Sort by specific column (useful with commands that return data in columns like du)

sort -k <column number>
sort -k 3nr (will sort by the 3rd column in reverse number order)

Sort and remove duplicates

sort -u

Ignore case when sorting

sort -f

sort by human numeric values:

sort -h

Redirect output to another file:

sort > <other file>

zip

Decompress tar.gz files:

tar –xvzf documents.tar.gz

Get Linux Info

Get current build and Linux OS data:

cat /etc/os-release

Remote Desktop Protocol App for linux

Remmina can be used for RDP connections to windows computers using RDP as well as VNC SSH and others


To user also need to install xrdp and tightvnc:

sudo apt install xrdp xorgxrdp -y
echo env -u SESSION_MANAGER -u DBUS_SESSION_BUS_ADDRESS cinnamon-session>~/.xsession

Other newer option is xserver-xorg-core


Change or Set IP address

With Newer Ubuntu versions netplan is used as the network management tool.

Get ip address and interfaces with:

ifconfig
ip -a
ip link

Set the network interface settings on the configuration file:

sudo nano /etc/netplan/<name of config file eg 00-installer-config.yaml>

Example of the .yaml file:

network:
  ethernets:
    ens160:
      dhcp4: no
      addresses:
        - 192.168.50.15/24
      gateway4: 192.168.50.1
      nameservers:
        addresses: [192.168.50.3, 1.1.1.1]
  version: 2

Swap File

Check size of swap file:

swapon -s

increase swap file to 8GB:

sudo swapoff -a #turn off swap file
sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 #set size of blocks for swap file and create
sudo chmod 0600 /swapfile #Assign it read/write permissions for root only
sudo mkswap /swapfile #Format the file as swap
sudo swapon -a #enable swap file - will also be activated on next reboot without this command


Alternate commands to make 8GB swap file

sudo swapoff /swapfile
sudo rm  /swapfile
sudo fallocate -l 8G /swapfile

App Image

Apps can be downloaded as an appimage which is the application with everythign it needs to run in one file.

You will need to make the appimage executable to be able to run it:

chmod u+x <appimage file>

SSH

Enable SSH

Install openssh server:

sudo apt-get install openssh-server

login.

Enable login using ssh Keys

Generate keys: Be careful if you have already generated a key as this will overwrite the current key

ssh-keygen

This gets stored in ~/.ssh

Copy key to server (must have ssh via password enabled)

ssh-copy-id name@server

This gets sored on remote server in ~/.ssh/authorized_keys file. you can append keys to this fille to add more logins:

cat ~/.ssh/id_rsa.pub | ssh name@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Copy over manually:


Check public key

cat ~/.ssh/id_rsa.pub

Make dir:

mkdir -p ~/.ssh

copy key over:

echo <public_key_string> >> ~/.ssh/authorized_keys

Disable Password Authentication

Edit config file:

sudo nano /etc/ssh/sshd_config

Change:

PasswordAuthentication no

Restart ssh service:

sudo systemctl restart ssh