windows+vagrant+python+emacs開發環境配置
背景說明
公司的電腦一般都是windows系統的。但是windows系統如果配置開發環境,在安裝一些依賴的時候經常會抓狂。
Vagrant是一個基於Ruby的工具,用於創建和部署虛擬機。
通過vagrant工具在windows系統上拉起一臺linux系統的虛擬機,在上面安裝emacs,然後通過 mobaxterm 工具的X server 功能,在windows上遠程打開虛擬機中的emacs GUI界面。
vagrant
vagrant下載地址:https://www.vagrantup.com/
從官網上下載vagrant的最新版後直接安裝在機器上。
vagrant box
在拉起虛擬機前我們需要有一個基礎的虛擬機鏡像包,鏡像包一般是xxxx.box文件。
假設我們從同事手中或者從網上獲取到了一個ubuntu18.04的box。
我們可以將這個box添加到我們的box list中
vagrant box add box的名字 box的絕對路徑
添加完後查看系統中存在的box
vagrant box list
配置
在D盤下創建一個目錄,然後進入到目錄裏。在目錄中按住shift+右鍵,選擇在此處打開命令行。
在打開的cmd窗口中執行命令。
vagrant init python_emacs_VM
python_emacs_VM 名字根據個人需要修改。
這個時候,在當前目錄下就會生成一個Vagrantfile文件。
使用編輯器打開這個文件。
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "ubuntu18.04" config.vm.hostname = "myVM" config.vm.network "public_network", ip: "192.168.1.120" config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui= false # Customize the amount of memory on the VM: # vb.memory = "1024" # vb.name = "mywork" vb.customize ["modifyvm", :id, "--name", "python_emacs", "--memory", "512"] end end
config.vm.box 指定基礎鏡像文件的名稱,如果box名稱在本地的box list不存在,那麽vagrant會嘗試去vagrant cloud中在線尋找,如果找到就會自動下載。
你也可以到https://app.vagrantup.com/boxes/search 裏搜尋你想要的鏡像包,然後把名字填到這裏就可以了。不過這個下載速度會比較慢。
config.vm.network "public_network" 指定使用公共網絡,需要指定一個ip。在cmd命令行中執行ipconfig可以查看本機的IP,然後配置一個同網段中沒人使用的配置上去就好。
當然也有其他網絡配置。你可以自己搜索解決。
vb.customize 指定了虛擬機在vm box中顯示的名稱,和使用的內存大小。
文件配置好了後,執行 vagrant up 就可以啟動虛擬機了。
然後執行vagrant ssh 就可以通過默認的用戶vagrant連上虛擬機。
新增個人用戶
ubuntu新增用戶命令自行查找。
建議新增一個有sudo權限的用戶。
一般新建的虛擬機會碰到在windows上使用xshell等工具連不上的問題。
可以通過修改/etc/ssh/sshd_confg文件解決。
sshd_config文件參考:
1 # Package generated configuration file 2 # See the sshd_config(5) manpage for details 3 4 # What ports, IPs and protocols we listen for 5 Port 22 6 # Use these options to restrict which interfaces/protocols sshd will bind to 7 #ListenAddress :: 8 ListenAddress 0.0.0.0 9 Protocol 2 10 # HostKeys for protocol version 2 11 HostKey /etc/ssh/ssh_host_rsa_key 12 HostKey /etc/ssh/ssh_host_dsa_key 13 HostKey /etc/ssh/ssh_host_ecdsa_key 14 HostKey /etc/ssh/ssh_host_ed25519_key 15 #Privilege Separation is turned on for security 16 UsePrivilegeSeparation yes 17 18 # Lifetime and size of ephemeral version 1 server key 19 KeyRegenerationInterval 3600 20 ServerKeyBits 1024 21 22 # Logging 23 SyslogFacility AUTH 24 LogLevel INFO 25 26 # Authentication: 27 LoginGraceTime 120 28 PermitRootLogin prohibit-password 29 StrictModes yes 30 31 RSAAuthentication yes 32 PubkeyAuthentication yes 33 #AuthorizedKeysFile %h/.ssh/authorized_keys 34 35 # Don‘t read the user‘s ~/.rhosts and ~/.shosts files 36 IgnoreRhosts yes 37 # For this to work you will also need host keys in /etc/ssh_known_hosts 38 RhostsRSAAuthentication no 39 # similar for protocol version 2 40 HostbasedAuthentication no 41 # Uncomment if you don‘t trust ~/.ssh/known_hosts for RhostsRSAAuthentication 42 #IgnoreUserKnownHosts yes 43 44 # To enable empty passwords, change to yes (NOT RECOMMENDED) 45 PermitEmptyPasswords no 46 47 # Change to yes to enable challenge-response passwords (beware issues with 48 # some PAM modules and threads) 49 ChallengeResponseAuthentication no 50 51 # Change to no to disable tunnelled clear text passwords 52 PasswordAuthentication yes 53 54 # Kerberos options 55 #KerberosAuthentication no 56 #KerberosGetAFSToken no 57 #KerberosOrLocalPasswd yes 58 #KerberosTicketCleanup yes 59 60 # GSSAPI options 61 #GSSAPIAuthentication no 62 #GSSAPICleanupCredentials yes 63 64 X11Forwarding yes 65 X11DisplayOffset 10 66 PrintMotd no 67 PrintLastLog yes 68 TCPKeepAlive yes 69 #UseLogin no 70 71 #MaxStartups 10:30:60 72 #Banner /etc/issue.net 73 74 # Allow client to pass locale environment variables 75 AcceptEnv LANG LC_* 76 77 Subsystem sftp /usr/lib/openssh/sftp-server 78 79 # Set this to ‘yes‘ to enable PAM authentication, account processing, 80 # and session processing. If this is enabled, PAM authentication will 81 # be allowed through the ChallengeResponseAuthentication and 82 # PasswordAuthentication. Depending on your PAM configuration, 83 # PAM authentication via ChallengeResponseAuthentication may bypass 84 # the setting of "PermitRootLogin without-password". 85 # If you just want the PAM account and session checks to run without 86 # PAM authentication, then enable this but set PasswordAuthentication 87 # and ChallengeResponseAuthentication to ‘no‘. 88 UsePAM yesView Code
修改配置文件後,需要重啟ssh服務
sudo service ssh restart
這個時候我們可以配置vagrantfile,指定vagrantg 的默認登陸用戶為我們新增的用戶。
參考:
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don‘t change it unless you know what # you‘re doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "ubuntu18.04" config.vm.hostname = "emacs" config.vm.network "public_network", ip: "192.168.1.120" config.ssh.username = ‘ubuntu‘ config.ssh.password = ‘ubuntu‘ config.ssh.insert_key = false config.ssh.private_key_path = ["~/.ssh/id_rsa"] config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys" config.vm.synced_folder ".", "/vagrant", disabled: true # config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)" config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = false # Customize the amount of memory on the VM: # vb.memory = "1024" # vb.name = "mywork" vb.customize ["modifyvm", :id, "--name", "emacs", "--memory", "512"] end end
修改文件後,執行
vagrant provision
將ssh的公鑰放到虛擬機上,免去連接虛擬機時需要輸入密碼的麻煩。ssh公鑰私鑰怎麽配置,請百度windows配置ssh。
emacs
個人使用emacs作為平時的編輯器。如果你使用pycharm等IDE工具,這些工具有很好的vagrant插件,可以根據相關攻略配置。
安裝配置
安裝, 最好使用ubuntu的官方源。不知是什麽原因,我換為阿裏雲的源是找不到emacs25的。
sudo apt-get update
sudo apt-get install git sudo apt-get install emacs25
下載完成後就可以將自己的配置文件下載到虛擬機上了。
emacs遠程編輯
爽歪歪的時候到了。
打開mobaxterm 工具,ssh連接上虛擬機。
emacs&
開始寫代碼吧!
打包
我們這個安裝了emacs的鏡像可以打個包,作為基礎鏡像。
在cmd中輸入打包命令
vagrant package ubuntu18.04_emacs.box
windows+vagrant+python+emacs開發環境配置