Skip to content

Latest commit

 

History

History
373 lines (252 loc) · 11.1 KB

20220912_01.md

File metadata and controls

373 lines (252 loc) · 11.1 KB

MacOS 制作ubuntu USB安装介质并安装和配置Ubuntu, openssh-server和expect ssh登陆脚本

作者

digoal

日期

2022-09-12

标签

PostgreSQL , ubuntu , usb , openssh-server , sshd_config


背景

假期翻出一台家里的老笔记本, 配置还可以(i7 8代, 16G内存, 256SSD), 拿出来当服务器用.

  • 下载ubuntu镜像, 制作ubuntu USB安装介质;
  • 安装ubuntu, 配置无线网络, 优化无线网络, 设置默认启动非图形界面, 安装openssh-server, 配置sshd;
  • 编写MacOS 自动ssh连接ubuntu脚本;

一、制作ubuntu usb安装介质

1、下载ubuntu, 选择就近, 网络带宽较大的网站下载:

https://launchpad.net/ubuntu/+cdmirrors

ubuntu-20.04.5-desktop-amd64.iso

2、检查checksum, 对比ubuntu给出的sum是否一致, 确保下载的iso文件正确.

shasum -a 256 ./ubuntu-22.04.1-desktop-amd64.iso  

3、插入并umount usb

diskutil list  
  
/dev/disk2为u盘设备号  
  
digoaldeAir:Downloads digoal$ diskutil umountDisk /dev/disk2  
Unmount of all volumes on disk2 was successful  

4、将镜像写入usb, 使用/dev/rdisk更快.

digoaldeAir:Downloads digoal$ sudo dd if=./ubuntu-20.04.5-desktop-amd64.iso of=/dev/rdisk2 bs=1m  
Password:  
3649+1 records in  
3649+1 records out  
3826831360 bytes transferred in 331.014593 secs (11560914 bytes/sec)  

为什么呢? 在OS X中,每个磁盘在/dev中可能有两个路径引用:

  • /dev/disk #是缓冲设备,这意味着要发送的所有数据都经过了额外的处理。
  • /dev/rdisk #是原始路径,速度更快,并且在使用dd程序时完全可以。

在Class 4 SD卡上,使用rdisk路径的差异大约快20倍。

5、推出usb

二、使用usb在非macbook中安装ubuntu

1、使用USB设备启动

2、安装desktop版,

3、安装完毕, 移除usb后, enter重启, 完成安装.

4、安装openssh-server

sudo apt-get install openssh-server  
  
如果报错, 可能是依赖特定版本的openssh-client  
  
sudo apt-get install openssh-client=1:8.2p1-4ubuntu0.2  
sudo apt-get install openssh-server  

5、配置sshd

sudo vi /etc/ssh/sshd_config  
  
去除如下几行的注释:   
Port 22  
ListenAddress 0.0.0.0  
PasswordAuthentication yes  
TCPKeepAlive yes  

6、配置默认启动到命令行界面

https://blog.csdn.net/joker00007/article/details/120658259

sudo systemctl set-default multi-user.target   

如果要改回来图形界面启动:

sudo systemctl set-default graphical.target  

如果要在命令行界面启动图形界面:

startx  

7、关闭无线网卡电源管理, 获得更稳定的无线网络性能

查询无线网卡设备名

iwconfig  
  
wlp5s0    IEEE 802.11  ESSID:"Redmi_keting_5G"    
          Mode:Managed  Frequency:5.805 GHz  Access Point: 28:D1:27:5A:A3:A8     
          Bit Rate=433.3 Mb/s   Tx-Power=22 dBm     
          Retry short limit:7   RTS thr:off   Fragment thr:off  
          Power Management:off  
          Link Quality=55/70  Signal level=-55 dBm    
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0  
          Tx excessive retries:0  Invalid misc:43   Missed beacon:0  

临时关闭无线网卡电源管理

sudo iwconfig wlp5s0 power off  

永久关闭无线网卡电源管理

cd /etc/NetworkManager/conf.d/  
sudo vi default-wifi-powersave-on.conf   
  
[connection]  
wifi.powersave = 2  

8、其他配置, 笔记本当称服务器用.

  • 1 region. EN_US

  • 2 关闭蓝牙

  • 3 插线时不使用节能, 不待机等

  • 4 不关闭网卡节能

  • 5 关闭自动安装更新(仅提示安全类更新, 但是需要人工进行更新)

  • 6 cpu主频设置: https://blog.csdn.net/xuershuai/article/details/122023817

  • 7 合盖和电源按钮配置:

sudo vi /etc/systemd/logind.conf
设置如下:
HandlePowerKey=ignore
HandleLidSwitch=ignore

重启systemd-logind服务
sudo systemctl restart systemd-logind
  • 8 GeForce MX150 驱动:
https://askubuntu.com/questions/1117153/how-enable-use-geforce-mx150
https://forums.developer.nvidia.com/t/ubuntu-20-04-geforce-mx150-couldnt-communicate-with-the-nvidia-driver/166514

ubuntu-drivers devices
sudo ubuntu-drivers autoinstall

重启ubuntu

查询nvidia卡信息:
digoal@digoal-Haier5000A:~$ nvidia-smi
Mon Sep 12 18:02:07 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 515.65.01    Driver Version: 515.65.01    CUDA Version: 11.7     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   38C    P0    N/A /  N/A |      0MiB /  2048MiB |      2%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
  
查询当前默认显卡, 设置默认显卡:
Alternatively, you can open your terminal and write the command:

prime-select query

to check which of the cards is your system using, and the commands

sudo prime-select nvidia

or

sudo prime-select intel

to change between the two cards and then restart the session.
  • 9 安装chrome, 并配置chrome使用gpu加速:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb

https://blog.csdn.net/chijiefen1087/article/details/100854188

  • 10 永久设置DNS:

未修改前, resolv.conf指向../run/systemd/resolve/stub-resolv.conf

digoal@digoal-Haier5000A:~$ ll /etc/resolv.conf   
lrwxrwxrwx 1 root root 39 Sep 12 15:58 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf  

修改/etc/resolv.conf重启后立马又会变回去. 为了永久修改dns, 按如下操作:

sudo vi /etc/systemd/resolved.conf  
DNS=8.8.8.8 114.114.114.114  

重启systemd-resolved服务

sudo systemctl restart systemd-resolved  
sudo systemctl enable systemd-resolved  

重新软链/etc/resolv.conf

digoal@digoal-Haier5000A:~$ sudo mv /etc/resolv.conf /etc/resolv.conf.bak  
digoal@digoal-Haier5000A:~$ sudo ln -s /run/systemd/resolve/resolv.conf /etc/  
digoal@digoal-Haier5000A:~$ ll /etc/resolv.conf  
lrwxrwxrwx 1 root root 32 Sep 12 21:46 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf  

重启

sudo reboot  

检查当前GLOBAL DNS配置

systemd-resolve --status  
  
         DNS Servers: 8.8.8.8               
                      114.114.114.114   

检查 /etc/resolv.conf

cat /etc/resolv.conf  
# This file is managed by man:systemd-resolved(8). Do not edit.  
#  
# This is a dynamic resolv.conf file for connecting local clients directly to  
# all known uplink DNS servers. This file lists all configured search domains.  
#  
# Third party programs must not access this file directly, but only through the  
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,  
# replace this symlink by a static file or a different symlink.  
#  
# See man:systemd-resolved.service(8) for details about the supported modes of  
# operation for /etc/resolv.conf.  
  
nameserver 8.8.8.8  
nameserver 114.114.114.114  
nameserver 192.168.28.1  

使用nsloop检查是否使用了配置的DNS

digoal@digoal-Haier5000A:~$ nslookup www.baidu.com  
Server:		8.8.8.8  
Address:	8.8.8.8#53  
  
Non-authoritative answer:  
www.baidu.com	canonical name = www.a.shifen.com.  
Name:	www.a.shifen.com  
Address: 14.215.177.38  
Name:	www.a.shifen.com  
Address: 14.215.177.39  

三、配置macos ssh登陆Ubuntu脚本.

《Linux Mac ssh 客户端长连接防断连 - tcp心跳 TCPKeepAlive,ServerAliveInterval,ServerAliveCountMax》

《Linux/Mac ssh 自动输入密码 - expect使用》

vi haier.sh  
  
  
#!/usr/bin/expect  
  
set user "digoal"  
set host "192.168.28.158"  
set port "22"  
set pwd "rootroot"  
  
spawn ssh -o TCPKeepAlive=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=3 $user@$host -p $port  
expect {  
"yes/no" { send "yes\r"; exp_continue }  
"password:" { send "$pwd\r" }  
}  
interact  
chmod 500 haier.sh  

登陆ubuntu

./haier.sh  

digoal's wechat