Linux: Difference between revisions

From Piszczynski
piszczynski>Aleks
No edit summary
No edit summary
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Basic Commands ==
=== Useful everyday commands ===
List directory contents:<syntaxhighlight lang="bash">
ls
in long format -l
list all -a
</syntaxhighlight>List block devices (all drives)<syntaxhighlight lang="bash">
lsblk
in list: -l
</syntaxhighlight>Get info on system<syntaxhighlight lang="bash">
uname -a
</syntaxhighlight>Change file mode bits (file permissions)<syntaxhighlight lang="bash">
chmod <options> <filename>
chmod 777 script.sh (gives read write execute permission to owner, group, world)
</syntaxhighlight>Change ownership of file<syntaxhighlight lang="bash">
chown <newowner>:<newgroup> <filename>
chown dave:users notes.txt
</syntaxhighlight>Compress file<syntaxhighlight lang="bash">
tar -zxvf <filename>
tat -zxvf something.tar.gz
extract:
    tar -xvf something.tar.gz
</syntaxhighlight>Display file contents or join files<syntaxhighlight lang="bash">
cat <filename>
join files:
    cat file1.txt file2.txt
combine files:
    cat file1.txt file2.txt >> file3.txt
</syntaxhighlight>
===Copy===
Copy a file:<syntaxhighlight lang="bash">cp <file> <Destination> </syntaxhighlight>
----
copy multiple files:<syntaxhighlight lang="bash">cp <file1> <file2> <Destination></syntaxhighlight>
----
Prevent overwriting: (will overwrite files by default)<syntaxhighlight lang="bash">cp -n <source> <dest> (can also use -i option to decide for each file)</syntaxhighlight>
----
Copy directory:<syntaxhighlight lang="bash">cp -r <source> <destination></syntaxhighlight>
----
Copy contents of directory to another directory:<syntaxhighlight lang="bash">cp <source>/. <destination></syntaxhighlight>
===Sort===
Pipe output to sort to sort it or display contents of file sorted:
<syntaxhighlight lang="bash"><command> | sort</syntaxhighlight>
<syntaxhighlight lang="bash">sort <options> <filename></syntaxhighlight>
----
Sort on numerical value:
<syntaxhighlight lang="bash">sort -n</syntaxhighlight>
----
sort in reverse order:
<syntaxhighlight lang="bash">sort -r</syntaxhighlight>
----
Sort by specific column (useful with commands that return data in columns like du)
<syntaxhighlight lang="bash">sort -k <column number></syntaxhighlight>
<syntaxhighlight lang="bash">sort -k 3nr (will sort by the 3rd column in reverse number order)</syntaxhighlight>
----
Sort and remove duplicates
<syntaxhighlight lang="bash">sort -u</syntaxhighlight>
----
Ignore case when sorting
<syntaxhighlight lang="bash">sort -f</syntaxhighlight>
----
sort by human numeric values:
<syntaxhighlight lang="bash">sort -h</syntaxhighlight>
----
Redirect output to another file:
<syntaxhighlight lang="bash">sort > <other file></syntaxhighlight>
===zip===
Decompress tar.gz files:
<syntaxhighlight lang="bash">tar –xvzf documents.tar.gz</syntaxhighlight>
== Get Linux Info ==
Get current build and Linux OS data:
<syntaxhighlight lang="bash">cat /etc/os-release</syntaxhighlight>Get init system the server uses<syntaxhighlight lang="bash">
ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1
</syntaxhighlight>


== Remote Desktop Protocol App for linux ==
== Remote Desktop Protocol App for linux ==
Line 6: Line 90:
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




