Linux 初始化使用

这里写自定义目录标题

  • 1. Linux 初始化使用
    • 1.1. 无 timeout GRUB 启动
    • 1.2. 免密登录
    • 1.3. sudo 免密
      • 1.3.1. 添加自 sudo 组
      • 1.3.2. 添加进 `sudoers`
    • 1.4. sudo 中使用 alias 别名
    • 1.5. 与 Windows 文件共享
      • 1.5.1. Windows 开启共享
      • 1.5.2. 安装 Samba
      • 1.5.3. 挂载
      • 1.5.4. 自动挂载
      • 1.5.5. 排错
        • 1.5.5.1. `mount error(115): Operation now in progress`
    • 1.6. 关机命令
    • 1.7. 开发
      • 1.7.1. vscode
      • 1.7.2. sublime text
        • 1.7.2.1. Tooling integration: install Package Control
        • 1.7.2.2. Automatic formatting: Gofmt
        • 1.7.2.3. Code linting
        • 1.7.2.4. Autocompletion
        • 1.7.2.5. Code navigation
        • 1.7.2.6. Snippets
    • 1.8. 其它
      • 1.8.1. 急救
      • 1.8.2. 排错
        • 1.8.2.1. Debian 查看启动日志
        • 1.8.2.2. debian 查看日志_如何使用Debian 10上的日志集中日志
      • 1.8.3. 卸载磁盘出现 `umount: /mnt: target is busy.`
      • 1.8.4. 从组中删除用户
      • 1.8.5. xfce 中使用 expect 在 su 时免密码输入切换到 root 账户

1. Linux 初始化使用

就选用 Debian 32 位 xfce 版本在虚拟机中使用了, 别折腾其它的了, 坑巨多、不效率。

下载地址, from 清华镜像站。

这里的登录用户名是: user

Debian 现在的 GUI 编辑器是 mousepad, 之前是 gpedit

1.1. 无 timeout GRUB 启动

vi /etc/default/grub

GRUB_TIMEOUT=0

# Debian
update-grub2
# CentOS
grub2-mkconfig -o /boot/grub2/grub.cfg

重启。

1.2. 免密登录

Look up these lines in lightdm configuration file, uncomment them and customize to your preference.

[Seat:*]
autologin-user=user
#autologin-user-timeout=0

参考自: https://wiki.debian.org/LightDM#Enable_autologin

1.3. sudo 免密

1.3.1. 添加自 sudo 组

sudo usermod -aG sudo user

1.3.2. 添加进 sudoers

一定要在文件 /etc/sudoers 的最后添加:

username     ALL=(ALL) NOPASSWD:ALL

否则会被覆盖相当于没添加。

1.4. sudo 中使用 alias 别名

根据 askubuntu 中的 Aliases not available when using sudo 的中回答, 需要在 ./.bashrc 中加上

alias sudo='sudo'

才行使 sudo 时, crontab 的 alias 生效。

bash manual: The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias.

1.5. 与 Windows 文件共享

这里不采用 VMware 的共享方式, 有 bug。这里采用的是 Windows 开启文件共享, Linux 通过 Samba 去访问。

1.5.1. Windows 开启共享

右键, 共享, Everyone 读写。

开启免密: 组策略 gpedit.msc。在 “WINDOWS 设置 - 安全设置 - 本地策略 - 安全选项” 中找到 “使用空白密码的本地帐户只允许进行控制台登录”, 将其设置为 “已停用”。这样就可以使用 Administrator 账号、空密码进行文件共享和远程桌面连接了。

1.5.2. 安装 Samba

sudo apt install nfs-common
sudo apt install cifs-utils

1.5.3. 挂载

sudo mount -t cifs //192.168.1.6/a /mnt/a -o username="Administrator",password=""

1.5.4. 自动挂载

注意: 这里操作不慎会引起系统无法启动, 如果无法启动, 请参照下面 “其它” 里面的 “急救” 方法。

sudo apt install cifs-utils
sudo mkdir /mnt/a

# 看是否能挂载成功, 如否修改直到成功。卸载命令 sudo umount /mnt/a
sudo mount -t cifs -o rw,vers=3.0,credentials=/root/.smbcredentials //192.168.1.6/a /mnt/a

# 如果上面成功的话就再执行下面的
mousepad/gedit/featherpad/nano/vim/vi /root/.smbcredentials
username=Administrator
password=

sudo mousepad/gedit/featherpad/nano/vim/vi /etc/fstab
# 在最后一行添加。注意不能删除任何行。
//192.168.1.6/a /mnt/a cifs vers=3.0,credentials=/root/.smbcredentials

1.5.5. 排错

1.5.5.1. mount error(115): Operation now in progress
# tail /var/log/syslog
CIFS: VFS: cifs_mount failed w/return code = -115

检测是自己开了 卡巴斯基安全软件 造成的,关掉就可以了。

1.6. 关机命令

$ sudo shutdown -h now

1.7. 开发

1.7.1. vscode

  • vscode 支持 32 Linux 的版本最高为 1.35.1,下载地址。国内镜像下载。
  • go 插件的兼容版本最高到 0.14.0,下载地址。国内镜像下载。

环境变量不支持全自动的 shell command,所以要手工将 /opt/code/bin 目录给 PATH 出来。(不要使用外层的那个 code,输入后会不退出)

1.7.2. sublime text

sublime text 配置就非常麻烦了,很多地方都需要手动。

参考了这篇文章来安装 go 的插件,不过有的插件过时了,需要替换,其中的 github 项目首页有提示。

