DAY01
更新源
打开终端(Ctrl+Alt+T),执行命令
sudo gedit /etc/apt/sources.list
将文本框的内容删除,粘贴以下内容,保存并退出
deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
执行命令
sudo apt-get update
sudo apt-get upgrade
配置VIM
打开终端,执行命令
sudo gedit /etc/hosts
将以下两行文本添加到文本框中,保存并退出
192.30.255.112 github.com git
185.31.16.184 github.global.ssl.fastly.net
执行命令
sudo wget -qO- https://raw.github.com/ma6174/vim/master/setup.sh | sh -x
安装sshpi及scppi
sshpi.sh参考代码
#!/bin/bash
#文件名:sshpi.sh
#用途:远程连接树莓派
function Usage() {
echo "Usage: sshpi Username@pi"
echo "Like This:sshpi UserA@pi1"
}
if [[ ! $# -eq 1 ]]; then
Usage
exit
fi
echo $1 | grep "@" >/dev/zero 2>&1
if [[ ! $? -eq 0 ]]; then
echo "argument wrong!"
Usage
exit
fi
Username=`echo $1 | cut -d "@" -f 1`
if [[ ${Username}x == x ]]; then
echo "Please input your username!"
Usage
exit
fi
Hostname=`echo $1 | cut -d "@" -f 2`
if [[ ${Hostname}x == x ]]; then
echo "Please input Hostname of Pi!"
Usage
exit
fi
echo $Hostname | grep -w "^pi[1-9][0-9]\?" >/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
echo "Hostname is Wrong!"
Usage
exit
fi
HostNum=`echo $Hostname | cut -c 3-`
if [[ $HostNum -gt 22 ]]; then
echo "Hostname is Wrong!"
Usage
exit
fi
port=$[6530 + $HostNum]
echo -e "\033[46;30m You Will login\033[0m\033[46;31m $Hostname\033[0m\033[46;30m with Username \033[46;31m$Username\033[0m\033[46;30m, enjoy it!\033[0m"
ssh -p $port ${Username}@zentao.haizeix.tech
scppi.sh参考代码
#!/bin/bash
#文件名:scppi.sh
#用途:远程拷贝文件,在树莓派和本机之间
function Usage() {
echo "Usage: scppi file_or_dir Username@piname:dest_file_or_dir"
echo " scppi Username@piname:./file_or_dir file_or_dir"
echo "Like This:scppi PiHealth pi@pi1:./new"
}
if [[ ! $# -eq 2 ]]; then
Usage
exit
fi
#1.源
#2.目标
echo $1 | grep -q "@"
if [[ $? -eq 0 ]]; then
remote=$1
local=$2
else
remote=$2
local=$1
fi
echo $remote | grep @ | grep : >/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
echo "argument wrong!"
Usage
exit
fi
Username=`echo $remote | cut -d "@" -f 1`
if [[ ${Username}x == x ]]; then
echo "Please input your username!"
Usage
exit
fi
Hostname=`echo $remote | cut -d "@" -f 2 | cut -d ":" -f 1`
if [[ ${Hostname}x == x ]]; then
echo "Please input Hostname of Pi!"
Usage
exit
fi
echo $Hostname | grep -w "^pi[1-9][0-9]\?" >/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
echo "Hostname is Wrong!"
Usage
exit
fi
dir_file=`echo $remote | cut -d "@" -f 2 | cut -d ":" -f 2`
if [[ ${dir_file}x == x ]]; then
echo "Please input dest_file_or_dir of Pi!"
Usage
exit
fi
HostNum=`echo $Hostname | cut -c 3-`
if [[ $HostNum -gt 22 ]]; then
echo "Hostname is Wrong!"
Usage
exit
fi
port=$[6530 + $HostNum]
echo -e "\033[46;30m Coping \033[46;31m$1\033[46;30m to \033[46;31m$dir_file\033[46;30m on \033[46;31m$Hostname\033[46;30m with Username \033[46;31m$Username\033[46;30m, enjoy it!\033[0m"
if [[ $1 = $local ]]; then
scp -P $port -r $1 ${Username}@zentao.haizeix.tech:$dir_file
else
scp -P $port -r ${Username}@zentao.haizeix.tech:$dir_file $local
fi
打开终端,执行命令
vim sshpi.sh
在VIM文本框里粘贴sshpi脚本的代码,保存并退出(:wq)
将sshpi.sh重命名为sshpi,赋予sshpi脚本可执行权限
mv sshpi.sh sshpi
chmod a+x sshpi
执行命令
echo $PATH
将sshpi复制到PATH路径下
cp sshpi /bin
同样的操作生成scppi脚本
DAY02
设置免密登录
打开终端,执行命令
sshpi lws@pi3
暂时需要密码(haizei)来连接树莓派
在本机生成秘钥
ssh-keygen
将秘钥复制到authorized_keys文件中
cat .ssh/id_rsa.pub>>.ssh/authorized_keys
将本机的authorized_keys文件复制到树莓派中
scppi .ssh/authorized_keys lws@pi3
再次连接树莓派,验证是否成功
check.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
int socket_connect(int port, char *host) {
int sockfd;
struct sockaddr_in dest_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket() error");
return -1;
}
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
dest_addr.sin_addr.s_addr = inet_addr(host);
if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0) {
perror("connect() error");
return -1;
}
return sockfd;
}
int main() {
int socket_fd;
struct passwd *pwd;
pwd = getpwuid(getuid());
char ip_addr[20] = "192.168.1.40";
int port = 8888;
char username[20] = {0};
strcpy(username, pwd->pw_name);
socket_fd = socket_connect(port, ip_addr);
if (socket_fd < 0)
{
exit(1);
}
if (send(socket_fd, username, strlen(username), 0) > 0) {
printf("Check Success\n");
}
close(socket_fd);
return 0;
}
gcc check.c -o check
VIM的简单使用
[外链图片转存失败(img-nR2ENjjQ-1563687390389)(https://ese3a9b6c5d0ic.prissl.qiqiuyun.net/course-activity-272/20190411093248-lntidnybu9wksckc/53eb89158008bece_img3?e=1563481633&token=ExRD5wolmUnwwITVeSEXDQXizfxTRp7vnaMKJbO-:CAFs0zlzq99uvJ2s6B6SYjVIbcI=)]
VIM的四种模式
普通模式
命令 | 说明 |
---|---|
x d dd ndd dw d$ d^ dG dnG | 删除说明 |
y yy yG ynG y$ y^ | 复制 |
p P | 粘贴 |
gg GG ngg | 移动光标 |
R cc cG cnG c$ c^ | 替换 |
u ctrl+r ctrl+v | undo redo 可视块操作 |
插入模式
i | 在光标之前追加 |
---|---|
a | 在光标之后追加 |
A | 在一行的结尾处追加 |
I | 在一行的开头处插入 |
o | 在光标所在位置的下一行打开新行插入 |
O | 在光标所在位置的上一行打开新行插入 |
命令模式
命令 | 说明 |
---|---|
:w :q :wq :x :wq! :q! | 文件的保存与退出操作 |
:args | 显示文件名称,切换文件 |
:e foo.txt | 打开foo.txt |
:saveas ~/foo.txt | 另存为~/foo.txt |
:split :vsplit | 切分窗口 |
:set | 设置选项 |
其他使用技巧
Ctrl+z | 挂起 |
---|---|
fg | 返回前台 |
/findstring | 查找 |
Python的升级与tldr的升级
打开终端,执行命令
sudo apt-get install python
sudo apt-get install python3
查看Python版本
python -V
python3 -V
获取Python路径
which python
查看Python
ls -al /usr/bin/python
强制删除Python,将Python3.5指向Python
sudo rm /usr/bin/python
sudo ln /usr/bin/python3.5 /usr/bin/python
安装pyhton3-pip
sudo apt-get insatll python3-pip
升级pip
pip install --upgrade pip
pip3 list报错,解决
sudo vim /usr/bin/pip3
在文本框中pip后加上._internal
安装tldr
sudo pip install tldr
Linux极简入门
软件的安装与卸载
- apt-get update
- apt-get upgrade
- apt-get install xxx
- apt-get --purge remove xxx
- apt-cache search xxx
目录
命令 | 功能 |
---|---|
cd | 切换当前目录 |
pwd | 打印当期目录工作 |
mkdir | 创建目录 |
cd切换工作
-
cd /etc 直接切换到/etc目录
-
cd … 切换到上层目录
-
cd . 切换到当前目录
-
cd 回到自己的家目录
-
cd ~ 回到自己的家目录
-
cd - 回到上次工作目录
pwd打印当前工作目录
pwd [-LP]
- -L 显示逻辑工作目录
- -P 显示物理工作目录
mkdir创建目录
mkdir [-pm]
-
-p 自动创建父目录
-
-m 设置权限