== Change or Set IP address ==
With Newer Ubuntu versions netplan is used as the network management tool.
Get ip address and interfaces with:
<syntaxhighlight lang="bash">ifconfig</syntaxhighlight>
<syntaxhighlight lang="bash">ip -c a</syntaxhighlight>
<syntaxhighlight lang="bash">ip link</syntaxhighlight>
Set the network interface settings on the configuration files located in the netplan directory. Netplan will parse all the files in this directory:
<syntaxhighlight lang="bash">sudo nano /etc/netplan/<name of config file eg 00-installer-config.yaml></syntaxhighlight>
Example of the .yaml file:
<pre>
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
</pre>Apply the changes using netplan apply:<syntaxhighlight lang="bash">
sudo netplan apply
</syntaxhighlight>The use of gateway4: option to set the default gateway has ben depreciated so you need to set a default route with the following option now:<syntaxhighlight lang="bash">
routes:
  - to: default
    via: 192.168.50.1
</syntaxhighlight>


== Swap File ==
== Swap File ==


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
<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 #enable writing the swap file
<syntaxhighlight lang="bash">sudo chmod 0600 /swapfile #Assign it read/write permissions for root only</syntaxhighlight>
*sudo mkswap /swapfile #make swap file
<syntaxhighlight lang="bash">sudo mkswap /swapfile #Format the file as swap</syntaxhighlight>
*sudo swapon -a #enable swap file
<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
<syntaxhighlight lang="bash">sudo swapoff /swapfile</syntaxhighlight>
<syntaxhighlight lang="bash">sudo rm  /swapfile</syntaxhighlight>
<syntaxhighlight lang="bash">sudo fallocate -l 8G /swapfile</syntaxhighlight>
 
==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:
 
<syntaxhighlight lang="bash">chmod u+x <appimage file></syntaxhighlight>
 
==SSH==
===Enable SSH===
Install openssh server:
<syntaxhighlight lang="bash">sudo apt-get install openssh-server</syntaxhighlight>
 
login.
 
===Enable login using ssh Keys===
Generate keys: Be careful if you have already generated a key as this will overwrite the current key
<syntaxhighlight lang="bash">ssh-keygen</syntaxhighlight>
 
This gets stored in ~/.ssh
 
Copy key to server (must have ssh via password enabled)
<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:
<syntaxhighlight lang="bash">cat ~/.ssh/id_rsa.pub | ssh name@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"</syntaxhighlight>
 
Copy over manually:
 
 
Check public key
<syntaxhighlight lang="bash">cat ~/.ssh/id_rsa.pub</syntaxhighlight>
 
Make dir:
<syntaxhighlight lang="bash">mkdir -p ~/.ssh</syntaxhighlight>
 
copy key over:
<syntaxhighlight lang="bash">echo <public_key_string> >> ~/.ssh/authorized_keys</syntaxhighlight>
 
===Disable Password Authentication===
Edit config file:
<syntaxhighlight lang="bash">sudo nano /etc/ssh/sshd_config</syntaxhighlight>
 
Change:
<syntaxhighlight lang="bash">PasswordAuthentication no</syntaxhighlight>
 
Restart ssh service:
<syntaxhighlight lang="bash">sudo systemctl restart ssh</syntaxhighlight>
 
== Services ==
Show services:<syntaxhighlight lang="bash">
sudo systemctl status #show all services info
sudo systemctl | grep service #get all services by name
</syntaxhighlight>Set service to start on boot (linux systemd init)<syntaxhighlight lang="bash">
sudo systemctl start <service name>
</syntaxhighlight>
 
== Mount Network Share ==
Mount a network share using the mount command. You may need to install cifs:<syntaxhighlight lang="bash">
sudo apt install cifs-utils
</syntaxhighlight>then mount with this command:<syntaxhighlight lang="bash">
sudo mount -o user=<username> //<fqdn of network share>/<share folder> <directory to mount on server locally>
 
</syntaxhighlight>To automatically mount a share on reboot you need to add the details to the /etc/fstab file:<syntaxhighlight lang="bash">
#add at the bottom of the /etc/fstab file:
 