注册码网上找一个可用的就行。注册后可以将每次启动升级的提示关掉(我用的是 Sublime 3 所以每次都会有更新提示)。

  • Tooling integration with the official sublime-build plugin.
  • Automatic formatting with the Gofmt plugin and goimports.
  • Code linting with the SublimeLinter plugin and gometalinter.
  • Autocompletion with the gocode package.
  • Code navigation with the GoGuru plugin.
  • Snippet management with Sublime Text’s inbuilt tool and the PackageResourceViewer plugin.
1.7.2.1. Tooling integration: install Package Control

Tools > Install Package Control

  1. Open the Sublime Text command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type Golang Build and hit Enter to install the package.
1.7.2.2. Automatic formatting: Gofmt
  1. Open the Sublime Text command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type Gofmt and hit Enter to install the package.
$ go get golang.org/x/tools/cmd/goimports
$ which goimports

Preferences > Package Settings > Gofmt > Settings - User

{
  "cmds": [
    ["goimports"]
  ],
  "format_on_save": true
}
1.7.2.3. Code linting
  • Install SublimeLinter 3 from here
  • Install SublimeLinter-golangcilint from here
  • Install the golangci-lint helper from here

三者的配置。

1.7.2.4. Autocompletion

gopls

1.7.2.5. Code navigation
$ go get golang.org/x/tools/cmd/guru
$ which guru
  1. Open command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type GoGuru and hit Enter to install the package.
1.7.2.6. Snippets
  1. Open command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type PackageResourceViewer and hit Enter to install the package.

1.8. 其它

1.8.1. 急救

使用 Debian 安装镜像无法进入 live CD, 进不了桌面, 不知道为啥, 换了 MX Linux 的安装盘才得以进入 Live Desktop。

MX Linux 的 Live Desktop 用户默认为 demo, 密码也是 demo

可以进入 File System 里面去选择挂载 60GB 的 Volume, 但是要先给 root 设置密码: sudo passwd root, root 用户密码如果为空的话是禁用的。

挂载好后鼠标点击进入该盘, 从上面地址栏复制地址, 一般是 /media/xxx, 将地址复制到 Terminal 里面进行删除即可。

1.8.2. 排错

1.8.2.1. Debian 查看启动日志

编辑 /etc/default/bootlogd

vim /etc/default/bootlogd

修改 BOOTLOGD_ENABLE 选项为

BOOTLOGD_ENABLE=Yes

保存退出, 以后在启动之后, 直接

cat /var/log/boot
1.8.2.2. debian 查看日志_如何使用Debian 10上的日志集中日志

How To Centralize Logs With Journald on Debian 10

1.8.3. 卸载磁盘出现 umount: /mnt: target is busy.

一般情况是自己 cd 在当前的文件夹下。

用下列方法可以查出来是哪个进程占用的: 使用 fuser 命令。

参考: fuser 命令的作用是使用文件或文件结构识别进程。

语法: fuser(选项)(参数)

参数

-a: 显示命令行中指定的所有文件; 
-k: 杀死访问指定文件的所有进程; 
-i: 杀死进程前需要用户进行确认; 
-l: 列出所有已知信号名; 
-m: 指定一个被加载的文件系统或一个被加载的块设备; 
-n: 选择不同的名称空间; 
-u: 在每个进程后显示所属的用户名。

此处用法:

  • 查看使用的进程: fuser -mv /mnt/
  • 杀死占用的进程: fuser -kv /mnt/

1.8.4. 从组中删除用户

编辑 /etc/group 找到 GROUP1 那一行, 删除 user

或者用命令

gpasswd -d A GROUP

1.8.5. xfce 中使用 expect 在 su 时免密码输入切换到 root 账户

apt-get install expect

比如我的密码是 appleyuchi, 那么下面的 send 的地方就改成 appleyuchi

下面的文件保存为 suz.sh(注意下面的内容必须在文件中置顶, 这些内容的前面不能有空行或者其他内容)

#!/usr/bin/expect  -f   
spawn su      
expect "Password:"
send "appleyuchi\r"
interact 

上面的意思是, spawn 开个进程, 用来模拟输入 su

expect 指的是, 在上面输入 su 后, 终端会出现的字符串, 可以看到下图中, 输入 su 以后出现了 Password:, 所以 expect 中填写输入 su 以后会出现的字符串

chmod u+x /usr/bin/suz.sh
chmod 777 /usr/bin/suz.sh

好了, 头疼的事情接下来要来了, 很多人复制了网上的脚本后, 改了改发现还是没法免输入密码, 调试方法是:

使用 expect suz.sh 来运行这个脚本, 而不是 ./suz.sh 来运行脚本, 因为前者会有更详细的报错信息

cp suz.sh /usr/bin

编辑 ~/.bashrc 文件, 在最后一行加入:

alias suz=/usr/bin/suz.sh

保存. bashrc 并且关闭

最后关闭当前终端, 重新开一个 terminal, 输入 suz, 效果如下:

Password:

另外一种情况, 如果终端在 su 的时候是中文的, 如图:

密码:

此时 suz.sh 需要修改如下:

#!/usr/bin/expect  -f     
spawn su        
expect "密码:"  
send "appleyuchi\r\n"  
interact   

注意上面 “密码:” 中的空格不能少, 并且终端用的冒号格式也很特殊 (冒号是半角还是圆角? 根据自己情况来改写脚本), 在 suz.sh 脚本中要保持高度一致才行。

你可能感兴趣的:(#,linux,Linux,Debian)