GNU / Linux 是一种自由和开放源码的类 UNIX 操作系统。Linux 英文解释为 Linux is not Unix。Linux 是在 1991 由Linus Benedict Torvalds 在赫尔辛基大学上学时创立的,主要受到 Minix 和 Unix 思想的启发。

1 远程登录Linux
远程登录 Linux 系统,我们一般使用 SSH(OpenSSH SSH client)。
1.1 ssh 简单使用
我们可以使用 man ssh
来查看 ssh
命令的使用手册。参数如下:
ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
- 方括号
[]
代表可选参数,可写可不写
详细信息请参考:
1.2 常用示例
1. 使用指定用户名和端口远程登录主机,然后再输入密码即可登录
ssh -p 22 用户名@IP地址
如果没有指定用户名和,则使用 ~/.ssh/ssh_config
和 /etc/ssh/ssh_config
配置文件中定义内容。如果配置文件中没有指定,则端口默认为 22,用户名默认为当前用户。
ssh 获取相关参数的优先级顺序为:
2. 配置免密登录
学习ssh、文件与目录管理命令、文件查看命令、用户管理命令第一步:使用 ssh-keygen -t rsa
命令在客户端生成 RSA 公钥和私钥,一直回车确认。公钥默认名称为 id_rsa.pub
,私钥默认名称为 id.rsa
,保存在 ~/.ssh
目录下。
第二步:将客户端的公钥追加至远程服务器端的 ~/.ssh/authorized_keys
文件。
有两种方法:通过 ssh-copy-id
命令;通过 scp
命令。
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 用户名@IP地址
回车后,输入登录密码,以后就可以免密登录了。
2 文件与目录管理命令
Linux 的目录结构如下:


2.1 ls
list 的缩写,列出目录
语法:
ls [options] [name...]
示例:
# 列出 demo01 目录下所有子目录及文件
$ ls -lR demo01
demo01:
total 49852
drwxr-xr-x. 2 root root 6 Dec 11 22:49 assets
drwxr-xr-x. 2 root root 18 Dec 11 22:50 bin
-rw-r--r--. 1 root root 62019 Nov 13 22:17 cpptools-linux.vsix
-rw-r--r--. 1 root root 454656 Nov 13 22:18 cpptools-linux.vsix.1
-rw-r--r--. 1 root root 50524369 Oct 20 10:49 cpptools-linux.vsix.2
drwxr-xr-x. 2 root root 6 Dec 11 22:49 include
drwxr-xr-x. 2 root root 6 Dec 11 22:49 lib
drwxr-xr-x. 2 root root 22 Dec 11 22:50 src
demo01/assets:
total 0
demo01/bin:
total 12
-rwxr-xr-x. 1 root root 8968 Nov 13 21:38 main
demo01/include:
total 0
demo01/lib:
total 0
demo01/src:
total 4
-rw-r--r--. 1 root root 134 Nov 13 21:48 main.cpp
# 统配符使用
# 列出/etc目录下以.conf结尾的所有文件
$ ls /etc/*.conf
/etc/asound.conf /etc/GeoIP.conf /etc/libaudit.conf /etc/mke2fs.conf /etc/sestatus.conf /etc/vconsole.conf
/etc/chrony.conf /etc/host.conf /etc/libuser.conf /etc/nsswitch.conf /etc/sos.conf /etc/yum.conf
/etc/dracut.conf /etc/kdump.conf /etc/locale.conf /etc/resolv.conf /etc/sysctl.conf
/etc/e2fsck.conf /etc/krb5.conf /etc/logrotate.conf /etc/rsyncd.conf /etc/updatedb.conf
/etc/fprintd.conf /etc/ld.so.conf /etc/man_db.conf /etc/rsyslog.conf /etc/usb_modeswitch.conf
# 使用 egrep,筛选etc目录下以s开头,.conf 结尾为文件
$ ls /etc/*.conf | egrep "/etc/s.*.conf"
/etc/sestatus.conf
/etc/sos.conf
/etc/sysctl.conf
# 最新修改的文件在最下面显示,方便查看最近修改的文件
$ ls -lrt /etc
total 1116
-rw-r--r--. 1 root root 1634 Dec 25 2012 rpc
-rw-r--r--. 1 root root 28 Feb 28 2013 ld.so.conf
-rw-r--r--. 1 root root 670293 Jun 7 2013 services
......
-rw-r--r--. 1 root root 34797 Dec 17 22:05 ld.so.cache
-rw-r--r--. 1 root root 77 Dec 17 22:05 shells
-rw-r--r--. 1 root root 49 Dec 24 10:37 resolv.conf
$
# 最大为文件在最下面显示,方便查看目录中最大文件
$ ll -Shr /etc
total 1.1M
-rw-r--r--. 1 root root 0 Jun 7 2013 motd
-rw-r--r--. 1 root root 0 Jun 7 2013 exports
-rw-r--r--. 1 root root 0 Apr 1 2020 environment
......
-rw-r--r--. 1 root root 12K Aug 14 17:05 aliases.db
-rw-r--r--. 1 root root 34K Dec 17 22:05 ld.so.cache
-rw-r--r--. 1 root root 655K Jun 7 2013 services
$
# 只列出子目录
$ ls -F /usr/local/ |grep /$
bin/
etc/
games/
include/
lib/
lib64/
libexec/
sbin/
share/
src/
2.2 cd
Change Directory 的缩写,切换工作目录
语法:
cd [dirName]
示例:
# 切换到用户主目录
cd ~
# 切换到上层目录的上层目录
cd ../..
# 切换到进入此目录之前的那个目录
cd -
# 把上个命令的参数作为 cd 参数使用, !$表示上一条命令
cd !$
2.3 pwd
Print Working Directory 的缩写,显示当前目录的路径
语法:
pwd [-LP]
示例:
# 显示当前实际物理工作目录,-P 会不以连结档的数据显示,而是显示正确的完整路径
pwd -P
2.4 mkdir
make directory 的缩写,创建目录
示例:
$ proj=test_proj; mkdir $proj/bin $proj/include $proj/lib $proj/src
mkdir: cannot create directory ‘test_proj/bin’: No such file or directory
mkdir: cannot create directory ‘test_proj/include’: No such file or directory
mkdir: cannot create directory ‘test_proj/lib’: No such file or directory
mkdir: cannot create directory ‘test_proj/src’: No such file or directory
# -p 递归创建多个目录或一次创建多级目录,如果该目录不存在,则新创建它
$ proj=test_proj; mkdir -p $proj/bin $proj/include $proj/lib $proj/src
$ ls test_proj/
bin include lib src
# -m 设置目录权限
# -(filetype)---(user)---(group)---(other)
# 权限为 rwx,可读可写可执行,在设置权限是可以用对应的二进制转换为十进制数
# 创建一个 user: rwx, group:r-x, other:--x 的目录test01
$ mkdir -m 751 test01
$ ls -l .
total 8
-rw-------. 1 root root 1544 Aug 14 17:01 anaconda-ks.cfg
drwxr-xr-x. 3 root root 17 Nov 30 23:37 code
drwxr-x--x. 2 root root 6 Dec 11 23:11 test01
drwxr-xr-x. 2 root root 6 Dec 11 23:14 test_proj
-rw-------. 1 root root 0 Nov 13 2020 yum.log
drwxr-xr-x. 2 root root 4096 Nov 29 21:07 yum.repos.d
注意到,test01 目录的权限确实是rwxr-x--x
(751),而没有指定权限的目录默认权限为 rwxr-xr-x
(755)。
2.5 rmdir
remove directory 的缩写,删除一个空的目录
语法:
rmdir [OPTION]... DIRECTORY...
-p
:递归删除多级空目录。
2.6 cp
copy 的缩写,拷贝文件或目录。
语法:
cp [options] src dst

示例:
$ tree test01/
test01/
├── bin
├── include
├── lib
└── src
└── main.cpp
4 directories, 1 file
# 将 test01 目录所有内容复制到新目录 test02 下
$ cp test01/ test02
cp: omitting directory ‘test01/’
$ cp -r test01/ test02
$ ls
test01 test02
- 注意:
cp
复制目录时,必须使用参数 -r 或者 -R 。
2.7 rm
remove 的缩写,删除文件或目录
语法:
rm [options] name...
示例:
rm -i test01
选项:
-f
:强制删除-i
:删除前提示是否删除-r
:递归删除,一遍删除目录
2.8 mv
move file 的缩写,移动文件 / 目录,或重命名文件 / 目录
语法:
mv [options] src dst
mv [options] src... directory
选项:
-f
:强制移动-i
:若目标文件(dst)已存在,提示是否覆盖-u
:若目标文件(dst)已存在,且 src 较新,才会升级 (update)
示例:
mv test.cpp main.cpp
详细信息请参看:
3 软链接与硬链接
Linux 链接文件类似于 Windows 中的快捷方式,Linux 链接文件分为硬链接和软链接。
在 Linux 文件系统中,一个文件被分成两个部分:元数据(metadata)与用户数据(user data)。
- 元数据(meta data):文件的附加属性。如索引节点(Inode)、文件大小、文件创建时间、文件所有者等。元数据中并不包含文件名,文件名仅仅是为了方便用户使用。Linux 文件系统为每一个保存在磁盘分区中的文件(无论什么类型)都分配一个索引节点号(Inode Number),索引节点号是文件在一个文件系统中的唯一标识,不同文件所对应的索引节点号是不相同的。
- 用户数据(user data)。即文件数据块(data block),文件数据块中以二进制的形式记录着文件的真实内容。
详细内容可参考:
使用 ln
命令,可以创建链接。
语法如下:
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... TARGET (2nd form)
ln [OPTION]... TARGET... DIRECTORY (3rd form)
ln [OPTION]... -t DIRECTORY TARGET... (4th form)

示例:
# 编译 main.c
$ gcc code/main.c -o code/main
# 给文件 main.py 创建一个名为 test 的软链接
$ ln -s /home/datawhale/geyashi/code/main test
# 查看
$ ll
total 4
drwxr-xr-x 2 1000 datawhale 21 Dec 14 11:58 code
drwxr-xr-x 2 1000 datawhale 25 Dec 13 21:00 download
-rw-r--r-- 1 1000 datawhale 157 Dec 13 20:58 download_files.txt
lrwxrwxrwx 1 1000 datawhale 33 Dec 14 12:43 test -> /home/datawhale/geyashi/code/main
# 执行 test 软连接,打印出来和 code/main 一样
$ ./test
Hello ln
当删除 code/main.c 文件时,软连接会失效:

# 此时再运行 test 软连接 就会提示找不到文件
$ ./test
-bash: ./test: No such file or directory
注意:删除软连接时,要小心,不要连源文件也一块删除了😼
4 文件内容查看
4.1 cat
由第一行开始显示文件内容
语法:
cat [OPTION]... [FILE]...
-A
:显示所有空白字符-b
:显示行号
示例:
# 将 test01 文件内容追加到 test02 文件中
cat -b test01 >> test02
4.2 tac
tac 与 cat 命令刚好相反,文件内容从最后一行开始显示。
4.3 more
一页一页翻动
4.4 head
取出文件前面几行
4.5 tail
取出文件后面几行
示例:
# 显示 文件 最后 5行 内容
tail -n 5 <filename>
# 实时查看文档更新
tail -f <filename>
4.6 od
以字符串或十六进制显示二进制文件
用法
od [OPTION]... [FILE]...
od [-abcdfilosx]... [FILE] [[+]OFFSET[.][b]]
od --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]
4.7 nm
查看动态库和静态库中的符号表
用法
nm [-A|-o|--print-file-name] [-a|--debug-syms]
[-B|--format=bsd] [-C|--demangle[=style]]
[-D|--dynamic] [-fformat|--format=format]
[-g|--extern-only] [-h|--help]
[-l|--line-numbers] [-n|-v|--numeric-sort]
[-P|--portability] [-p|--no-sort]
[-r|--reverse-sort] [-S|--print-size]
[-s|--print-armap] [-t radix|--radix=radix]
[-u|--undefined-only] [-V|--version]
[-X 32_64] [--defined-only] [--no-demangle]
[--plugin name] [--size-sort] [--special-syms]
[--synthetic] [--target=bfdname]
[objfile...]
4.8 cut
对数据进行切分,取出想要的部分
4.9 man
Linux 命令纷繁复杂,一般所有 Linux 命令都提供了手册,我们可以通过 man
命令来查看它。而不需要去网上搜。
用法
man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale]
[-m system[,...]] [-M path] [-S list] [-e extension] [-i|-I]
[--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages]
[-P pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation]
[--no-justification] [-p string] [-t] [-T[device]]
[-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
man -k [apropos options] regexp ...
man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
man -f [whatis options] page ...
man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale]
[-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t]
[-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
man -w|-W [-C file] [-d] [-D] page ...
man -c [-C file] [-d] [-D] page ...
man [-?V]
示例
# 等价于 whatis man
# 打印手册中的NAME描述
$ man -f man
man (1) - an interface to the on-line reference manuals
4.10 info
4.11 whatis-which-where
whatis
可以通过 whatis
命令查看一个命令是做什么的。
# 查看
$ whatis nm od tac
nm (1) - list symbols from object files
od (1) - dump files in octal and other formats
tac (1) - concatenate and print files in reverse
which
where
5 用户和用户组管理
GNU / Linux 系统是一个多用户多任务的分时操作系统(Multi-tasking / Time sharing OS)。允许多个用户共享一台计算机。每个用户账号都拥有一个唯一的用户名和各自的口令。因此,我们有必要了解 Linux 的用户和用户组管理。
5.1 useradd 添加用户账号
useradd [options] username

实例:
useradd -c test03 -d /home/test03 -g users -s /bin/bash -u 1002 test03
5.2 用户口令的管理
passwd [OPTION...] <accountName>
-l
锁定口令,即禁用账号。-u
口令解锁。-d
使账号无口令。-f
强迫用户下次登录时修改口令。
不指定用户名,则修改当前用户的口令。
示例:
# 修改 test01 口令
[root@bogon ~]# passwd test01
Changing password for user test01.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
$ ssh test01@192.168.56.104
test01@192.168.56.104's password:
Last login: Sun Dec 12 18:59:34 2021
-bash-4.2$
-bash-4.2$ whoami
test01
-bash-4.2$
# 需要配置一下.bashrc 和 .bash_profile
$ cp -r ~/.bash* /home/test01
$ ssh test01@192.168.56.104
test01@192.168.56.104's password:
Last login: Sun Dec 12 20:35:16 2021 from bogon
[test01@bogon ~]$ exit
logout
Connection to 192.168.56.104 closed.
5.3 删除账户
userdel [options] LOGIN
- 一般选项为
-r
表示将该用户的主目录一块删除。
userdel -r test03
5.4 添加批量用户
1. 按照 /etc/password
密码文件编辑一个userfile.txt
文件
每个用户的用户名、uid、主目录都可以相同,其中密码可以不写。
用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell
# 创建 test01 和 test02 两个普通用户
# 属于 users 组(100),登录 shell 为 /bin/bash
test01:x:1000:100:test01:/home/test01:/bin/bash
test02:x:1001:100:test02:/home/test02:/bin/bash
- 口令:不是明文,在
/etc/shadow
中存放着真正的加密后的口令,而在/etc/passwd
文件的口令字段中只存放一个特殊的字符,例如x
或者*
。 - 用户标识号:范围是 0~65535。0 是 root 的标识号。1-99 由系统保留,作为管理员账号。CentOS7 从 1000 开始。
- 组标识号:对应于
/etc/group
文件中的记录。 - 登录 shell:用户登录后,需要启动一个进程,负责将用户的操作传给内核,这个进程是用户登录到系统后运行的命令解释器或某个特定的程序,即 登录 Shell。
2. 执行 /usr/sbin/newusers
命令,利用 userfile.txt
创建多个用户
newusers < userfile.txt
通过查看 /etc/passwd
文件发现这两个用户已经创建成功了。
3. 执行 /usr/sbin/pwunconv
命令,
将 /etc/shadow
产生的 shadow 密码解码,写到 /etc/paaawd
中,并将 etc/shaow
的 shadow 密码栏删掉。先取消 shadow password
功能。
pwunconv
4. 编辑每个用户的密码对照文件。
userpwd.txt 文件如下:
test01:111111
test02:222222
5. 以 root 身份运行 /usr/sbin/chpasswd
命令,批量更新用户口令
6. 执行 pwconv
命令。
查看 /etc/shadow
文件的最后两行有了两个用户的密码信息。
$ tail -n 5 /etc/shadow
chrony:!!:18973:0:99999:7:::
ntp:!!:18973:0:99999:7:::
tcpdump:!!:18973:0:99999:7:::
test01:$6$LbnsXFG3yd/WJF$P.LpxEWq4pIuXf4bjPAfRIWJ2d0Am.3l2erou10cvuVF.Q/nRuTcsU4HQSPztoX0Z4am5BW7V2..czysYb6bp/:18973:0:99999:7:::
test02:$6$5A1lV/NC/oq/ms$Yo8UDKsuFWL3irWwyUpiZoHtXhFs7WdFAmlR/shf2Ty7iHV5oKm1JL8IUCCUKQvBJ8adS8eomJyuGTURfUpOC.:18973:0:99999:7:::
7. 验证两个用户是否可以登录成功。