owned this note
owned this note
Published
Linked with GitHub
---
title: Linux use
tags: linux
---
:::spoiler Table Of Content
[TOC]
:::
# Installation and settings
建議使用 Ubuntu Linux 20.04 LTS (或版本更高的發行套件),Linux 核心必須在 v5.4 以上,否則課程提供的範例和作業無法正確執行。
- Try installation on Apple MacBook by [multipass](https://multipass.run/docs/installing-on-macos?fbclid=IwAR32IXSEvGkjrWTdig7smGjm3Rnt3LFVFeSz3Nm1vsq_adInMrBsLGcgnPY) project
- 步驟 from teacher:
`brew install multipass # 安裝套件`
`multipass launch -c 2 -d 10G -m 2G -n ubuntu # 建立名為 ubuntu 的虛擬機器環境,其中 -c 表示 CPU 數量、-d 表示儲存裝置的容量、-m 表示主記憶體空間`
`multipass shell ubuntu # 一旦上述的虛擬機器環境建立,透過本命令即可存取到 Ubuntu Linux`
`uname -a # 取得 Ubuntu Linux 核心版本資訊`
`lsb_release -a # 取得 Distro資訊,如:Ubuntu release`
- Update
- On linux2022q1
- Install the `Ubuntu Linux` along with `macOS` to my wife's [MacBook Air 2014 Early](https://support.apple.com/kb/SP700?locale=zh_TW)
- I followed the [installation guide](https://www.lifewire.com/dual-boot-linux-and-mac-os-4125733)
> :warning: **use a USB Flash Drive which has full permission to read or write**, otherwise, Dual-Boot Manager may not recognize the Ubuntu image correctly.
## Set `prompt` (提示;提詞)
### For GIT
> Reference the offical doc: [A1.6 Appendix A: Git in Other Environments - Git in Bash](https://git-scm.com/book/en/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-Bash)
:notes: Points:
1. Copy the prepared scripts, `git-completion.sh` and `git-prompt.sh` from *contrib/completion/.* in Git repo somewhere handy
> I put them in my home directory: *[~/Scripts](https://github.com/asahsieh/my-os-home-dir/tree/master/Scripts)*
2. Note that to enclose the value assigned to `PS1` by 'single quote', inorder to evaluate `$(__git_ps1 " (%s)")` correctly
- [ ] Why?
- :thinking_face: The difference between 'single quote' and "double quote":
> reference [QUOTING](https://man7.org/linux/man-pages/man1/bash.1.html#QUOTING) in `man bash`
- Enclosing characters in **single quotes** preserves ++the literal value of each character++ within the quotes.
- Enclosing characters in **double quotes** preserves the literal value of all characters within the quotes, with the exception of ==\$, \`, \\,== and, ==when history expansion is enabled, !==.
Other references
- [6.9 Controlling the Prompt](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt) in the `Bash Reference Manual`
- Clear explaination in [4.7 Git prompt](https://wiki.archlinux.org/title/git#Git_prompt) in `ArchWiki`
## Shell
### bash-it
#### installed plugins
- https://github.com/romkatv/gitstatus
- https://github.com/clvv/fasd
## Set Display to XQuartz
> https://www.reddit.com/r/osx/comments/y6e59/xquartz_and_apparently_incorrectly_set_display/
- Add the `XQuartz` to the Login Items

# Use
## Terminal
- [5 ways to navigate the Linux terminal faster](https://www.redhat.com/sysadmin/shortcuts-command-line-navigation)
- [Command Line Editing](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_toc.html#TOC242)
### Backspace becomes Space key in GDB
- [Backspace, Tab not working in terminal (using ssh) Stack Exchange https://unix.stackexchange.com › questions › backspac...](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjymfLHrsL-AhXFFogKHYVRDs4QFnoECBMQAQ&url=https%3A%2F%2Funix.stackexchange.com%2Fquestions%2F43103%2Fbackspace-tab-not-working-in-terminal-using-ssh&usg=AOvVaw1agSd98iXqkE_1hXeubtiD)
- In the terminal, press Ctrl + v and press your backspace. You'll see what code is sent as "erase". ... From iTerm (or another terminal app).
`Ctrl + v` + `Backspace` --> `^?`
- [Help! Backspace Doesn't Work in the Linux Terminal](https://www.makeuseof.com/backspace-not-working-in-linux-terminal/)
Modern terminals use "Ctrl + ?" for Backspace. Most systems will recognize the latter as Backspace
## Editors
- [VIM](/sfjW7NLjR9CGTfjEOCsqrQ)
## Remotely connect the server from MAC OS X
### by `iTerm2`
- :notes: `ls` output does not have colors on different types
*Original*:

:bulb: I google "iterm2 ssh no color", and was inspired from [a solution](https://apple.stackexchange.com/a/51307):
:::info
- Go to `references window -> profiles -> Default -> Colors`
- Go to the list of `Color Presets...` and change the upper item from `Solarized` to `Regular` or `High Contrast`; the following picture shows the result of selecting `Regular` item:

:::
## SSH
### Run graphical programs remotely
Ref.: [How to install X Window System XQuartz on macOS for ssh X11 forwarding](https://www.cyberciti.biz/faq/apple-osx-mountain-lion-mavericks-install-xquartz-server/)
- :question: hung on executing the app after connecting server by `ssh -Y the_server`:
- [x] :thinking_face: (guess): the `localhost` and the offset `11` were missed in my $DISPLAY
- *expected output*:

- *output on my server:*
```shell
[10:58] asahsieh@asahsieh-MacBookAir:~$ echo $DISPLAY
:0
```
> Try to update the *environment variable* `$DISPLAY` but the system still hung on the app
> > but the issue is not the *root cause*, please check the next item [color=red]
- [x] Try to debug on `ssh`
> ref.: [X11 forwarding requested but DISPLAY not set](https://stackoverflow.com/questions/63626203/x11-forwarding-requested-but-display-not-set)
- append `vvv` option
- and I found my issue is the same as the question: `X11 forwarding requested but DISPLAY not set`
Follow [one](https://stackoverflow.com/a/64106469/3401549) of the answers, the points are:
- **Make sure the *X11 display number***
--> Your `DISPLAY` on ==the local machine== (\*NOT\* on the server side) is almost always `:0.0`
- **Make sure you can run X11 applications on your Mac alone first**
--> You may need to install [XQuartz](https://www.xquartz.org/).
- ~~**Execute `ssh -X` within `xterm` window from `XQuartz`**~~
> updated: no need to do this step
- **For better use**
1. no need to pass the `-X` option ssh command:
Edit [your ~/.ssh/config file](https://www.cyberciti.biz//www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/):
```shell
ForwardX11 yes
```
2. you can run ssh in the background after running a GUI app as follows
```shell
ssh user@server -f -X app-Name
ssh nixcraft@nas01 -f -X gpass
```
- :notes: *On virtual machine*: [Can-not-open-display-on-gitg](https://hackmd.io/@asahsieh/git#Can-not-open-display-on-gitg)
### [SSH ProxyCommand example: Going through one host to reach another server](https://www.cyberciti.biz/faq/linux-unix-ssh-proxycommand-passing-through-one-host-gateway-server/)
## Free up Space
> Ref.:
> - https://itsfoss.com/free-up-space-ubuntu-linux/
> - https://www.fosslinux.com/2986/free-up-hard-disk-space-ubuntu.htm
- Use `Stacer` – GUI based System Optimizer
### Check use percentage of mounted devices
`df -h`
## Migrate or Back home directory
> ref.: [[Partitioning](https://help.ubuntu.com/community/Partitioning)/[Home](https://help.ubuntu.com/community/Partitioning/Home)/[Moving](https://help.ubuntu.com/community/Partitioning/Home/Moving)](https://help.ubuntu.com/community/Partitioning/Home/Moving)
1. (Option) Mount an SD card
> [How to Mount an SD Card in Linux](https://www.techwalla.com/articles/how-to-mount-an-sd-card-in-linux)
> [How do I mount an SD card?](https://askubuntu.com/questions/95391/how-do-i-mount-an-sd-card)
:::warning
- [ ] [Ubuntu doesn't read SD Card after wake](https://askubuntu.com/questions/911078/ubuntu-doesnt-read-sd-card-after-wake)
--> solve this issue by `autofs`?
:::
2. Find the mount point for the SD card
- by `lsblk`
```shell
[13:10] asahsieh@asahsieh-MacBookAir:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 131.8M 1 loop /snap/cmake/1035
loop1 7:1 0 209.1M 1 loop /snap/code/91
loop2 7:2 0 65.2M 1 loop /snap/gtk-common-themes/1519
loop3 7:3 0 248.8M 1 loop /snap/gnome-3-38-2004/99
loop4 7:4 0 110.8M 1 loop /snap/core/12725
loop5 7:5 0 55.5M 1 loop /snap/core18/2344
loop6 7:6 0 22.4M 1 loop /snap/universal-ctags/892
loop7 7:7 0 61.9M 1 loop /snap/core20/1361
loop8 7:8 0 55.5M 1 loop /snap/core18/2284
loop9 7:9 0 323.5M 1 loop /snap/kde-frameworks-5-qt-5-15-core20
loop10 7:10 0 4K 1 loop /snap/bare/5
loop11 7:11 0 155.4M 1 loop /snap/firefox/1073
loop12 7:12 0 242.3M 1 loop /snap/gnome-3-38-2004/76
loop13 7:13 0 9.5M 1 loop /snap/htop/3359
loop14 7:14 0 617M 1 loop /snap/libreoffice/242
loop15 7:15 0 61.9M 1 loop /snap/core20/1376
loop16 7:16 0 15.7M 1 loop /snap/universal-ctags/882
loop17 7:17 0 110.7M 1 loop /snap/core/12821
loop18 7:18 0 54.2M 1 loop /snap/snap-store/557
loop19 7:19 0 625.3M 1 loop /snap/libreoffice/247
loop20 7:20 0 295.7M 1 loop /snap/vlc/2344
loop21 7:21 0 150.2M 1 loop /snap/okular/109
loop22 7:22 0 155.3M 1 loop /snap/firefox/1115
loop23 7:23 0 54.2M 1 loop /snap/snap-store/558
sda 8:0 0 113G 0 disk
├─sda1 8:1 0 200M 0 part /boot/efi
├─sda2 8:2 0 84.9G 0 part
└─sda3 8:3 0 27.9G 0 part /
sdb 8:16 1 241.9G 0 disk
├─sdb1 8:17 1 200M 0 part
├─sdb2 8:18 1 120.8G 0 part /media/asahsieh/Transcend
└─sdb3 8:19 1 120.7G 0 part /media/asahsieh/Ubuntu_data
```
- or by `fdisk -l`
4. Move data by `rsync` command
> Ref.: [How to backup your home directory in Linux](https://www.pragmaticlinux.com/2021/05/how-to-backup-your-home-directory-in-linux/)
## File manipulation
### Replace string in file
> ref.: [How to use sed to find and replace text in files in Linux / Unix shell](https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/)
`sed -i 's/search_string/replace_string/' filename`
### Create and Extract .tar.bz2 or .tar.gz files
> Ref.: https://www.cyberciti.biz/faq/how-to-create-tar-gz-file-in-linux-using-command-line/#Creating_tar.gz_file
- Extract files
- bz2
`tar -xvjf guitar_songs.tar.bz2`
- gz
`tar -xvzf ukulele_songs.tar.gz`
- Create files
- The syntax for the tar command is as follows:
`tar -czvf filename.tar.gz /path/to/dir1`
`tar -czvf filename.tar.gz /path/to/dir1 dir2 file1 file2`
- Create a tar.gz file from all pdf (".pdf") files
`tar -czvf archive.tgz *.pdf`
- To create projects.tar.gz in the current working directory, run:
`tar -czvf projects.tar.gz $HOME/projects/`

## Network checking
### Get routing path
`sudo mtr access.sifive.com -c 100 -r`
# Information
## Directory structure
- Typical Ubuntu filesystem hierarchy

> 圖片出處: [Filesystem Hierarchy Standard](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard)
- [Filesystem Hierarchy Standard
LSB Workgroup, The Linux Foundation](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html)
# Coding
> reference [https://www.kernel.org/doc/html/latest/](https://www.kernel.org/doc/html/latest/)
## [Linux kernel coding style](https://www.kernel.org/doc/html/latest/process/coding-style.html)
## [8) commenting](https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting)
:::info
Comments are good, but there is also a danger of over-commenting. ==NEVER try to explain HOW your code works in a comment==:
- it’s much better to write the code so that the working is obvious, and it’s a waste of time to explain badly written code.
==Generally, you want your comments to tell WHAT your code does, not HOW. Also, try to avoid putting comments inside a function body==:
- if the function is so complex that you need to separately comment parts of it, you should probably go back to chapter 6 for a while.
:::
When commenting the kernel API functions, please use the kernel-doc format. See the files at [Documentation/doc-guide/](https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting../doc-guide/index.html#doc-guide) and `scripts/kernel-doc` for details.
The preferred style for long (multi-line) comments is:
```c
/*
* This is the preferred style for multi-line
* comments in the Linux kernel source code.
* Please use it consistently.
*
* Description: A column of asterisks on the left side,
* with beginning and ending almost-blank lines.
*/
```
For files in `net/` and `drivers/net/` the preferred style for long (multi-line) comments is a little different.
```c
/* The preferred comment style for files in net/ and drivers/net
* looks like this.
*
* It is nearly the same as the generally preferred comment style,
* but there is no initial almost-blank line.
*/
```
> :notes: There is no initial almost-blank line.
It’s also important to ++comment `data`++, whether they are basic types or derived types.
- To this end, use just one data declaration per line (no commas for multiple data declarations).
> This leaves you room for a small comment on each item, explaining its use.
# Plugins
## `fzf`
> Source: https://github.com/junegunn/fzf
### Shell extensions
- Key bindings (`CTRL-T`, `CTRL-R`, and `ALT-C`) (bash, zsh, fish)
- Fuzzy auto-completion (bash, zsh)
# FAQ
## How to read `man` pages
> [Understanding man Pages in Linux \[Beginner's Guide\]](https://itsfoss.com/linux-man-page-guide/)
- check the *format* on terminal, e.g., `SYNOPSIS`:
```shell
$man man
The following conventions apply to the SYNOPSIS section and can be used as a guide in other sections.
bold text type exactly as shown.
italic text replace with appropriate argument.
[-abc] any or all arguments within [ ] are optional.
-a|-b options delimited by | cannot be used together.
argument ... argument is repeatable.
[expression] ... entire expression within [ ] is repeatable.
```
> [HOW TO READ MAN PAGES](https://www.cs.mcgill.ca/~guide/help/man.html)
- SYNOPSIS
- words in `[ ]`: means *flags* for this command
# References
- [Linux handbook](https://linuxhandbook.com/)