Linux
A collection of notes on using Linux systems, in particular Ubuntu.
Basic Terminal Commands
Disk Space
du
Disk Usagedu -sh
Show size of current directorydu -h --max-depth=1
- Flags:
-h
human readable (adds M or G)
df
Disk Filesystems- Shows usage, total space available, and mount position
df -Ph .
See free space in current directory
Package Management
List all installed packages
# List everything
sudo dpkg -l
# List things with apache in the name
sudo dpkg -l | grep apache
Repositories
Repos are stored in
/etc/apt/sources.list
- A file in
/etc/apt/sources.list.d/
apt
apt list --installed
SSH
SSH Keys
Generate an ssh-key for every client
ssh-keygen -t ed25519 -a 100 [-C "comment your client name"] [-f output_path]
Some older software such as Solid file explorer require RSA keys in PEM key format
ssh-keygen -t rsa -b 4096 -a 100 -m PEM [-C "comment your client name"] [-f output_path]
You can also convert existing keys to PEM format
ssh-keygen -p -m PEM [-C "comment your client name"] [-f output_path]
Manage ssh keys
# On the client
ssh-copy-id <host>
# On the server
vim ~/.ssh/authorized_keys
Notes:
- According to this you should avoid using ECDSA and DSA keys.
Port Forwarding
Also: SSH Tunneling, SSH Proxy, SSH Reverse Proxy
If you need to access a port on the remote computer, you can use the -L
option to forward ports from the remote to the local machine.
ssh -L <localport>:localhost:<remoteport> <remoteurl>
# E.g. ssh -L 8080:localhost:80 [email protected]
You can also do the reverse, giving the remote access to a local port using -R
ssh -R <localport>:host:<remoteport> <remoteurl>
# E.g. ssh -R 8080:localhost:80 [email protected]
alias
You can create aliases in your .ssh/config
Host my_alias User my_username Hostname my_server@my_domain.com Port 52
VNC
x11vnc
Reference I recommend not exposing VNC. Set it to localhost only and use ssh port forwarding.
Remmina
How to use Remmina to connect to over ssh tunnel
Nvidia
Driver Installation
- Run
ubuntu-drivers list
to get a list of drivers - Install the latest driver
- E.g.
sudo apt install nvidia-driver-440
- E.g.
- If you have secure boot enabled, you will be asked for a password during installation
- This is because the driver is a DKMS module.
- After installation, reboot your computer and select "Enroll MOK" and enter that password in.
- Note Failure to do this will result in the driver not working
- Validate your installation by running
nvidia-smi
Cuda Installation
Download cuda from the nvidia website.
Switching between Nvidia and Intel
Reference
Make sure the Nvidia graphics drivers are installed. Then you can select between Nvidia and Intel GPUs using the Nvidia X Server Settings application nvidia-settings
. Alternatively, you can use the following commands in the terminal.
To switch to the Nvidia GPU:
sudo prime-select nvidia
To switch back to the Intel GPU:
sudo prime-select intel
prime-select query
will print either nvidia
or intel
to stdout.
Environment Variables
Tmux
Tmux cheat sheet
Tmux, or Terminal Multiplexer is an alternative to screen.
Use it to keep terminals open and tasks running after you disconnect your SSH connection.
Getting Started:
# Make a new session
tmux
# Make a new named session
tmux new -s my_session
# Rename a session
# Keybinding: Ctrl + b, $
tmux rename-session [-t current-name] [new-name]
# Detach from a session
# Keybinding: Ctrl + b, d
tmux detach
# List windows
tmux ls
# Attach to a session
tmux attach -t my_session
File Manager
Etcher
Github
Installing etcher
echo "deb https://deb.etcher.io stable etcher" | sudo tee /etc/apt/sources.list.d/balena-etcher.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61
sudo apt update
sudo apt install balena-etcher-electron
Logs
Logs are stored under /var/log
. These can end up taking up a lot of space.
You can delete logs in the journal folder Reference
Default gcc/g++ version
See https://askubuntu.com/questions/26498/how-to-choose-the-default-gcc-and-g-version.
# Install
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 20
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++
# Select
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
Power Management
tlp
Website
Battery power management
Virtual Machines (VM)
Using Ubuntu as a guest:
- Install
open-vm-tools-desktop
Services and Scheduling
crontab
systemd service
# Contents of /etc/systemd/system/myservice.service [Unit] Description=My Service After=network.target [Service] Type=simple Restart=always WorkingDirectory=/usr/local/bin ExecStart=/usr/local/bin/myservice [Install] WantedBy=multi-user.target
Enable with sudo systemctl enable myservice
- Notes
- Type should be
forking
if your service runs and then ends - See service log with
sudo journalctl myservice
File Management
rsync
Documentation
Use this to sync folders between directories of across networks
- Common Flags
-a, --archive
archive mode; equals -rlptgoD
rclone
Similar to rsync but for cloud services such as Dropbox and Google Drive.
I recommend installing from their website to get the latest version.
scp
Usage
scp [source_machine]:[source_file] [target_machine]:[target_file]
- Flags
-r
recursive, needed to scp directories-P [port]
- Notes
- The machine can be an alias or user@domain
7z
7zip CLI
Install with sudo apt install p7zip-full
# Archive
7z a <output_file> <input_file/folder>
# Extract
7z x <file> [-o{dir}]
zip/unzip
Note that p7zip-full also includes the ability to zip/unzip .zip files.
- Zip a folder
zip -r file.zip folder
- Unzip an archive
unzip file.zip [-d destination]
Dual Booting
Fix time difference between Windows
timedatectl set-local-rtc 1 --adjust-system-clock
Recover GRUB after installing Windows
Ubuntu Help
If you install windows after installing Ubuntu
GrubReboot
GrubReboot
Allows you to reboot into an OS one time.
i.e. If you are ssh'd into linux and want to boot into Windows one time.
Users and Groups
Groups
# List members in groups
getent group groupname
Permissions
In unix filesystems, files and folders have individual permissions.
You can set permissions for each file/folder independently and for the following sets of users:
- User/Owner
u
- Group
g
- Other
o
You can also set permissions for all of the above with:
- All
a
Each file and folder can have the following permission for each set of user:
- Read
r
- Write
w
- Execute
x
The above totals 9 bits (3 sets of users times 3 permissions).
In addition to the above, there are 3 special bits:
- Sticky bit
t
- only allow the owners of subfiles/subfolders to modify them- Useful for shared folders such as /tmp
- Setuid - automatically elevate execution of this file to the owner's priviledges
- Setgid - automatically elevate execution of this file to the group's priviledges
In total, permissions for each file and folder can be stored in 16 bits or 2 bytes.
chmod
chown
chgrp
OpenVPN
See https://help.ubuntu.com/lts/serverguide/openvpn.html
- Notes
- The
client.conf
file can be renamed toclient.ovpn
Display Scaling
See Arch Wiki HiDPI
- Xorg
# Find your display xrandr xrandr --output <display> --scale 1.25x1.25
- Wayland
gsettings set org.gnome.mutter experimental-features "['scale-monitor-framebuffer']"
I have the following script run at startup
#!/bin/bash gsettings set org.gnome.desktop.interface scaling-factor 2 gsettings set org.gnome.settings-daemon.plugins.xsettings overrides "{'Gdk/WindowScalingFactor': <2>}" xrandr --output DP-2 --scale 1.3x1.3
Clock
See Ubuntu Time Synchronization
# Install chrony sudo apt install chrony # Synchronize time sudo chronyd -q # Check time synchronization sudo chronyd -Q
Notes
- Syncing over the internet will be off by a few milliseconds (e.g. 0.003 seconds).
- Syncing with another computer over lan
Syncing with another computer
See askubuntu
- On the server
Add the following to /etc/chrony.conf
# make it serve time even if it is not synced (as it can't reach out) local stratum 8 # allow the IP of your peer to connect (192.168 subnet) allow 192.168 # Or # allow all
- On the client
Add the following to /etc/chrony.conf
# set the servers IP here to sync to it server <Server_IP> iburst # remove the default servers in the config