Linux basics

What is Linux in the first place?

A type of operating system derived from unix (Unix is not open source, but Linux is open source). By the way, the OS of mac is macOS (I misunderstood it as Linux because I can use linux commands in the terminal, but that is macOS). Linux is called the de facto standard for web systems. In other words, Linux is the mainstream OS for WEB servers. Also, Ubuntu and centOS, which I often hear, are called Linux distributions, which are also Linux, and are also often used as OSs for web servers.

Supplement (Ingenuity to make the current local environment the same as the server) However, most users do not have a Linux environment on their PC. However, by using vigrant or virtual box, you can use the Linux environment on your own PC. Specifically, place a vital box on the OS of your PC and use vagrant to build a Linux environment on it. Placing an OS that is not originally on the PC on the original PC in this way is called virtualization. vigrant is required to place the originally set environment (box) in the vital box. The vagrantfile is required for each server on the virtual box, and you have to set the IP address etc. there. Because you can have multiple servers in virtualbox.

The fancy dress machine starts with vagrant up.

[What is Linux? ](Https:// AE% E3% 83% 87% E3% 83% 95% E3% 82% A1% E3% 82% AF% E3% 83% 88% E3% 82% B9% E3% 82% BF% E3% 83% B3% E3% 83% 80% E3% 83% BC% E3% 83% 89) [Linux environment construction] What are Vagrant and VirtualBox? Explain how to use for beginners!

Virtual box and vigrant are convenient for creating a virtual environment, but some have come out that can create a virtual environment more conveniently. That is Docker. Docker is also placed on the OS of the original PC (it is called a container) and virtualized. Docker is created based on the idea of "immutable infrastructure", and if you want to make changes to the infrastructure, you can mess with Docker without having to mess with the infrastructure. It's amazing that you can create infrastructure with just code! !! → It will be very easy to break the infrastructure and make it. By the way, the contents of the virtual environment like the vigrantfile are described in the dockerfile.

Docker's container (virtualized environment) uses the lower Linux kernel, so it operates quickly. The big difference from Virtual box is that Virtual box creates a guest OS separately from the host OS and starts it, so the processing amount is larger than Docker that uses the lower Linux kernel.

Actual usage of Docker Place Docker on the OS of the local PC, specify the OS etc. on it and develop ↓ When you want to deploy, just put Docker on the OS of the server and migrate the one created in development on it as it is.

What is Docker

Linux component

Shell and linux kernel The kernel is inside, and the shell is outside. The kernel has the function of working in a PC with the image of a tool. The shell acts as an intermediary between human commands and the kernel, translating human commands so that the kernel can be executed.

You can also change the shell to your liking (you can switch shells by entering commands!). The main shell used is bash.

Login shell → The shell that is automatically reflected when you log in

Shell script → Create your own command by combining the original commands

How to write commands, absolute path relative path

Command + location + options

A command commands what you want to execute

The location specifies the location and things of the PC. ・ Directory → It's like a box ・ File → It's like paper

/ Means root directory Therefore, the meaning of / aaa / bbb / is .. of aaa in the root directory. In other words, if / is added to the beginning, it will be an absolute path, otherwise it will be a relative path.

What is an option? You can use the command in more detail by adding a-after the command and adding an option. Even if you enter the same option (character), the content will change depending on the command.


Log in to your account when you enter your PC. ↓ If you open the console in this state, you will be logged in with your account logged in above.

If you want to change from your current user to the root user sudo su - Enter with

Become the root user with sudo su-


alias Name Command → A cute version of a shell script that allows you to define command content unalias name → invalidate definition type name → know what the definition is

set shopt → Decide whether to turn options on or off

Shell variables (environment variables)

You can define variables in bash Note that = does not create a space between expressions → You have to make an export shell variable to make the variables you defined available

Originally included shell variables PATH → Tells you where all commands (shells) are located (not necessarily one location) LANG → Tells you in what language the error statement appears

To pass the PATH means to treat the file as a kernel. Make sure the file is read by human command → shell. This is a clearer explanation of how to pass the PATH. Put PATH on Mac If you use the export command directly in the command, you can pass the path

User group management

In Linux, registered users can use the system. If there are multiple people who want to use the system, you have to create a user account for each. And although Linux has directories and files, not everyone can play with them, but authorized ones. And multiple users can be managed together as a group. Users who belong to that group use it because they have the authority to view a file called OO.

・ User creation Create the user as the root user. Information about users created in Linux can be found in / etc / passwd. ↓ The user ID, home directory, and shell are specified in / etc / passwd. The home directory is given to each user (for example, / home / ishikawa), in which the user can create files as he / she likes. For macOS, it will be "/ Usesrs / user name".

・ Group creation You can also create multiple users as a group. There is group information in etc / group. There you will find the group name, group ID and information about the users being added as subgroups. When a user is created, the user is set to belong to the group with the same name as the user name. The group at this time is called the primary group of the handle to the user. Users can also join multiple groups. A group to which you belong other than the primary group is called a subgroup.

Also, by specifying an existing group name at the time of user registration, the primary group of that user becomes the specified existing group.

File owner

Every file or directory has an owner (owning user) But the owner is not the only one who handles these

Owners, groups, etc. A group is a user who is registered on the computer, and others are not. What you can do with that file depends on each

You can see it by looking at the file details with ls -l ↓

-rw-r--r--1 root root 81233 Last modified file name

The file mode (1st character) and authority (2nd to 10th characters) are written at the beginning.

File mode -Is an ordinary file, d is a directory, l is a symbolic file

Operable range (You can see what you can do in the order of owner, group, etc. like rw-rw-r-- →) r → read w → write x → execute

1 indicates the number of links and the number of hard links. (Does not include the number of symbolic links)

The first root is the owning user. The user who owns this file. The second root is the owning group. It is basically the primary group of owning users. If you belong to that group, what you can do may change compared to other groups.

Commands used for permissions

Commands that only the root user can do

chmod → You can change these permissions The number after this means 644 → rw-r--r--. It feels like 11-1--- is being converted to a decimal number. chown → Used when you want to change the user who owns the file. Add -R when you want to change recursively chgrp → Used when you want to change the owning group

Superuser → users who can do all rwx

su → Log in to superuser sudo command → Temporarily become superuser and execute commands

Which authority is required to execute the command

cd can only be used by the current user with X privileges on that directory

ls requires the current user to have R privileges on that directory

For touch and rm, the current user must have W and X privileges for that directory.

Processes and jobs

A process is a task that occurs in memory due to a command. A single command can spawn multiple processes.

Since Linux is a multitasking operating system, it is doing many processes at the same time. To be precise, the process is switched in detail, so it just looks like they are doing it at the same time. In other words, a server running Linux is running a tremendous amount of processes.

You can see the processes in ps (basically with the aux option to make all processes easier to see). pid is the process number For more information [for beginners] Learn about the Linux process! ](Https://

It is said that a process spawns a process, but you can see the relationship between the processes that are currently running by using pstree.

You can kill the specified process by using the kill command.

A job is to allow you to see another material while you are working. Pause the work and then see another material. That work is called a job. Basically, it is rarely used.

How to pause ctrl + z → Pause work and recognize as job

How to see the list of jobs jobs → Know the job number and what work is paused

fg% job number → resume work

kill% job number → stop pausing the job

bg% job number → resume work, used for copying

Redirects and pipelines

Redirects change the location of inputs and outputs

Input redirect → command <where to read Change the reading location. I don't use it much. ..

Output redirect → Command> Read location Change the reading location. Set the read location to a specific file and use it to view the log.

Error syntax output → Command 2> Where to read → Save the error details

dev / null → No matter what is written here, nothing remains. No matter what I take out, nothing comes out ↓ When you want to empty the file File </ dev / null Good to do

What is a pipeline? Insert an l between commands to connect them.

Shell script

A shell script is your own shell created by combining commands.

A model Create a file for shell scripts in the location of the shell, add .sh after the name ↓ On the first line in the file #! File location write ↓ Empower with chomad ↓ source Self-made shell file name or .Self-made shell file name Run on Use source if there is an alias command in your own shell script

Archive and compression

An archive is a file that is a collection of multiple files and directories, and the name of the archive is followed by .tar.

Compression is to reduce the size of a file

tar cf archive name directory you want to archive tar tf archive → check the contents of the archive tar xf archive → unpack the archive

gzip Compressed source file → Compressed, compressed file will be the original file name with .gz added gzip -d compressed file name → decompress

bzip2 compresses more than gzip xy compresses from bzip2

・ How to archive and compress at once tar czf compressed archive name (~ .tar.gz) source directory → Archive and compress

tar xzf compressed archive name (~ .tar.gz) → decompress

tar cjf compressed archive name (~ .tar.bz2) source directory → Archive and compress to bzip2

tar cJf compressed archive name (~ .tar.xy) source directory → Archive and compress to xy

zip -r compressed archive (.zip) source directory → Archive and compress at once,

zip -er compressed archive (.zip) original directory → Archive and compress at once, you can also attach a password

unzip compressed archive → decompress

Download package

A package is an archive required to run software The package also has a format. rmp type and deb type. How to install depends on the type

yum type yum install package name → install yum erase / remove Package name → Remove yum search all search word → search package yum info Package name → Get information

deb type sudo apt-get install package name → install sudo apt-get remove Package name → Remove apt-cache search Search word → search apt-cache show Package name → Get information

Remote login and ssh

Remote login means logging in to another server from your own computer. ↓ At that time, you will need the IP address and ssh of the other server. The IP address is the address of your computer. ssh is like a tunnel between servers.

How to log in ssh user name IP address of the other party

Sending data to a remote server via ssh is called port forwarding or tunneling. ↓ Premise Each server of myself and the other party has a lot of places to send information called ports. ↓ How to do port forwarding ssh -L local port number remote IP address remote port number → You can now send data

Main command 1

cd location → move pwd → Know where you are now ls location → find out what's in your current directory

. → Show where you are .. → Indicates the location of the parent directory ~ or Nothing → Indicates the location of your home directory. Show the contents of your account in your home directory! / → Show the location of the root directory! , This is the really top directory on your computer

Main command 2

mkdir → create a directory touch → create a file rm → file deletion rm -r → Delete directory rmdir → delete empty directory cat -n → Know the contents of the file, cannot scroll less → You can see the contents of the file and scroll

cp → Can be duplicated in the same location, duplicated in another location, or overwritten cp a b → duplicate if b does not already exist, overwrite b with the contents of a if it exists, cp a directory → duplicate to directory

mv → transfer or rename mv file File → Rename file to second time mv file directory → move file to directory

You can search faster than locate → find man → Know how to use commands know which → where the command is

ln Link source registered name → You can specify the link source with the registered name (it feels like duplication? The authority is exactly the same. Just give an alias to the link source). Create hard links (relatively rarely use hard links)

ln -s Link A Link B → Creates link B, which is a symbolic link of link A. A symbolic link is a link that connects to the original link when accessed. Think of it as different from the original link A. When the original link is deleted, in the case of a hard link, the content remains, but in the case of a symbol link, there is no one to refer to and nothing can be done.

Strictly speaking, the file name is associated with the i-node (the location of the hard disk where the execution contents are written). Since the hard link is the same i-node as the original link, you can access and execute the i-node even if the original file disappears. In the case of a symbolic link, accessing your i-node will execute the original file, but it cannot be executed if the original file does not exist.

For details, The ln command is summarized in detail [Linux command collection] For the difference between hard links and symbolic links, see symbolic links.

find Search start directory Condition → Search for the one that meets the condition from the start directory (This article was very easy to understand. [Find / grep] How to get a list of files containing a specific character string. )

Main command 3

wc → Know the number of lines, words, and bytes in the file sort → Sort by default in alphabetical order uniq → delete duplicate lines

cut d Delimiter -f field number File name → Separate each line in the file with a delimiter, and read the contents of the field number, like cat. ↓ In other words, the contents of the file must look the same line by line!

tr Character before replacement Character after replacement → Replaces the character in the file, only one character can be used tr -d character → Deletes the character in the file

tail, head → Tells you the contents of the last or first 10 lines of the file

diff Comparison source file Comparison file → Tells you what has changed

grep Word file → Tells you the file containing the word, with -n with the number of lines -i with -i regardless of case-r will check all layers below the current directory ↓ In addition, you can search for any character by using the word part as a metacharacter. (This article was very easy to understand. [Find / grep] How to get a list of files containing a specific character string.) Detailed summary of grep command [Linux command collection]

Can be replaced in word units that could not be done with sed → tr Number of sed addresses d File → Delete the address line of the file sed -n number of addresses p File → Display the address line of the file sed's / A word / B word'File → Replace A word in file with B word

awk'{print field count}' → Show only the contents of the specified number of fields in the file

du File → Know the used capacity of the file

Directory type

/ bin → important commands are stored in this / home → Home directory, personal box assigned to each Linux user (that is, when you go home with cd, it jumps to the contents of your account)

Recommended Posts

Linux basics
Linux basics
Linux command basics
linux basics (users / groups)
linux memorandum
Python basics ⑤
Linux commands
Linux commands
Linux command # 3
Linux overview
Python basics
direnv (linux)
NumPy basics
Python basics ④
Organize Linux
linux commands
Linux practice
Ubuntu Linux 20.04
Pandas basics
Git basics
Linux Summary
Linux process
Python basics ③
Linux permissions
Python basics
Django basics
Linux command # 5
About Linux
[Must-see for beginners] Basics of Linux
Forgot linux
Pandas basics
Python basics
Python basics
About Linux
Python basics ③
Linux commands
Linux commands
Python basics ②
About Linux
Python basics ②
About Linux
About Linux ①
Linux redirect
[Linux] Learn the basics of shell commands
Linux command list
[Linux] About export
Python basics: list
Python basics memorandum
2-2. Input for becoming a WEB engineer (Linux basics)
[Linux] Symbolic link
Shell script basics # 2
linux at command
Linux environment construction
Network Linux commands
[Linux] Search command
Linux x memo
Linux directory structure
Linux command <Basic 2>
Verbalize Linux commands