linux 基本使用、新建用户、emqtt 的基本使用

发布时间 2023-08-30 14:23:48作者: dysonnnn

连接云服务器

推荐软件

  1. putty
  2. xshell 点击下载
  3. windows10 cmd 命令行自带的 ssh

linux 基本命令:

按下按键 tab 可以自动补全文件名/目录名

# 显示当前目录的文件
ls		

# 切换到目录dir   ./(点/)是当前目录
cd dir

# 切换到上级目录
cd ..

# 显示当前路径
pwd		

# copy 拷贝文件到当前目录的 dir文件夹
cp file1  ./dir     

# copy 拷贝文件夹 dir1 到文件夹 dir2 内
cp dir1  ./dir2   -rf  

# remove 修改文件名
mv oldname newname   

# remove 移动文件到文件夹 dir1 中
mv file ./dir1   

# 在当前路径新建一个名为tdir目录
mkdir dir		

# 在当前路径新建一个名为file的文件
touch file   	

# 删除当前路径的名为file的文件
rm file 	


# 获取更新列表
sudo apt-get update 

# 更新软件
sudo apt-get upgrade  


# 下载文件使用,默认下载到当前路径
wget  下载地址 

# 下载 GitHub 上的文件到当前路径
git clone https://github.com/username/xxxxx.git 

vim 编辑器的使用

简单教程 Linux vi/vim

快速入门

# 新建一个txt文件并且编辑
vim test

vim有三种模式:

命令模式

进入 vim 编辑器之后直接按下以下按键可实现相应功能

命令 解释
H 向左移动
J 向下移动
K 向上移动
L 向右移动
shift+g 移动到最后一行
gg 移动到第一行
dd 删除当前行
ndd 向下删除n行
yy 复制当前行
nyy 复制从当前行开始的n行
p 粘贴到当前行的下面

编辑模式

进入 vim 编辑器之后直接按下以下按键可进入文本编辑模式

输入 i 或者 a # 左下角显示 insert,在光标当前位置插入内容
输入 o # 左下角显示 insert,可以在光标的下一行输入内容

底线命令模式

  1. 按下按键 ESC
    注意当前的输入模式必须英文
  2. 按下按键 shift+:
    低栏显示
    参数解释
参数 参数解释
w 保存
q quit 退出
强制执行

一般编辑完成后使用的命令:

:wq  #保存并且退出
:q!  #不保存,强制退出

新建用户,赋予管理员权限

推荐新建一个用户,操作/home/之外路径的文件的时候,
加前缀 sudo 获得临时的管理员权限进行操作,防止错误操作损坏系统。

新建用户操作

方法1-指定用户组创建

# 新建用户 用户组为root,用户名为iot,同时为用户在 /home 创建用户目录
root@iZ:~# useradd -m -g root iot    

# 为用户iot创建密码
root@iZ:~# passwd iot    
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

# 删除用户scau
userdel  scau

方法2-默认用户组创建

adduser username

接着根据提示输入密码,其余直接默认回车就可以了。

赋予新建的用户执行 sudo 的权限

执行命令

root@iZ:~# sudo vim /etc/sudoers

修改内容如下:

# User privilege specification
root ALL=(ALL) ALL

# 下面一行是新增的内容
iot ALL=(ALL:ALL) ALL   #iot为使用的用户名

切换用户命令

# 切换到用户iot
su iot  

# 切换到用户root
su root 

可能出现的问题

输入方向键的时候出现 [[A[[B[[A[[B[[A[[B,删除键也乱码,
原因是未指定使用的终端类型

解决方法:修改用户的终端类型

# 切换到root用户
su root 

# 编辑文件 /etc/passwd 
sudo vim /etc/passwd 

在文件的最后找到自己的用户名,例如用户名为 iot 的时候

iot:x:1001:0::/home/iot:

在末尾添加如下内容

/bin/bash

最后效果:

iot:x:1001:0::/home/iot:/bin/bash

emqtt配置

emqtt官网文档

下载软件emqtt,emqtt官网下载地址

cd ~  #切换到用户目录

#下载软件emqtt,这个是对应的ubuntu16.04 64bit的稳定版
wget http://emqtt.com/static/brokers/emqttd-ubuntu16.04-v2.3.11.zip  



#安装zip unzip软件
sudo apt-get install zip unzip

#解压软件到当前路径
unzip emqttd-ubuntu16.04-v2.3.11.zip

开启emqttd软件

#进入emqttd软件目录
cd emqttd

# 启动emqttd
./bin/emqttd start

# 检查运行状态
./bin/emqttd_ctl status

Node 'emq@127.0.0.1' is started
emqttd 2.3.11 is running           #这样表示软件开启成功

# 停止emqttd
./bin/emqttd stop

开启云服务器的18083端口和1883端口 参考以下教程
开启阿里云服务器端口

EMQ 消息服务默认允许匿名认证,启动后 MQTT 客户端可连接1883端口,启动运行日志输出在 log/ 目录。

打开网址 ip:18083 就可以看到emqttd的管理页面了

  • 默认用户: admin,密码:public

mqtt实践

操作环境搭建

操作平台:

windows 系统,要求装有 python 软件(例如 python 官网下载的 python3 或者配合 pycharm 使用
或者直接在云服务器操作(云服务器自带 python3 软件)

安装 python3paho-mqtt

  1. 先安装 python3pip
sudo apt-get install python3-pip
  1. 然后再用 pip 命令安装 paho-mqtt
sudo python3 -m pip install paho-mqtt

以上方法安装失败的话 参考 教程 Python pip 安装与使用 再次进行安装

创建 python 文件

用文本编辑器新建 Consumer.py 和 Producer.py 文件

  1. Consumer.py 文件
    功能: mqtt 主题的订阅以及接收主题中发布的信息。
    内容:
#!/usr/bin/python3  
#以上是指定运行的软件路径
import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.

    #订阅主题 1234
    client.subscribe("1234")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
	# 打印主题名字 + 指定字符串内容 + 生产者发布的内容
    print(msg.topic+" "+str(msg.payload))  


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60) # 客户端连接到指定的 ip 地址和端口

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()   #客户端一直运行

  1. Producer.py 文件
    功能: 往 mqtt 中指定的主题发布信息。
    内容:
#!/usr/bin/python3  
#以上是指定运行的软件路径
import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client:mqtt.Client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    
    # 往 Consumer 订阅的主题发送信息 
    client.publish("1234","i send a message")  

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload)) 

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
  
client.connect("iot.eclipse.org", 1883, 60)   # 客户端连接到指定的 ip 地址和端口

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()        #客户端一直运行
print("end")  

将代码内的 ip 地址修改为自己云服务器的地址。

执行客户端代码

  • 如果是用ssh连接到云服务器操作的话,分开两个窗口执行代码。
python3 Consumer.py    
python3 Producer.py   

运行完以上两个程序之后,可以看到以下效果:

python Consumer.py
Connected with result code 0
1234  send a message    #接收到 Producer 发送的信息
python3 Producer.py
Connected with result code 0

打开网址 ip:18083 可以查看 Consumer.py 文件订阅的主题

停止执行程序,输入 ctrl+c 即可。

mysql数据库安装

sudo apt-get install mysql-server mysql-client libmysqlclient-dev
  • 安装过程中会提示设置密码

  • 查看mysql 运行情况

sudo netstat -tap | grep mysql
  • 登陆mysql数据库可以通过如下命令:
mysql -u root -p 

-u 表示选择登陆的用户名, -p 表示登陆的用户密码,上面命令输入之后会提示输入密码,输入密码就可以登录到mysql。

退出命令

quit