//<fqdn of file share server>/<filesharefolder> <local mount point folder> cifs user=<user>,password=<password>,uid=<uid of user to mount(can be the default user 1000)> 0 0
 
</syntaxhighlight>
 
== Manage Disks with LVM ==
display all of the available block storage devices that LVM can potentially manage, use the <code>lvmdiskscan</code> command:<syntaxhighlight lang="bash">
sudo lvmdiskscan
</syntaxhighlight>display all of the physical devices on your system by using <code>lvmdiskscan</code> with the <code>-l</code> option, which will only return physical volumes:<syntaxhighlight lang="bash">
sudo lvmdiskscan -l
</syntaxhighlight>The <code>pvscan</code> command is similar in that it searches all available devices for LVM physical volumes. The output format includes a small amount of additional information:<syntaxhighlight lang="bash">
sudo pvscan
</syntaxhighlight>can also use the pvs command to show useful info:<syntaxhighlight lang="bash">
sudo pvs
sudo pvdisplay
</syntaxhighlight>get info on lvm groups:<syntaxhighlight lang="bash">
sudo vgscan
sudo vgs -o +devices,lv_path
sudo vgdisplay -v
sudo lvscan
</syntaxhighlight>
 
== Control groups ==
List control groups:<syntaxhighlight lang="bash">
sudo systemd-cgls --no-pager
</syntaxhighlight>

Latest revision as of 15:16, 14 April 2024

Basic Commands

Useful everyday commands

List directory contents:

ls
in long format -l
list all -a

List block devices (all drives)

lsblk
in list: -l

Get info on system

uname -a

Change file mode bits (file permissions)

chmod <options> <filename>
chmod 777 script.sh (gives read write execute permission to owner, group, world)

Change ownership of file

chown <newowner>:<newgroup> <filename>
chown dave:users notes.txt

Compress file

tar -zxvf <filename>
tat -zxvf something.tar.gz
extract:
    tar -xvf something.tar.gz

Display file contents or join files

cat <filename>
join files:
    cat file1.txt file2.txt
combine files:
    cat file1.txt file2.txt >> file3.txt

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

Get init system the server uses

ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1ps --no-headers -o comm 1

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 -c a
ip link

Set the network interface settings on the configuration files located in the netplan directory. Netplan will parse all the files in this directory:

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

Apply the changes using netplan apply:

sudo netplan apply

The use of gateway4: option to set the default gateway has ben depreciated so you need to set a default route with the following option now:

routes:
  - to: default
    via: 192.168.50.1

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

Services

Show services:

sudo systemctl status #show all services info
sudo systemctl | grep service #get all services by name

Set service to start on boot (linux systemd init)

sudo systemctl start <service name>

Mount Network Share

Mount a network share using the mount command. You may need to install cifs:

sudo apt install cifs-utils

then mount with this command:

sudo mount -o user=<username> //<fqdn of network share>/<share folder> <directory to mount on server locally>

To automatically mount a share on reboot you need to add the details to the /etc/fstab file:

#add at the bottom of the /etc/fstab file:

//<fqdn of file share server>/<filesharefolder> <local mount point folder> cifs user=<user>,password=<password>,uid=<uid of user to mount(can be the default user 1000)> 0 0

Manage Disks with LVM

display all of the available block storage devices that LVM can potentially manage, use the lvmdiskscan command:

sudo lvmdiskscan

display all of the physical devices on your system by using lvmdiskscan with the -l option, which will only return physical volumes:

sudo lvmdiskscan -l

The pvscan command is similar in that it searches all available devices for LVM physical volumes. The output format includes a small amount of additional information:

sudo pvscan

can also use the pvs command to show useful info:

sudo pvs
sudo pvdisplay

get info on lvm groups:

sudo vgscan
sudo vgs -o +devices,lv_path
sudo vgdisplay -v
sudo lvscan

Control groups

List control groups:

sudo systemd-cgls --no-pager